Title Description
Description
Give you the number of n, there are two operations:
1: Add x for all numbers of interval [a, b]
2: Ask for the number of intervals [A, b] and.
Enter a description
Input Description
The first line is a positive integer n, and the next n rows n integers,
Then the next positive integer q, each line represents the number of operations,
If the first number is 1, followed by 3 positive integers,
Indicates that each number in the interval [a, b] is increased by x, if it is 2,
Represents the number of the and of the action 2 ask interval [A, b].
Pascal player please do not use READLN to read in
Output description
Output Description
One answer for each query output line
Sample input
Sample Input
3
1
2
3
2
1 2 3 2
2 2 3
Sample output
Sample Output
9
Data range and Tips
Data Size & Hint
Data range
1<=n<=200000
1<=q<=200000
Idea: Line segment tree + delay tag
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>#defineTrue Ture#defineFalse flaseusing namespacestd;#definell Long LongintScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}struct is{ intL,r; ll num; intlazy;} tree[200010*3];voidBuild_tree (intLintRintPOS) {TREE[POS].L=l; TREE[POS].R=R; Tree[pos].lazy=0; if(l==r) {//Tree[pos].num=1;scanf"%lld",&tree[pos].num); return; } intMid= (L+R)/2; Build_tree (L,mid,pos*2); Build_tree (Mid+1, r,pos*2+1); Tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;}voidUpdateintLintRintChangeintPOS) { if(tree[pos].l==l&&tree[pos].r==r) {Tree[pos].lazy+=Change ; Tree[pos].num+ = (tree[pos].r-tree[pos].l+1)*Change ; return; } if(tree[pos].lazy) {Tree[pos*2].num+= (tree[pos*2].r+1-tree[pos*2].L) *Tree[pos].lazy; Tree[pos*2+1].num+= (tree[pos*2+1].r+1-tree[pos*2+1].L) *Tree[pos].lazy; Tree[pos*2].lazy+=Tree[pos].lazy; Tree[pos*2+1].lazy+=Tree[pos].lazy; Tree[pos].lazy=0; } intMid= (TREE[POS].L+TREE[POS].R)/2; if(r<=mid) Update (L,r,change,pos*2); Else if(l>mid) Update (L,r,change,pos*2+1); Else{update (L,mid,change,pos*2); Update (Mid+1, r,change,pos*2+1); } tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;} ll query (intLintRintPOS) { //cout<<l<< "" <<r<< "" <<pos<<endl; if(tree[pos].l==l&&tree[pos].r==R)returnTree[pos].num; if(tree[pos].lazy) {Tree[pos*2].num+= (tree[pos*2].r+1-tree[pos*2].L) *Tree[pos].lazy; Tree[pos*2+1].num+= (tree[pos*2+1].r+1-tree[pos*2+1].L) *Tree[pos].lazy; Tree[pos*2].lazy+=Tree[pos].lazy; Tree[pos*2+1].lazy+=Tree[pos].lazy; Tree[pos].lazy=0; } intMid= (TREE[POS].L+TREE[POS].R)/2; if(l>mid)returnQuery (l,r,pos*2+1); Else if(r<=mid)returnQuery (l,r,pos*2); Else returnQuery (l,mid,pos*2) +query (mid+1, r,pos*2+1);}intMain () {intx,q,i,t; while(~SCANF ("%d",&x) {Build_tree (1X1); scanf ("%d",&P); while(q--) { intFlag,change,l,r; scanf ("%d%d%d",&flag,&l,&R); if(flag==1) {scanf ("%d",&Change ); Update (L,r,change,1); } Elseprintf ("%lld\n", Query (L,r,1)); } } return 0;}View Code
Codevs 1082 Segment Tree Exercise 3 interval update + delay tag