POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 92632 | Accepted: 28818 | |
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
线段树的区间更新,区间求和裸题,感觉线段树比树状数组好理解……先照着别的的模版敲一遍再理解,线段树的写法很多人都存在差异,改成自己的比较好用
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) (x<<1)|1
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
LL l,r,mid;
LL sum;
LL add;
}T[N<<2];
LL arr[N];
void pushup(int k)
{
T[k].sum=T[LC(k)].sum+T[RC(k)].sum;
}
void pushdown(int k)
{
T[LC(k)].add+=T[k].add;
T[LC(k)].sum+=T[k].add*(T[LC(k)].r-T[LC(k)].l+1);
T[RC(k)].add+=T[k].add;
T[RC(k)].sum+=T[k].add*(T[RC(k)].r-T[RC(k)].l+1);
T[k].add=0;
}
void build(int k,LL l,LL r)
{
T[k].l=l;
T[k].r=r;
T[k].add=0;
T[k].mid=(T[k].l+T[k].r)>>1;
if(l==r)
T[k].sum=arr[l];
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(LL l,LL r,LL val,int k)
{
if(r<T[k].l||l>T[k].r)
return ;
if(l<=T[k].l&&r>=T[k].r)
{
T[k].add+=val;
T[k].sum+=val*(T[k].r-T[k].l+1);
}
else
{
if(T[k].add)
pushdown(k);
update(l,r,val,LC(k));
update(l,r,val,RC(k));
pushup(k);
}
}
LL query(int k,LL l,LL r)
{
if(l<=T[k].l&&r>=T[k].r)
return T[k].sum;
if(T[k].add)
pushdown(k);
if(r<=T[k].mid)
return query(LC(k),l,r);
else if(l>T[k].mid)
return query(RC(k),l,r);
else
return query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r);
}
int main(void)
{
int tcase,n,i,m;
LL l,r,val;
char ops[3];
while (~scanf("%d%d",&n,&m))
{
MM(arr,0);
for (i=1; i<=n; i++)
scanf("%lld",&arr[i]);
build(1,1,n);
for (i=0; i<m; i++)
{
scanf("%s",ops);
if(ops[0]=='Q')
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",query(1,l,r));
}
else
{
scanf("%lld%lld%lld",&l,&r,&val);
update(l,r,val,1);
}
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case 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 ...
随机推荐
- Codeforces 424C(异或)
Magic Formulas Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Subm ...
- 矩阵按键的试验---verilog
矩阵键盘的试验,采用三段式状态机处理方法. 难点在于检测状态机中:按键消抖以后逐列检测. 电路图: 代码 /********************************Copyright***** ...
- SecureCRT乱码
http://jingyan.baidu.com/article/948f59245be128d80ff5f9aa.html
- 对线程调度中Thread.sleep(0)的深入理解
在Java或者C#中,都会用到 Thread.Sleep()来使线程挂起一段时间.那么你有没有正确的理解这个方法的用法呢?思考下面这两个问题: 1.假设现在是 2014-8-13 17:00:00.0 ...
- 会员制实现C2B定制有机农产品,被中粮我买投资的良食网这样卖有机生鲜
前几天,中粮我买网战略投资了位于深圳的有机生鲜自营平台良食网,宣布双方将会在供应链上展开合作.然而良食网对大家来说还是比较陌生的,为此36氪专访了良食网的创始人唐忠. 良食网成立于2011年,是一家以 ...
- android 拨号
public class CallActivity extends Activity { @Override public void onCreate(Bundle savedInstanceStat ...
- C++程序设计课程学习的网址
很详细 C++程序设计课程主页 http://blog.csdn.net/sxhelijian/article/details/7910565 孙鑫C++视频教程 rmvb格式 全20CD完整版 ...
- WITH AS短语,也叫做子查询部分(subquery factoring)
可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到. 作为提供数据的部分. 代码例子: with temp as (select ID, Type_Name, Type_ID ...
- MATLAB学习笔记(九)——MATLAB符号计算
(一)符号对象 一.建立符号对象 1.建立符号变量和符号常量(sym,syms): 只可以建立一个符号变量 可以一次性建立多个符号变量 PS:符号常量计算的结果是精确的数学表达式,而数值常量是进行约分 ...
- MySQL监控系统MySQL MTOP的搭建
MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统.系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理.最重要是MySQL服务器无需安装任何Agent,只 ...