poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 63565 | Accepted: 19546 | |
| 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
给出n个数q次操作
C代表把a到b间的数分别加c
Q要求输出和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=111111;
long long sum[MAXN<<2];
long long lazy[MAXN<<2];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(l>=L&R>=r)
{
lazy[rt]+=c;
sum[rt]+=(long long)c*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(L,R,c,l,mid,rt<<1);
if(R>mid)
update(L,R,c,mid+1,r,rt<<1|1);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(l>=L&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
long long cnt=0;
if(L<=mid)
cnt=query(L,R,l,mid,rt<<1);
if(R>mid)
cnt+=query(L,R,mid+1,r,rt<<1|1);
return cnt;
}
int main()
{
int n,q;
char op[2];
int a,b,c;
while(scanf("%d %d",&n,&q)!=EOF)
{
build(1,n,1);
//printf("%d\n",sum[1]);
while(q--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d %d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else
{
scanf("%d %d %d",&a,&b,&c);
update(a,b,c,1,n,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(线段树,区间更新,区间求和)
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 ...
- 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 ...
随机推荐
- 聊聊C++模板函数与非模板函数的重载
前言 函数重载在C++中是一个很重要的特性.之所以有了它才有了操作符重载.iostream.函数子.函数适配器.智能指针等非常有用的东西. 平常在实际的应用中多半要么是模板函数与模板函数重载,或者是非 ...
- SSM+Maven的JavaWeb项目中的异常的可能性
1.404 可能:1):被拦截了,即:springmvc中的controller可能不存在,可能没有被配置,可能配置出错 2):资源确实不存在 3):路径出错 2.500,程序异常,但是业务逻辑什么都 ...
- 《简明python教程》笔记三
图形软件(GUI工具) 可供选择的GUI: 一.PyQT 是Qt工具包的python绑定.Qt工具包是构建KDE的基石.linux下使用免费,windows下使用收费. 二.PyGTK 是GTK+工具 ...
- Spring --- 异常处理机制
1.定义全局异常处理器,为全局的异常,如出现将调用 error.JSP <!-- 定义异常处理器 --> <bean class="org.springframework. ...
- eclipse中的project 和classpath文件的具体作用
项目->右键 Properties-> java Build Path -> source: xxx/data xxx/src 体现在 .classpath文件中: .classpa ...
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
- [转]python dubbo接口测试
转自:https://www.cnblogs.com/chunyanxu/p/8732734.html 会吐泡泡的鱼 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔- 57 文章- 0 评论- ...
- HDU 1495 非常可乐【BFS/倒水问题】
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- HDU 6313: Hack it
Hack It Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 【博弈论】【SG函数】hdu1848 Fibonacci again and again
某个状态的SG函数被定义为 除该状态能一步转移到的状态的SG值以外的最小非负整数. 有如下性质:从SG值为x的状态出发,可以转移到SG值为0,1,...,x-1的状态. 不论SG值增加与否,我们都可以 ...