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 ...
随机推荐
- TensorFlow函数(六)初始值生成函数
1.常量生成函数 tf.constant(value, dtype) 生成一个初始值为常量value的数组 value:指定的常量 dtype:数据类型 tf.zeros(shape, dtype) ...
- regex_replace
Regex_iterator方法需要输入一个正则表达式,以及一个用于替换匹配的字符串的格式化字符串:这个格式化的字符串可以通过表的转义序列引用匹配子字符串的部分内容: 转义序列 $n 替换第n个捕获的 ...
- Android攻城狮学习笔记—入门篇三
第十章 CheckBox 与其他控件类似 有自己的监听方法 实现监听 并定义被选中或取消后的操作 第十一章 RadioGroup和RadioButton RadioGroup是RadioButton的 ...
- P1412 经营与开发
题目描述 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以“EX”为开头的英语单词. eXplore(探索) eXpand(拓张与发展) eXploit(经营与开发) ...
- apue2e unp安装
最近在读 Richard Stevens 的大作<UNIX环境高级编程>,相信很多初读此书的人都会与我一样遇到这个问题,编译书中的程序实例时会出现问题,提示 “错误:apue.h:没有那个 ...
- #leetcode刷题之路49-字母异位词分组
给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串.示例:输入: ["eat", "tea", "tan" ...
- 有关ajax
1.什么是ajax? ajax是前端与后端交互所依赖的一项技术,它相当于一座桥梁,沟通了前端与后端. 2.ajax的优点 可以局部更新网页内容. 3.ajax的本质就是xmlHttpRequest对象 ...
- 基于FPGA的DDS设计(一)
最近在学习基于FPGA的DDS设计,借此机会把学习过程记录下来,当作自己的学习笔记也希望能够帮助到学习DDS的小伙伴. DDS(Direct Digital Synthesizer)直接数字合成器,这 ...
- Docker:unauthorized: incorrect username or password.
用VS2017编译DockerCompose项目,显示错误:unauthorized: incorrect username or password. 打开命令行工具,输入docker login命令 ...
- Catalan&Stirling数
Catalan&Stirling数 Tags:数学 作业部落 评论地址 Catalan数 \(1,1,2,5,14,42,132,429,1430,4862,16796,58786...\) ...