Can you answer these queries HDU -? 4027 (tree line, the square root of the interval, the interval sum)

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6

The meaning of problems:
you array comprising a number of n, m operations

0 operation, give you an interval l, r square root of the number of each interval.

Operation 1, the output value of a sum and interrogation interval.


Ideas:

We see the data range is less than or equal to 2 ^ 63, we can be found through local prescribing tests,

Each number in the array, prescribing a maximum of seven times, you can go to 1 and 1 ,, whether ye are prescribing or 1, that is, the value will not change.

So we use the tree line maintenance interval sum and,

For updates, we update violence to each leaf node of the tree line, the summation on the normal sum.

There is little need to optimize the tree line is that if a range of sum and equal to the length of the interval, the results for each value of this interval is 1, then the direct return, do not update because the update is meaningless.

The title pits two points ::

1, to the range of x, y, not x <y, it is possible x> y wa here many times.

2, talked about the problem face each sample multiple output a carriage return, the beginning did not see. Pe here once.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m;
int root;
ll a[maxn];// 初始点权
ll wt[maxn];// 新建编号点权。
int cnt;// 编号用的变量
int top[maxn];// 所在重链的顶点编号
int id[maxn];//节点的新编号。
std::vector<int> son[maxn];
int SZ[maxn];// 子数大小
int wson[maxn];// 重儿子
int fa[maxn];// 父节点
int dep[maxn];// 节点的深度
struct node
{
    int l,r;
    ll sum;
    ll laze;
}segment_tree[maxn<<2];


void pushup(int rt)
{
    segment_tree[rt].sum=(segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum);
}
void build(int rt,int l,int r)
{
    segment_tree[rt].l=l;
    segment_tree[rt].r=r;
    segment_tree[rt].laze=0;
    if(l==r)
    {
        segment_tree[rt].sum=wt[l];
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}

void update(int rt,int l,int r)
{
    if((segment_tree[rt].l>=l&&segment_tree[rt].r<=r)&&segment_tree[rt].sum==(segment_tree[rt].r-segment_tree[rt].l+1))
    {
        return ;
    }
    if(segment_tree[rt].l==segment_tree[rt].r)
    {
        segment_tree[rt].sum=sqrt(segment_tree[rt].sum);
        return ;
    }
    int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
    if(mid>=l)
    {
        update(rt<<1,l,r);
    }
    if(mid<r)
    {
        update(rt<<1|1,l,r);
    }
    pushup(rt);
}
ll query(int rt,int l,int r)
{
    if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r)
    {
        ll res=0ll;
        res+=segment_tree[rt].sum;
        return res;
    }
    int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
    ll res=0ll;
    if(mid>=l)
    {
        res+=query(rt<<1,l,r);
    }
    if(mid<r)
    {
        res+=query(rt<<1|1,l,r);
    }
    return res;

}

int main()
{
    // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    // freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);

    gbtb;
    int cas=1;
    while(cin>>n)
    {
        cout<<"Case #"<<cas<<":"<<endl;
        repd(i,1,n)
        {
            cin>>wt[i];
        }
        build(1,1,n);
        cin>>m;
        int op;
        int x,y;
        while(m--)
        {
            cin>>op>>x>>y;
            if(x>y)
            {
                swap(x,y);
            }
            if(!op)
            {
                update(1,x,y);
            }else
            {
                cout<<query(1,x,y)<<endl;
            }
        }
        cout<<endl;
        cas++;
    }


    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11306562.html