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 ...
随机推荐
- 【数据结构】hanoi
#include<stdio.h> void hanoi(int n,char x,char y,char z) { ; ) printf("%d. Move disk %d f ...
- 纯css3 加载loading动画特效
最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...
- ShareSDK集成微信、QQ、微博分享
1.前言 为什么要使用第三方的作为集成分享的工具呢?而不去用官方的呢?有什么区别么? 一个字"快",如果你使用官方的得一个个集成他们的SDK,相信这是一个痛苦的过程. 2.准备需要 ...
- 谈谈Objective-C的警告 (转)
原文地址:http://onevcat.com/2013/05/talk-about-warning/ 一个有节操的程序员会在乎自己的代码的警告,就像在乎饭碗边上有只死蟑螂那样. ——@onevcat ...
- Linq学习笔记---Linq to Sql之where
http://kb.cnblogs.com/page/42465/ Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断 ...
- UVa 11995:I Can Guess the Data Structure!(数据结构练习)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- matlab练习程序(多边形顶点凹凸性)
生成简单多边形后,有时还需要对多边形各顶点的凹凸性做判断. 先计算待处理点与相邻点的两个向量,再计算两向量的叉乘,根据求得结果的正负可以判断凹凸性. 结果为负则为凹顶点,为正则为凸顶点. 凹顶点用o表 ...
- TextView属性大全
今天研究了TextView一天了,发现网上有一篇讲TextView属性的,非常全,收藏一下先. 发现TextView有一个比较大的问题,就是文字排版的问题,遇到数字,字母,符号等就会有问题,目前还没有 ...
- Hierarchy视图里的Transform和Camera组件
Hierarchy视图里的Transform和Camera组件 在Hierarchy视图里,选中Camera,然后在Inspector视图里查看其各组件,如图1-8所示.对于Transform和Cam ...
- PDA手持终端集成一体打印 二次开发
PDA手持终端集成一体打印 二次开发支持 VS2008或VS2005开发工具 c#或C++开发语言 Mobile6.5,支持GSM通话,GPRS,EDGE网络;内置wifi,蓝牙,gps商场单品管理小 ...