A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4494 Accepted Submission(s): 1384

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

比赛的时候没有注意到k的值很小果断超时.

更新区间(a,b)中(i-a)%k==0的点其实就是更新区间(a,b)中i%k==a%k的值,所以可以用树状数组实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX = 55000; int FK[MAX][11][11]; int num[MAX]; int n,m; int lowbit(int x)
{
return x&(-x);
}
void update(int x,int k,int mod,int va)//更新摸为k,取模后为mod的区间的数组
{
while(x>0)
{
FK[x][k][mod]+=va;
x-=lowbit(x);
}
}
int Query(int x,int a)//查询a所在的区间的增加值
{
int s=0;
while(x<MAX)
{
for(int i=1;i<=10;i++)
{
s+=FK[x][i][a%i];
}
x+=lowbit(x);
}
return s;
} int main()
{
int flag,a,b,k,va;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
scanf("%d",&m);
memset(FK,0,sizeof(FK));
for(int i=1;i<=m;i++)
{
scanf("%d",&flag);
if(flag==1)
{
scanf("%d %d %d %d",&a,&b,&k,&va);
update(b,k,a%k,va);
update(a-1,k,a%k,-va);
}
else
{
scanf("%d",&a);
printf("%d\n",Query(a,a)+num[a]);
}
}
}
return 0;
}

A Simple Problem with Integers(树状数组HDU4267)的更多相关文章

  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 == ...

  2. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

    今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...

  3. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  4. A Simple Problem with Integers_树状数组

    Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...

  5. luogu 2519 [HAOI2011]problem a 动态规划+树状数组

    发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...

  6. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  8. A Simple Problem with Integers(100棵树状数组)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   ...

随机推荐

  1. tomcat deploy部署项目三种方法

    1.将应用文件夹或war文件直接copy到tomcat的webapps目录下,这样tomcat启动的时候会将webapps目录下的文件夹或war文件的内容当成应用部署.这种方式最简单且无须书写任何配置 ...

  2. 【Origin】 画道

    -夏之时,晚归途中,观云,感雨,识迷障,题以记之. 原道是东边日出西边雨, 却原来乌云蔽日雨不息; 只不过光芒也破云边洒, 可还是满地泥泞水花起. --作于二零一六年七月十一日

  3. Java基础(31):String的大小写转换、分离成数组、==与equals()的区别(String类)

    继续来看 String 类常用的方法,如下代码所示: 运行结果: 那么,“==” 和 equals() 有什么区别呢? ==: 判断两个字符串在内存中首地址是否相同,即判断是否是同一个字符串对象 eq ...

  4. css样式重写

    //我们经常想修改插件的某一个或几个样式特性,并保留其它的样式.而不是把某个css全部重写一遍. /*原有样式*/.ninew {padding: 0 10px;width: 600px;height ...

  5. 一、Java基础--01

    Java基础测试题分析 第一题是关于基本的算法知识,这个很有必要去掌握以下,在学校也经常听老师们说找工作比试面试会出一些这方面的知识,我拿到的第一题是关于排序的,虽然很简单,但是我还是要提醒一下基础不 ...

  6. 原生js获取execl里面的值 主要使用ActiveXObject

    今天一个程序员给了一个excel表,里面有一百多条数据,叫我一个一个数据的复制到系相应的函数里面比如 put("gaga1","gaga2"),这样一句话,要我 ...

  7. js正则函数match、exec、test、search、replace、split使用介绍集合

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  8. 五分钟打造自己的sql性能分析工具

    1.首先要有一个trace文件 2. 打开trace文件 3. 另存为跟踪表 4.登录你要保存到的目标sqlserver服务器 5. 选择要保存的数据库和表名称 6. 保存完成(左下角出现进度直到显示 ...

  9. 【secureCRT】会话保存的路径

  10. MAC常用快捷键

    1.F11 用于当前界面与桌面进行切换,Command-M 最小化窗口,Option-Command-M 最小化所有窗口,Command-数字 切换数字指定的网页列表 Shift-Command-D ...