模板:树状数组

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
#include<iostream>
#include<string.h>
using namespace std;

int num[1000010],N = 0,q = 0;

inline int lowbit(int x)
{
return (x) & (-x);
}

inline void add(int x,int k)
{
while(x<=N)
{
num[x]+=k;
x+=lowbit(x);
}
}

inline long long sum(int x)
{
long long ans = 0;
for(;x;x-=lowbit(x))
ans+=num[x];
return ans;
}

int main(void)
{
cin >> N >> q;
while(q--)
{
char qwq;
cin >> qwq;

if(qwq == 'x')
{
int x,y;
cin >> x >> y;
add(x,y);
}
if(qwq == 'y')
{
int x,y;
cin >> x >> y;
cout << sum(y) - sum(x-1) << endl;
}
}

return 0;
}
本站文章除注明转载外均为原创,未经允许不要转载哇. ヾ(゚ー゚ヾ) http://chicago01.top/2019/05/10/mo-ban-shu-zhuang-shu-zu/index.html
Compartir