题目链接: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. 微信小程序的开发(一)

    我现在在学习,微信小程序开发,刚刚看看一篇对我特别有用的博客文章,我就把摘抄过来了,好好的学习一下. 序言 开始开发应用号之前,先看看官方公布的「小程序」教程吧!(以下内容来自微信官方公布的「小程序」 ...

  2. vue2.* 事件结合双向数据绑定、模块化以及封装Storage实现todolist 待办事项 已经完成 和进行中持久化 06

    ceshi.vue <template> <div id="app"> <input type='text' v-model='todo' @keyd ...

  3. sort与sorted

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1.list.sort()方法仅被定义在list中,相反地so ...

  4. Selenium 设置代理chrome

    1.1. 连接无用户名密码认证的代理 from selenium import webdriver chromeOptions = webdriver.ChromeOptions() chromeOp ...

  5. 统计单词出现的最多次数(Trie树)

    A Time Limit: 60ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 给出n(1<= n && n <= 2*10^6) ...

  6. SharePoint Search 分词(WordBreaker)

    [问题] 我们经常会搜索一个很长的词,例如“国際協力銀行” ,对应的搜索结果中会有关于“国際”“協力”“銀行”相关内如都会搜索到,这样的行为就是分词,那如何知道搜索内如是如何分词的呢? [解决办法] ...

  7. SQL语句查询关键字中含有特殊符号怎么处理, 例如 'SMI_'

    SQL语句查询关键字中含有特殊符号怎么处理, 例如 'SMI_' 错误:select * from emp  where ename like '%SML_%' 正确:select * from em ...

  8. BZOJ2595: [Wc2008]游览计划(斯坦纳树,状压DP)

    Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 2030  Solved: 986[Submit][Status][ ...

  9. PHP 抓取函数curl 实践

    最近在学习curl的抓取实践, 在里面也学到了一些东西. 有一些网站需要cookie才可以抓取成功.这个时候我们就可以通过fiddle4 去抓包实现.然后通过构建头部信息 ,绕过网站端的验证. 以下是 ...

  10. SEO优化上首页之搜索引擎原理简要

    搜索引擎(Search Engine)是特定的计算机程序,它根据一定的策略.从互联网上搜集信息,对信息进行处理后,为用户提供检索服务,并将用户结果展示给用户. 搜索引擎优化(Search Engine ...