HDU 4267 A Simple Problem with Integers(树状数组区间更新)
2016-09-20 19:06
369 查看
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
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
Recommend liuyiding
/* 类似的区间更新,但这个区间不是实际意义上的区间而是,i到j满足条件的点更新 这个题和区间更新类似,用另一个数组维护,满足条件的点的前缀和,询问的时候直接用原数组的值加上满足条件的值 */ #include<iostream> #include<stdio.h> #include<string.h> #define lowbit(x) x&(-x) #define N 50010 using namespace std; int c[12][12] ;// 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 ; 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; }
相关文章推荐
- 【JZOJ 4783】Osu
- Linux 平台七大桌面环境通览
- (二):模块相关的基础概念
- httpPost对JSON发送和接收
- 【LeetCode-Algorithm】【7-Reverse Integer】【Python】
- 第四周项目5-循环双链表应用
- maven通俗易懂的解释
- 以操作系统的角度述说线程与进程
- geotrellis使用(二十二)实时获取点状目标对应的栅格数据值
- activeMQ 安装于使用
- JSP动作元素1
- 使用adb抓取安卓设备的log
- 扩大eclipse的运行内存,扩大tomcat的内存
- Hibernate 单项级联操作
- kettle及数据库导数_20160920
- MTK elian(smartlink)在WIN32下的实现
- libc.so.6被删后导致系统无法使用的原因及解决方法
- poj2778DNA Sequence(AC自动机)
- 在jq easyui 中添加本地搜索功能
- 关于递归- -纪念