POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)
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<stdio.h>
using namespace std; const int MAXN = ;
int a[MAXN];
long long int tree[MAXN*];
long long int lazy[MAXN*];
int N,Q;
string s;
int x , y, z;
void push_down(int l ,int r ,int rt)
{
int m = (l+r)/; if(lazy[rt])
{
tree[rt*] += lazy[rt]*(m-l+);
tree[rt*+] += lazy[rt]*(r-m);
lazy[rt*] += lazy[rt];
lazy[rt*+] += lazy[rt];
lazy[rt] = ;
}
}
void bulid_tree(int l ,int r ,int rt)
{
if(l==r)
{
tree[rt] = a[l];
return ;
}
int m = (l+r)/; bulid_tree(l,m,rt*);
bulid_tree(m+,r,rt*+);
tree[rt] = tree[rt*]+tree[rt*+];
} long long int Query(int x ,int y ,int l ,int r ,int rt)
{
long long sum = ;
if(x<=l&&r<=y)
{
return tree[rt];
}
int m = (l+r)/;
push_down(l,r,rt);
if(x<=m)
{
sum += Query(x,y,l,m,rt*);
}
if(m<y)
{
sum += Query(x,y,m+,r,rt*+);
}
return sum;
}
void Update(int x ,int y ,int k ,int l ,int r ,int rt)
{
if(x<=l&&y>=r)
{
tree[rt] += k*(r-l+);
lazy[rt] += k;
return ;
}
push_down(l,r,rt);
int m = (l+r)/;
if(x<=m)
{
Update(x,y,k,l,m,rt*);
}
if(y>m)
{
Update(x,y,k,m+,r,rt*+);
}
tree[rt] = tree[rt*]+tree[rt*+];
}
int main()
{
scanf("%d%d",&N,&Q);
for(int i = ; i <= N;i++)
{
scanf("%d",&a[i]);
}
bulid_tree(,N,);
while(Q--)
{
cin>>s;
if(s[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",Query(x,y,,N,)); }
else
if(s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
Update(x,y,z,,N,);
}
}
return ;
}
POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)的更多相关文章
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 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 (线段树区间更新求和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 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [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<< ...
随机推荐
- Extjs5.0 学习之路【结构篇】
Extjs5.0在原有的MVC模式下增加了一个MVVM Extjs5.0项目文件执行顺序. 新增特性一 bind---data
- 详解NXP Cortex-M3加密设置
加密芯片是对内部集成了各类对称与非对称算法,自身具有极高安全等级,可以保证内部储存的密钥和信息数据不被非法读取与篡改的一类安全芯片的通称.使用到安全加密芯片的产品:银行加密U盾.刻录机.加密硬盘.P ...
- websocket之一:websocket简介
Websocket websocket为一次HTTP握手后,后续通讯为tcp协议的通讯方式. WebSocket 使用一种被称作“Upgrade handshake(升级握手)”的机制将标准的 HTT ...
- 开发环境无错,部署至测试环境报错“NoSuchMethodError”OR"NoSuchClassError"
背景: 实现一个简单的功能,需要用到jedis的jar包连接Redis.在之前便已经有使用jedis,它的版本比较旧,是2.1的.而新实现的功能,在编码的时候使用的是2.8的.在开发环境完成单元测试后 ...
- enable or disable Oracle block change tracking
Oracle的block change tracking用于记录上次备份以来改变过的block信息,因此打开block change tracking可以大大加快增量备份的速度. 1. Enable ...
- eclipse格式化代码模板
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...
- Group Layout
----------------siwuxie095 将根面板 contentPane 的布局切换为 Group Layout Grou ...
- 基于IFC的大型三维城市群体——智慧城市模拟
- win10 requireAdministrator设置开机自启动无效的解决方案
开发了一个wpf程序,需要管理员权限,设置了requireAdministrator 同时需要开机自启动,所以添加了注册表: using (RegistryKey key = Registry.Cur ...
- 前端学习笔记2017.6.12 HTML的结构以及xhtml、html、xml的区别
HTML的结构 一个HTML文档可分为几个部分,如下图所示: DOCTYPE部分.head部分和body部分 DOCTYPE部分,这个很重要,可以理解为不同的DOCTYPE意味着不同的html标准,因 ...