242. 一个简单的整数问题

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
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+1;
ll a[maxn],tree[maxn],n,m,l,r,k;
char op[2];
ll lowbit(ll x)
{
return x&-x;
}
void add(ll x,ll k)
{
while(x<=n)
{
tree[x]+=k;
x+=lowbit(x);
}
}
ll getsum(ll x)
{
ll res=0;
while(x)
{
res+=tree[x];
x-=lowbit(x);
}
return res;
}
int main()
{
cin>>n>>m;
for (int i = 1; i <= n; i ++ )
scanf("%lld",a+i);
while(m--)
{
scanf("%s",op);
if(strcmp(op,"Q")==0)
{
scanf("%lld",&l);
printf("%lld\n",a[l]+getsum(l));
}
else if(strcmp(op,"C")==0)
{
scanf("%lld%lld%lld",&l,&r,&k);
add(l,k);add(r+1,-k);
}
}
return 0;
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信