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)
 
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
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <iomanip>
#include <sstream>
using namespace std;
//#define local
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int C[][][MS]; //C[mod][k][x]
int num[MS]; int lowbit(int x)
{
return x&(-x);
} // 修改区间,单点求职, 树状数组需要逆过来。 void updata(int mod,int k,int x,int d)
{
while(x>)
{
C[mod][k][x]+=d;
x-=lowbit(x);
}
} int getsum(int a,int x)
{
int res=;
while(x<MS) //x<=n
{
for(int k=;k<=;k++)
{
res+=C[a%k][k][x];
}
x+=lowbit(x);
}
return res;
} int main()
{
#ifdef local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // local
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&num[i]);
memset(C,,sizeof(C));
int m;
scanf("%d",&m);
while(m--)
{
int op,a,b,k,c;
scanf("%d",&op);
if(op==)
{
scanf("%d%d%d%d",&a,&b,&k,&c);
updata(a%k,k,b,c);
updata(a%k,k,a-,-c);
}
else
{
scanf("%d",&a);
int ans=getsum(a,a);
printf("%d\n",ans+num[a]);
}
}
}
return ;
}
上一篇:DWZ(JUI) 教程 普通表单提交


下一篇:windows的bat脚本