A Simple Problem with Integers_树状数组
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)
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
1
1
1
1
3
3
1
2
3
4
1
【题意】给出n个数,再给出m个操作
被这题虐哭,(毕竟太弱了~~)关键就是建立多个树状数组,然而我对树状数组理解还是不行啊!
sum[x][k][x%k]代表x对k取余的值,然后每次更新树状数组的时候只需要更新update(a,.....) 与update(b+1,.....);
参考资料:http://blog.csdn.net/yeguxin/article/details/47999833
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- using namespace std;
- const int N=+;
- int aa[N];
- int n,m;
- int sum[N][][];//开稍大一点就会MLE
- int lowbit(int x)
- {
- return x&(-x);
- }
- void update(int x,int k,int mod,int v)
- {
- while(x<=n)
- {
- sum[x][k][mod]+=v;
- x+=lowbit(x);
- }
- }
- int query(int x,int y)
- {
- int res=;
- while(x)
- {
- for(int i=;i<=;i++)
- {
- res+=sum[x][i][y%i];
- }
- x-=lowbit(x);
- }
- return res;
- }
- int main()
- {
- while(~scanf("%d",&n))
- {
- memset(sum,,sizeof(sum));
- for(int i=;i<=n;i++)
- {
- scanf("%d",&aa[i]);
- }
- scanf("%d",&m);
- int op,a,b,k,c;
- while(m--)
- {
- scanf("%d",&op);
- if(op==)
- {
- scanf("%d",&k);
- int ans=query(k,k);
- printf("%d\n",ans+aa[k]);
- }
- else if(op==)
- {
- scanf("%d%d%d%d",&a,&b,&k,&c);
- int kk=(b-a)/k;
- update(a,k,a%k,c);
- update(b+,k,a%k,-c);
- }
- }
- }
- return ;
- }
A Simple Problem with Integers_树状数组的更多相关文章
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- POJ3468 A Simple Problem with Interger [树状数组,差分]
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- luogu 2519 [HAOI2011]problem a 动态规划+树状数组
发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...
- A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- A Simple Problem with Integers(100棵树状数组)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97217 ...
随机推荐
- First commit
今天是2016年11月14日.天气晴. 第一篇博客,准备在这里记录下我学到的技术,希望能够坚持下来.^.^
- windows php swoole 安装
Cygwin 官方地址:http://www.cygwin.com/ swoole 官方下载地址:https://github.com/swoole/swoole-src/releases 1.下载 ...
- Bootstrap 表单
表单布局 Bootstrap 提供了下列类型的表单布局: 垂直表单(默认) 内联表单 水平表单 垂直或基本表单 基本的表单结构是 Bootstrap 自带的,个别的表单控件自动接收一些全局样式.下面列 ...
- IOS常用框架
IOS开发中有用的第三方库 #Objective-C中最受瞩目库 [链接](https://github.com/languages/Objective-C/most_watched) * [th ...
- XML 详解
import urllib import requests from xml.etree import ElementTree as ET root = ET.XML(open('D:\E\seman ...
- Android Token的使用学习
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- 深入理解JQuery插件开发
如果你看到这篇文章,我确信你毫无疑问会认为jQuery是一个使用简便的库.jQuery可能使用起来很简单,但是它仍然有一些奇怪的地方,对它基本功能和概念不熟悉的人可能会难以掌握.但是不用担心,我下面已 ...
- Python Day 01
What is variables? 一段命名的内存空间 变量即在程序运行过程中,它的值是允许改变的量 1.变量命名: 合法: 显式.通俗易懂. nums_of_jay_gf = 19 NumsOfJ ...
- 嵌入式 python异常except语句用法与引发异常 zz
http://blog.sina.com.cn/s/blog_8795b0970101dj0a.html
- 第九篇.bootstrap导航
创建一个标签式的导航菜单的步骤是: 在ul标签上加上class nav 再ul标签上加上 class .nav-tabs. 在li标签上加上 active表示激活该项 <ul class=&q ...