HDU 4267 A Simple Problem with Integers(线段树)

来源:互联网 发布:58同城小程序源码 编辑:程序博客网 时间:2024/06/12 21:23

A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5447    Accepted Submission(s): 1726


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 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

题意:对区间内所有对k取模余数相同的进行更新,查询某个值。
思路:果断线段树,也想到了延迟更新,但是中间实现的还是没想清楚。主要是因为每次需要取模对余数相同的进行更新,并不是对区间内所有都更新,这个地方比较难想。看了一下别人的题解发现用到了很巧妙的方法,直接空间换时间,建了55棵线段树,每棵线段树对应模k余数相同的,每次更新时都对余数相同的对应的线段树进行更新。

#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;const int MAX=50010;int sum[55][MAX<<2];//线段树int cot[MAX<<2];//是否更新 int lo[15];//记录位置int re[MAX];#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define delf int mid=(l+r)>>1void build(int l,int r,int rt){cot[rt]=0;for(int i=0;i<55;i++)sum[i][rt]=0;if(l==r){return;}int mid=(l+r)>>1;build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);return;}void update(int L,int R,int k,int c,int l,int r,int rt){if(L<=l&&r<=R){int t=lo[k-1]+L%k;sum[t][rt]+=c;cot[rt]=1; return;}int mid=(l+r)>>1;if(R<=mid){update(L,R,k,c,l,mid,rt<<1);}else if(L>mid){update(L,R,k,c,mid+1,r,rt<<1|1);}else{update(L,R,k,c,l,mid,rt<<1);update(L,R,k,c,mid+1,r,rt<<1|1);}return;}void pushdown(int rt){if(cot[rt]==0)return;cot[rt]=0;for(int i=0;i<55;i++){sum[i][rt<<1]+=sum[i][rt];sum[i][rt<<1|1]+=sum[i][rt];sum[i][rt]=0;}cot[rt<<1]=1;cot[rt<<1|1]=1;return;}int query(int l,int r,int rt,int k){if(l==r){int s=0;for(int i=1;i<=10;i++){s+=sum[lo[i-1]+k%i][rt];}return s;}pushdown(rt);int mid=(l+r)>>1;if(mid>=k)return query(l,mid,rt<<1,k);elsereturn query(mid+1,r,rt<<1|1,k);}int main(){//freopen("data.txt","r",stdin);int n;lo[0]=0;lo[1]=1;for(int i=2;i<=10;i++)lo[i]=i+lo[i-1]; while(~scanf("%d",&n)){build(1,n,1); for(int i=1;i<=n;i++)scanf("%d",&re[i]);int m;scanf("%d",&m);while(m--){int q;scanf("%d",&q);if(q==1){int l,r,k,c;scanf("%d %d %d %d",&l,&r,&k,&c);update(l,r,k,c,1,n,1);}else{int k;scanf("%d",&k);int s=re[k];s+=query(1,n,1,k);printf("%d\n",s);}}} return 0;} 
0 0
原创粉丝点击