A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:
题解:树状数组的区间修改,单点查询,注意这里的不连续区间修改
///1085422276 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<cmath> #include<map> #include<bitset> #include<set> #include<vector> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)); #define TS printf("111111\n"); #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a) scanf("%d",&a) #define g 9.8 #define mod 100000000 #define eps 0.0000001 #define maxn 50000+1 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } //**************************************** int c[maxn][11][11],s[maxn],n; int lowbit(int x){return x&(-x);} void update(int a,int b,int x,int v) { while(x<=n) { c[x][a][b]+=v; x+=lowbit(x); } } int get(int x,int a) { int sum=0; while(x>0) { FOR(i,1,10) sum+=c[x][i][a%i]; x-=lowbit(x); } return sum; } int main() { while(READ(n)!=EOF) { mem(c); FOR(i,1,n) { scanf("%d",&s[i]); } int q=read(); int t,a,b,k,add; FOR(i,1,q) { scanf("%d",&t); if(t==1) { scanf("%d%d%d%d",&a,&b,&k,&add); update(k,a%k,a,add); update(k,a%k,b+1,-add); } else { scanf("%d",&a); printf("%d\n",get(a,a)+s[a]); } } } return 0; }
代码