-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.cpp
More file actions
103 lines (89 loc) · 2.77 KB
/
SegmentTree.cpp
File metadata and controls
103 lines (89 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<sstream>
#include<iostream>
using namespace std;
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define memo(a,v) memset(a,v,sizeof(a))
#define PI 2*acos(0.0)
#define pb push_back
#define all(a) a.begin(),a.end()
#define EPS 1e-8
#define i64 long long
#define MAX 100005
#define QUERY 1
#define UPDATE 2
int A[MAX<<2];
int lazy[MAX<<2],input[MAX];
void update(int lf,int rt,int idx,int x,int y,int val){
if(lf>=x && rt<=y){
lazy[idx] += val;
A[idx]+= (rt - lf + 1)*val;
return;
}
int mid = (lf + rt) /2;
if(x<=mid) // some part of the update range is in the left side
update(lf,mid,2*idx,x,y,val);
if(y>mid) // some part of the update range is in the right side
update(mid+1,rt,2*idx+1,x,y,val);
A[idx] = A[2*idx] + A[2*idx+1]; // update the value of the current node from its children
}
int query(int lf,int rt,int idx,int x,int y){
if(lf>=x && rt<=y){
return A[idx];
}
int mid = (lf + rt) / 2 ,ret = 0;
if(lazy[idx]){ // have to propagate the lazy value to the children
lazy[2*idx] += lazy[idx]; // pass lazy to children
lazy[2*idx+1] += lazy[idx];
A[2*idx] += (mid - lf +1)*lazy[idx]; // adjust the original sum of children
A[2*idx+1] += (rt - mid) * lazy[idx];
lazy[idx] = 0; // reset the lazy value of current node
}
if(x<=mid) // some part of the query range is in the left side
ret+=query(lf,mid,2*idx,x,y);
if(y>mid) // some part of the query range is in the right side
ret+=query(mid+1,rt,2*idx+1,x,y);
return ret;
}
void build(int lf,int rt,int idx){
lazy[idx] = 0; // initialize lazy vlue to 0
if(lf==rt){
A[idx] = input[lf];
return ;
}
int mid = (lf + rt) /2;
build(lf,mid,2*idx); // call left child
build(mid+1,rt,2*idx + 1); // call right child
A[idx] = A[2*idx] + A[2*idx+1]; // answer for current node is the sum of its children value
}
int main()
{
int n,i,q,type,x,y,val;
scanf("%d",&n); // number of integer in total
for(i = 0;i<n;i++)
scanf("%d",&input[i]);
build(0,n-1,1); // build the tree, root node is in index 1
scanf("%d",&q); // number of query
for(i = 0;i<q;i++){
scanf("%d",&type); // which type , query or update, QUERY = 1, UPDATE = 2
if(type == QUERY){
scanf("%d %d",&x,&y); // print the sum from index x to y
printf("%d\n",query(0,n-1,1,x,y));
}
else if(type == UPDATE){
scanf("%d %d %d",&x,&y,&val); // add val to each index from x to y
update(0,n-1,1,x,y,val);
}
}
return 0;
}