Luo Gu P2068 statistics and problem solution

Title Description

Given a length of n (n <= 100000), the initial values ​​are 0 as a sequence of numbers on the modified x (x <= 10000) certain locations times, each time adding a number, and submit y (y <= 10000) issue, and seek each segment interval. One second time limit.

Input Format

The first line number 1, n denotes the length of the sequence

Second line number 1, w denotes the number of operations

W is sequentially behind the line, respectively and added interrogate operation

Thereto, denoted by x, y represented by the interrogation

X is the format "xa b" represents a plus b at a position of the sequence

y is in the format of "ya b" indicates a query and adding the interval b

Output Format

One per line number, which are the result of each inquiry

Sample input and output

Input # 1
5 
4 
x 8 March 
y 1 3 
x April 9 
and April 3
Output # 1
8
17

Resolution:

 

Template title
Fenwick tree template
support monotonous modify, query interval

The bar code

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<iomanip>
 8 #include<cstdlib>
 9 #include<queue>
10 #include<set>
11 #include<map>
12 #include<stack>
13 #include<vector>
14 #define re register
15 #define Max 210000
16 #define D double
17 #define gc getchar
18 inline int read()
19 {
20     int a=0;int f=0;char p=gc();
21     while(!isdigit(p)){f|=p=='-';p=gc();}
22     while(isdigit(p)){a=a*10+p-'0';p=gc();}
23     return f?-a:a;
24 }
25 int c[Max]={0},n,m;char ch;
26 int lowbit(int x)
27 {
28     return x&-x;
29 }
30 void add(int x,int k)
31 {
32     for(;x<=m;x+=lowbit(x)) c[x]+=k;
33 }
34 int query(int x)
35 {
36     int ans=0;
37     for(;x;x-=lowbit(x)) ans+=c[x];
38     return ans;
39 }
40 int main()
41 {
42     m=read();n=read();int l,r;
43     for(re int i = 1 ; i <= n ; ++ i) {
44         std::cin>>ch;l=read(),r=read();
45         if(ch=='x') add(l,r);
46         if(ch=='y') printf("%d\n",query(r)-query(l-1));
47     }
48     return 0;
49 }
AC Code

 

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11295538.html