poj 3468 A Simple Problem with Integers【线段树区间修改】
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 79137 | Accepted: 24395 | |
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<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 100000+10
using namespace std;
long long add[MAX<<2];
long long sum[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
if(add[o])
{
add[o<<1]+=add[o];
add[o<<1|1]+=add[o];
sum[o<<1]+=add[o]*(m-(m>>1));
sum[o<<1|1]+=add[o]*(m>>1);
add[o]=0;
}
}
void gettree(int o,int l,int r)
{
add[o]=0;
if(l==r)
{
scanf("%lld",&sum[o]);
return ;
}
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L,int R,int val)
{
if(L<=l&&R>=r)
{
add[o]+=val;
sum[o]+=val*(r-l+1);
return ;
}
pushdown(o,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(o<<1,l,mid,L,R,val);
if(R>mid)
update(o<<1|1,mid+1,r,L,R,val);
pushup(o);
}
long long find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return sum[o];
}
pushdown(o,r-l+1);
long long ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans+=find(o<<1,l,mid,L,R);
if(R>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
int main()
{
int n,m,i,j;
int a,b,c;
char d[12];
while(scanf("%d%d",&n,&m)!=EOF)
{
gettree(1,1,n);
for(i=1;i<=m;i++)
{
scanf("%s%d%d",d,&a,&b);
if(d[0]=='Q')
printf("%lld\n",find(1,1,n,a,b));
else
{
scanf("%d",&c);
update(1,1,n,a,b,c);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers【线段树区间修改】的更多相关文章
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- 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 Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 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 线段树区间更新
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 , 线段树+区间更新。
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(线段树区间更新)
题目地址: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<< ...
随机推荐
- Jquery中去除左右空格
$.trim(" abc "); jQuery.trim(" abc ");
- php学习,一个简单的Calendar(2) 一个简单的活动页面
有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来 这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...
- Computer Talker with C# z
Using the Code Add a textbox named 'txtWords' to a form. Add a button named 'btnSpeak' to a form. Ad ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含 ...
- [转贴]从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
一.C语言错误处理方法 1.返回值(if … else语句判断错误) 2.errno(linux 系统调用) 3.goto语句(函数内局部跳转) 4.setjmp.longjmp(Do not use ...
- zabbix 通过key 获取
zabbix:/root# zabbix_get -s 192.168.2.224 -k "perf_counter[\Processor(_Total)\% Processor Time] ...
- nginx+gunicorn
wsgi接口,使用gunicorn作为server,想在外层加nginx. 配置了 proxy_pass http://127.0.0.1:9008; 访问报301. 参考gunicorn 官网配 ...
- 【HDOJ】3234 Exclusive-OR
并查集.对于对元素赋值操作,更改为I p n v.令val[n]=0(任何数与0异或仍为原值).考虑fa[x] = fx, fa[y] = fy.如果使得fa[fx] = fy, 那么val[fx] ...
- 【HDOJ】1166 敌兵布阵
线段树. #include <stdio.h> #define maxn 55555 ]; void PushUP(int rt) { sums[rt] = sums[rt<< ...