HDU4267 A Simple Problem with Integers(树状数组 离散化)
2017-08-10 21:02
323 查看
A Simple Problem with Integers
题目链接Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5991 Accepted Submission(s): 1919
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)
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
Source
2012 ACM/ICPC Asia Regional Changchun Online
题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。
思路:区间操作,离散化思想去写。
不来想着用线段树写的,可是会内存超限,看大佬用树状数组写的,开55棵树状数组,用cnt[x][k][mod] 表示x 对k 取余为mod,这样在询问的时候只要循环到10就可以 了。而且(i - a) % k == 0即是 i % k == a % k == mod,这样更新的时候就可以更新(1,b) 和(1,a)了。
随后用线段树写一下再补上。
/* 区间修改树状数组 和离散化 */ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namesp 4000 ace std; const int mm=50010; int cnt[mm][11][11]; int num[mm]; int lowbit(int x) { return x&(-x); } void update(int x,int k,int mod,int add)//对a%K 进行修改存储 { while(x>0) { cnt[x][k][mod]+=add; x-=lowbit(x); } } int sum(int x,int a) { int s=0; while(x<mm) { for(int i=1;i<=10;i++)//k小于等于10 s+=cnt[x][i][a%i]; x+=lowbit(x); } return s; } int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&num[i]); memset(cnt,0,sizeof(cnt)); int m; scanf("%d",&m); while(m--) { int o,a,b,k,c; scanf("%d",&o); if(o==1) { scanf("%d%d%d%d",&a,&b,&k,&c); update(b,k,a%k,c); update(a-1,k,a%k,-1*c);//由于b 算的是 b到0的 需要修改的树状数组树 a到 b 所以要对a前面的树状数组进行修改,以免影响后面的操作 } else { scanf("%d",&a); int ans=sum(a,a); printf("%d\n",ans+num[a]); } } } return 0; }
相关文章推荐
- hdu4267 A Simple Problem with Integers(树状数组区间更新点查询)
- HDU4267 A Simple Problem with Integers(树状数组)
- 【三维树状数组与离散化】HDU 4267——A Simple Problem with Integers
- hdu4267--A Simple Problem with Integers(树状数组)
- HDU 4267 A Simple Problem with Integers(树状数组)
- [POJ 3468] A Simple Problem with Integers [树状数组]
- poj3468--A Simple Problem with Integers(树状数组解法)
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- HDU-4267-A Simple Problem with Integers-(树状数组)
- HDU 4267 A Simple Problem with Integers [树状数组]
- [POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
- hdu 4267 A Simple Problem with Integers(分类别维护多个树状数组)
- POJ 3468 A Simple Problem with Integers (树状数组写法)
- 【G - A Simple Problem with Integers 树状数组 】
- 20140719 「树状数组 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers(树状数组)
- A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267
- POJ 3468_A Simple Problem with Integers(树状数组)
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)