题目链接: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. virtualbox+vagrant学习-2(command cli)-5-vagrant halt命令

    Halt 格式: vagrant halt [options] [name|id] 该命令关闭vagrant管理的正在运行的机器. userdeMacBook-Pro:~ user$ vagrant ...

  2. 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数。定义 Gymnastics 类和 School 类,它们都是 ComputerAverage 的子类。Gymnastics 类中计算选手的平均成绩的方法是去掉一个最低分,去掉一个最高分,然后求平均分;School 中计算平均分的方法是所有科目的分数之和除以总科目数。 要求:定义ComputerAv

    题目: 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数. 定义 Gymnastics 类和 School 类,它们都是 ComputerAverag ...

  3. vue2 broadcast和dispatch的理解

    /* broadcast 事件广播 @param {componentName} 组件名称 @param {eventName} 事件名 @param {params} 参数 遍历寻找所有子孙组件,假 ...

  4. zabbix 监控机器监听的端口 + 触发器 表达式理解

    在zabbix web 页面配置item,监控监听的21端口 配置trigger 参考:http://www.cnblogs.com/saneri/p/6126786.html 5. {www.zab ...

  5. 视频直播时的QoS策略

    一.如何判断当前的网络状况 可以以发送一帧视频数据的时间为依据,判断当前网络拥塞情况.    网络中出现丢包和抖动,导致接收端接收数据超时,会激发发送端数据重传,重传机制本身挤占网络带宽,导致send ...

  6. RadioButtonFor值为false.默认选中的问题

    (自己看了下.图片有点宽.显示的不全.可以右键新标签查看) 作为一个新手.今天又开始了mvc的学习之旅.然而学习过程中又遇到了一个奇妙的问题.... 一切按部就班到了这里.注册界面. 一眼看上去就不对 ...

  7. Hibernate的应用与注解开发

    Hibernate注解可以帮助我们大大简化hbm映射文件的配置,学习记录之. 先看示例: 1 package com.webShop.domain; 2 import java.io.Serializ ...

  8. activiti基础环境搭建创建数据库表及策略

    博主使用为activiti5.22的版本. 1.创建maven工程. 2.在pom文件中引入所需要的包,如:activiti包.数据库包. 这是我引用的包: <dependencies> ...

  9. mysql/mariadb学习记录——查询3(AVG、SUM、COUNT)函数

    AVG() 求平均数函数: //求emp表中的sal属性的平均值 mysql> select avg(sal) as salAverage from emp; +-------------+ | ...

  10. python 继承与多重继承

    当然,如果不支持python继承,语言特性就不值得称为“类”.派生类定义的语法如下所示: <statement-1> . . . <statement-N> 名称 BaseCl ...