Codevs 2492 God-made questions for seven minutes 2 (segment tree)

Source: Internet
Author: User
Tags time limit

Time limit: 1 s
Space limit: 64000 KB
Title Level:Master Master
Title Description Description
Xlk that "God made the seven minutes of the problem" is not very enjoyable, so there is the second part.
"In the first minute, X says, to have a sequence, a positive integer sequence is given."
The second minute, L said, to be able to modify, so there is a number of each number in the square (the next rounding) operation.
The third minute, K said, to be able to query, so there is to seek a number of operations.
Four minutes, Rainbow Meow said, if Noip difficulty, so there is the data range.
Five minutes, the poet said, to have rhyme, so there is time limit and memory limit.
Six minutes, and the snow said, to save things, so there is a guarantee in the process and the final result does not exceed the 64-bit signed integer type of the range of the limit.
The first seven minutes, the problem was finally finished, however, the god of the cows do not want to write the problem of the program. ”
--seven minutes of God's problems • Part II
So this sacred mission is yours.
Enter a description input Description
The first line is an integer n, which represents the number of numbers in the sequence.
The second row n positive integers that represent the number in the initial state of the sequence.
The third line, an integer m, indicates that there is an M operation.
The next m line is three integers per line k,l,r,k=0 for each number in [l,r] squared (rounded down), and K=1 is asking for the sum of each number in [L,r].
UPD: Note that the data may be l>r, so in this case, swap L and R.
Outputs description Output Description
For the inquiry operation, each line outputs an answer.
Sample input to sample
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 Sample Outputs
19
7
6
Data Size & Hint
For 30% of the data, 1<=n,m<=1000, the number in the series does not exceed 32767.
For 100% of the data, 1<=n,m<=100000,1<=l,r<=n, the number in the sequence is greater than 0, and no more than 1e12.
Note that L may be greater than r, and in this case please exchange l,r.
Source: Nescafe 20
Category Label Tags
The tree structure of tree-like array segment

/* Line tree violence.
Sweep to the leaf node each time.
Well, of course not.
Need to add a little bit of optimization.
We know that a few times to take sqrt will become 1.
Then it is no contribution to take sqrt again at this time.
We'll write them down to make a tag that says the interval can be changed again. 
It's also very slow, after all, it's violence. */#include <iostream> #include <cstdio> #include <cmath> #define MAXN 100001 #define LL long Long using NA
Mespace std;
int n,m,cut; struct Data{int l,r,lc,rc;bool bj; LL x;}
TREE[MAXN*4];
    ll read () {ll x=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
    } void Build (int l,int r) {int k=++cut;
    Tree[k].l=l,tree[k].r=r;
    if (l==r) {tree[k].x=read (); return;}
    int mid= (L+R) >>1;
    Tree[k].lc=cut+1;build (L,mid);
    Tree[k].rc=cut+1;build (MID+1,R);
    tree[k].x=tree[tree[k].lc].x+tree[tree[k].rc].x;
return;
    } LL query (int k,int l,int R) {if (l<=tree[k].l&&tree[k].r<=r) return tree[k].x;
    int mid= (TREE[K].L+TREE[K].R) >>1;
    LL tot=0; if (l<=mid) tot+=query (tree[k].lc,l, R);
    if (r>mid) tot+=query (tree[k].rc,l,r);
return tot;
    } void Change (int k,int l,int R) {if (TREE[K].BJ) return;
        if (TREE[K].L==TREE[K].R) {if (tree[k].x!=1) tree[k].x=sqrt (tree[k].x);
        if (tree[k].x==1) tree[k].bj=true;
    return;
    } int mid= (TREE[K].L+TREE[K].R) >>1;
    if (l<=mid) change (TREE[K].LC,L,R);
    if (r>mid) change (TREE[K].RC,L,R);
    tree[k].x=tree[tree[k].lc].x+tree[tree[k].rc].x;
    tree[k].bj=tree[tree[k].lc].bj&tree[tree[k].rc].bj;
return;
    } int main () {int x, y, Z;
    N=read ();
    Build (1,n);
    M=read ();
        while (m--) {z=read (); X=read (), Y=read ();
        if (x>y) swap (x, y);
        if (!z) change (1,x,y);
    else printf ("%lld\n", Query (1,x,y));
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.