-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBinaryIndexedTree-2.cpp.cpp
More file actions
63 lines (55 loc) · 1.04 KB
/
BinaryIndexedTree-2.cpp.cpp
File metadata and controls
63 lines (55 loc) · 1.04 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
#include<bits/stdc++.h>
using namespace std;
/**
Range updates and Point Queries
1-based indexing everywhere
**/
long long getSum(int BITree[], int n, int index)
{
long long sum = 0;
while (index>0)
{
sum += BITree[index];
index -= index & (-index);
}
return sum;
}
void updateBIT(int *BITree, int n, int index, int val)
{
while (index <= n)
{
BITree[index] += val;
index += index & (-index);
}
}
int *constructBITree(int n)
{
int *BITree = new int[n+10];
for (int i=1; i<=n; i++)
BITree[i] = 0;
return BITree;
}
int main()
{
int n,Q,l,r,pos,x,t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&Q);
int *BITree = constructBITree(n);
while(Q--)
{
scanf("%d%d%d",&l,&r,&x);
l++; r++;
updateBIT(BITree,n,l,x);
updateBIT(BITree,n,r+1,-x);
}
scanf("%d",&Q);
while(Q--)
{
scanf("%d",&pos);
pos++;
printf("%lld\n",getSum(BITree,n,pos));
}
}
return 0;
}