首页 > 代码库 > HDU 4267 A Simple Problem with Integers(树状数组区间更新)
HDU 4267 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5402 Accepted Submission(s): 1710
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 1142 12 22 32 41 2 3 1 22 1 2 22 32 41 1 4 2 12 12 22 32 4
Sample Output
111113312341
Source
2012 ACM/ICPC Asia Regional Changchun Online
Recommend
liuyiding
/*类似的区间更新,但这个区间不是实际意义上的区间而是,i到j满足条件的点更新这个题和区间更新类似,用另一个数组维护,满足条件的点的前缀和,询问的时候直接用原数组的值加上满足条件的值*/#include<iostream>#include<stdio.h>#include<string.h>#define lowbit(x) x&(-x)#define N 50010using namespace std;int c[12][12][N];//int n,q,op;void update(int s1,int s2,int x,int val)//间隔为s1,起点为s2,需要更新的点为x{ while(x<=n) { c[s1][s2][x]+=val; x+=lowbit(x); }}int getsum(int s1,int s2,int x){ int s=0; while(x>0) { s+=c[s1][s2][x]; x-=lowbit(x); } return s;}int num[N];int main(){ //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { //cout<<n<<endl; memset(c,0,sizeof c); for(int i=0;i<n;i++) scanf("%d",&num[i]); scanf("%d",&q); int x,y,k,val; while(q--) { scanf("%d",&op); if(op==1) { scanf("%d%d%d%d",&x,&y,&k,&val); x--; y--; int knum=(y-x)/k;//需要更新的点的个数 int s1=x%k;//这次更新的起点 update(k,s1,x/k+1,val); update(k,s1,x/k+knum+2,-val); } else if(op==2) { scanf("%d",&x); x--; int cur=num[x]; for(int i=1;i<=10;i++)//遍历的是k的取值 cur+=getsum(i,x%i,x/i+1); printf("%d\n",cur); } } } return 0;}
HDU 4267 A Simple Problem with Integers(树状数组区间更新)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。