A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267
A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4032 Accepted Submission(s): 1255
Problem Description
Let
A1, A2, ... , AN be N elements. You need to deal with two kinds of
operations. One type of operation is to add a given number to a few
numbers in a given interval. The other is to query the value of some
element.
Input
There are a lot of test cases.
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
Output
For each test case, output several lines to answer all query operations.
Sample Input
4
1 1 1 1
14
2 1
2 2
2 3
2 4
1 2 3 1 2
2 1
2 2
2 3
2 4
1 1 4 2 1
2 1
2 2
2 3
2 4
Sample Output
1
1
1
1
1
3
3
1
2
3
4
1
(i-a)%k==0 即 i%k==a%k 分组 x%k==a%k的为一组, 参数 mod, k,x
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <map> 8 #include <string> 9 #include <vector> 10 #include <queue> 11 #include <stack> 12 #include <iomanip> 13 #include <sstream> 14 using namespace std; 15 //#define local 16 typedef long long LL; 17 const int INF=0x4fffffff; 18 const int EXP=1e-5; 19 const int MS=50005; 20 21 int C[11][11][MS]; //C[mod][k][x] 22 int num[MS]; 23 24 int lowbit(int x) 25 { 26 return x&(-x); 27 } 28 29 // 修改区间,单点求职, 树状数组需要逆过来。 30 31 void updata(int mod,int k,int x,int d) 32 { 33 while(x>0) 34 { 35 C[mod][k][x]+=d; 36 x-=lowbit(x); 37 } 38 } 39 40 int getsum(int a,int x) 41 { 42 int res=0; 43 while(x<MS) //x<=n 44 { 45 for(int k=1;k<=10;k++) 46 { 47 res+=C[a%k][k][x]; 48 } 49 x+=lowbit(x); 50 } 51 return res; 52 } 53 54 int main() 55 { 56 #ifdef local 57 freopen("in.txt","r",stdin); 58 freopen("out.txt","w",stdout); 59 #endif // local 60 int n; 61 while(scanf("%d",&n)!=EOF) 62 { 63 for(int i=1;i<=n;i++) 64 scanf("%d",&num[i]); 65 memset(C,0,sizeof(C)); 66 int m; 67 scanf("%d",&m); 68 while(m--) 69 { 70 int op,a,b,k,c; 71 scanf("%d",&op); 72 if(op==1) 73 { 74 scanf("%d%d%d%d",&a,&b,&k,&c); 75 updata(a%k,k,b,c); 76 updata(a%k,k,a-1,-c); 77 } 78 else 79 { 80 scanf("%d",&a); 81 int ans=getsum(a,a); 82 printf("%d\n",ans+num[a]); 83 } 84 } 85 } 86 return 0; 87 }