题目链接:http://poj.org/problem?id=3468

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 144616   Accepted: 44933
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

Source

 
分析:
今天刚接触分块的思想。对于这道题,我们将所给序列分成一个一个大小为根号n的块,每次询问时在线处理:
额外用到的数组:
pos[maxn]:pos[i]表示第i个元素属于哪个块。
add[maxn]:add[i]表示第i个块的增量。
sum[maxn]:sum[i]表示第i个块的总和。
L[maxn]:L[i]表示第i个块的左端点。
R[maxn]:R[i]表示第i个块的右端点。
一·询问操作  Q l r :
1.如果l和r都在一个块内,直接利用朴素算法从a[l]加到a[r]。
2.如果不一样,我们令p=pos[l],q=pos[r],l和r之间完整的块为从p+1到q-1,所以我们累加sum[i]+add*(R[i]-L[i]+1),i从p+1到q-1。
之后我们对于不完整的两端进行朴素处理。
二·改变操作 C l r val:
1.如果l和r都在一个块内,直接利用朴素算法从a[l]改变到a[r]。
2.如果不一样,我们令p=pos[l],q=pos[r],l和r之间完整的块为从p+1到q-1,所以我们改变add[i]+=val,i从p+1到q-1。
之后我们对于不完整的两端进行朴素处理。
总复杂度为O((N+M)*√N)
//参考《算法竞赛进阶指南》
#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(分块入门)的更多相关文章

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

  2. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  3. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  4. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

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

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

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

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

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

随机推荐

  1. 了解python wed 框架

    随着人工智能发展,python这门编程语言也渐渐被人们熟知.至于python为什么能AL的时代脱颖而出可以看一下旁边的网址了解一下https://blog.csdn.net/lixingshi/art ...

  2. PAT乙级1006

    1006 换个格式输出整数 (15 分)   让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正 ...

  3. HDU 1171 (01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 分析: 例如数据 3 10    2 20    1 30    1 获得这样一个降序的数组: ...

  4. select * from * with ur

    DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级别来隔离数据.隔离级是影响加锁策略的重要环节,它直接影响加锁的范围及锁的持续时间.两个应用程序即使执行的相同的操作,也可 ...

  5. delphi7 TRichView 安装

    下载: 链接: https://pan.baidu.com/s/1gfMYeGF 密码: 45bn 打开目录:E:\Delphi7\TRichView.v.16.10.3 ScaleRichView. ...

  6. django的Session-10

    目录 配置储存引擎 存储在sql数据库 储存在缓存 储存在本地文件 储存在redis session操作 django需要使用一个中间价来实现 session功能, 一般情况下默认启用了该中间价 ,可 ...

  7. angular自定义过滤器在页面和控制器中的使用

    首先设置自定义过滤器. 定义模块名:angular .module('myApp') .filter('filterName',function(){ return function(要过滤的对象,参 ...

  8. [转]Kafka 设计与原理详解

    一.Kafka简介 本文综合了我之前写的kafka相关文章,可作为一个全面了解学习kafka的培训学习资料. 1 2 1 2 转载请注明出处 : 本文链接 1.1 背景历史 当今社会各种应用系统诸如商 ...

  9. WP10的一点小问题

    兼容WP8.0/WP7.5不太完整!也许是测试版的问题.毕竟还没发布正式版! 具体如:WP8.0或WP7.5的启动器!就是选择图片的启动器!调用后对话框返回的结果都是Cancel本来应该是OK的.也就 ...

  10. 【转载】COM小结

    原文:http://blog.csdn.net/byxdaz/article/details/6595210 一.Com概念 所谓COM(Componet Object Model,组件对象模型),是 ...