POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 144616 | Accepted: 44933 | |
| Case Time Limit: 2000MS | ||
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
Source
//参考《算法竞赛进阶指南》
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=;
ll a[maxn],sum[maxn],add[maxn];
int L[maxn],R[maxn];
int pos[maxn];
int n,m,t;
void change(int l,int r,ll d)
{
int p = pos[l],q = pos[r];
if(p == q)
{
for(int i=l;i<=r;i++) a[i] += d;
sum[p] += (r-l+)*d;
}
else
{
for(int i=p+;i<=q-;i++) add[i] += d;
for(int i=l;i<=R[p];i++) a[i] += d;
sum[p] += (R[p]-l+)*d;
for(int i=L[q];i<=r;i++) a[i] += d;
sum[q] += (r-L[q]+)*d;
}
}
ll ask(int l,int r)
{
int p = pos[l],q = pos[r];
ll ans = ;
if(p == q)
{
for(int i=l;i<=r;i++) ans += a[i];
ans += add[p]*(r-l+);
}
else
{
for(int i=p+;i<=q-;i++) ans += (sum[i]+add[i]*(R[i]-L[i]+));
for(int i=l;i<=R[p];i++) ans += a[i];
ans += add[p]*(R[p]-l+);
for(int i=L[q];i<=r;i++) ans +=a[i];
ans += add[q]*(r-L[q]+);
}
return ans;
}
int main()
{
int i,j;
char c;
while(cin>>n>>m)
{
memset(sum,,sizeof(sum));
memset(add,,sizeof(add));
for(i=;i<=n;i++) scanf("%lld",&a[i]);
//分块
t = sqrt(n);
for(i=;i<=t;i++)
{
L[i] = (i-)*sqrt(n) + ;
R[i] = i*sqrt(n);
}
if(R[t] < n) t++,L[t] = R[t-] + ,R[t] = n;
//预处理
for(i=;i<=t;i++)
for(j=L[i];j<=R[i];j++)
{
pos[j] = i;
sum[i] += a[j];
}
while(m--)
{
getchar();
scanf("%c",&c);
if(c == 'Q')
{
int x,y;
scanf("%d%d",&x,&y);
cout<<ask(x,y)<<endl;
}
else
{
int l,r;
ll val;
scanf("%d%d%lld",&l,&r,&val);
change(l,r,val);
}
}
}
return ;
}
POJ 3468 A Simple Problem with Integers(分块入门)的更多相关文章
- POJ 3468 A Simple Problem with Integers (分块)
Description You have \(N\) integers, \(A_1, A_2, ... , A_N\). You need to deal with two kinds of ope ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
随机推荐
- 4514: [Sdoi2016]数字配对
Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对 ...
- kubeadm init 时从本地私有仓库下载镜像
#kubeadm init 时从本地私有仓库下载镜像 images=( gcr.io/google_containers/kube-proxy-amd64:v1.6.1 gcr.io/google_c ...
- 20155314 2016-2017-2《Java程序设计》课程总结
20155314 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:刘子健的第一篇博客 预备作业2:刘子健的第二篇博客--有关CCCCC语言(・᷄ᵌ・᷅) ...
- 【转】iOS消息推送实现过程记录
客户端代码:链接地址 服务器代码:链接地址 链接地址 这里记录下iOS消息推送实现的全过程 首先,申请秘钥. 之后进入链接地址开发者,当然你得有啊!!!!! 点击这里 如图: 下面实现创建推送证书( ...
- git +maven+java+jenkins自动化代码持续集成
1.安装JDK JDK下载: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 新 ...
- 移动端适配之sprite雪碧图背景定位
移动端适配一般我会使用rem进行适配,大致操作就是按照一定尺寸设计稿进行制作,最后将所有px值转换为rem,但是一些手机背景精灵图(cssSprite)就会出现一些误差(1px左右),如果公司要求不高 ...
- 安装MySQL56时,停止在start service这一步
问题: 在安装MySQL56时,安装程序执行到start service这一步就不能完成,系统一直提示“安装时间比预期的的长,是否停止安装这一步(configuration of mysql seve ...
- EatWhat 实用的APP
EatWhat最近比较闲开始学习煮粥,买了好多米和豆子,每天煮的都不一样,试了几天发现有的时候记不住要煮什么粥,所以就写了这个APP.每天煮粥的时候拿出来看看就可以了,再也不用思考半天要煮什么粥了. ...
- Dijkstra学习笔记
暂时空白.... 没有前置,我用vector存图 //存储 struct edge{ int w,to;//w是权值,to是连接到的下一条边 }; vector<edge> e; //连边 ...
- ie中input光标问题
为input 添加 readonly=”readonly” UNSELECTABLE="on" 属性. IE中设置了readonly=”readonly”,点击使用日期选择器 ...