HDU-4267-A Simple Problem with Integers-(树状数组)
来源:互联网 发布:光伏数据采集器接法 编辑:程序博客网 时间:2024/06/05 11:04
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
树状数组很有趣的一种做法
代码:
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long ll;#define M 50005int tree[M][11][11];int p[M];int n;inline int lowbit(int i){ return i&(-i);}void update(int x,int k,int mod,int v){ while(x<=n) { tree[x][k][mod]+=v; x+=lowbit(x); }}int query(int x,int y){ int ret=0,i; while(x>0) { for(i=1;i<=10;i++) { ret+=tree[x][i][y%i]; } x-=lowbit(x); } return ret;}int main(){ int q,i,op,a,b,k,c; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&p[i]); memset(tree,0,sizeof(tree)); scanf("%d",&q); while(q--) { scanf("%d",&op); if(op==2) { scanf("%d",&a); int ans=query(a,a); printf("%d\n",p[a]+ans); } else { scanf("%d%d%d%d",&a,&b,&k,&c); update(a,k,a%k,c); update(b+1,k,a%k,-c); } } } return 0;}
阅读全文
0 0
- HDU 4267 A Simple Problem with Integers(树状数组)
- HDU-4267-A Simple Problem with Integers-(树状数组)
- HDU 4267 A Simple Problem with Integers 树状数组
- hdu(4267)A Simple Problem with Integers(三维树状数组)
- HDU 4267 A Simple Problem with Integers --树状数组
- HDU 4267 A Simple Problem with Integers(树状数组)
- HDU - 4267 A Simple Problem with Integers(树状数组)
- hdu 4267 A Simple Problem with Integers 树状数组
- HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)
- A Simple Problem with Integers+hdu+树状数组
- HDOJ 4267 A Simple Problem with Integers 树状数组
- poj3468--A Simple Problem with Integers(树状数组解法)
- hdu4267--A Simple Problem with Integers(树状数组)
- POJ 3468 A Simple Problem with Integers (树状数组)
- POJ 3468 A Simple Problem with Integers(树状数组)
- A Simple Problem with Integers(线段树-树状数组)
- HDU 4267 A Simple Problem with Integers 数列离散化 + 树状数组
- 【三维树状数组与离散化】HDU 4267——A Simple Problem with Integers
- Leetcode:Maximal Square & Maximal Rectangle
- 记住企业与股票之间总是有差距的
- JDBC数据源(DataSource)的简单实现
- 关于const 修饰变量、引用、指针的说明
- Safe to store list::iterator for later use?
- HDU-4267-A Simple Problem with Integers-(树状数组)
- Redis安装和配置
- 怎样让一段代码在主函数之前执行
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 C.Sum(找规律)
- Java多线程之队列Quene-yellowcong
- pycharm中配置pyqt4环境
- Uva 1153 Keep the Customer Satisfied(贪心)
- HDU 1078 FatMouse and Cheese (dfs + dp记忆化搜索)
- R Notes