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<< ...
随机推荐
- Sql server big data如何批量update数据
原因: 要一次性update 2千万条数据,虽然update sql很简单,但是由于一次性修改太多的数据,造成数据库log满了,就会报error: [ErrorCode: 9002, SQL Stat ...
- 【转】Rails中Bootstrap的安装和使用
转自:http://blog.csdn.net/lissdy/article/details/9195651 眼看着前端攻城师们都开始使用Bootstrap创作网页,于是也想学着在最近正在学习的 ...
- SQL基础(3)
SQL FULL JOIN (1)SQL FULL JOIN关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. (2)语法 SELECT column_name(s) FROM t ...
- 谈谈开发文本转URL小工具的思路
URL提供了一种定位互联网上任意资源的手段,由于采用HTTP协议的URL能在互联网上自由传播和使用,所以能大行其道.在软件开发.测试甚至部署的环节,URL几乎可以说无处不再,其中用来定位文本的URL数 ...
- pyodbc连接MySQL数据库
1:Python安装pyodbc:pip install pyodbc 2:安装unixODBC-2.3.4.tar.gz ./configure make make install 3:下载基于py ...
- SPARC T4 RAID Setup (ZT)
http://www.confignotes.com/2013/09/sparc-t4-raid-setup/ How to configure a RAID volume with the inte ...
- 问题:oracle DECLARE 变量重复利用;结果:Oracle 定义变量总结
首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...
- struts2学习笔记(1)配置与基本操作
主要作用:将请求与页面区分开 配 置: 下载struts 2.0,在安装路径D:\项目学习\三大框架视屏\struts-2.3.24-all\struts-2.3.24\apps 中解压struts2 ...
- PROCEDURE存储过程传入表参数
) ,itemNum ) ,itemQty )) ---2.创建一个存储过程以表值参数作为输入 alter proc usp_TestProcWithTable @tb LocationTa ...
- [luogu3379]最近公共祖先(树上倍增求LCA)
题意:求最近公共祖先. 解题关键:三种方法,1.st表 2.倍增法 3.tarjan 此次使用倍增模板(最好采用第一种,第二种纯粹是习惯) #include<cstdio> #includ ...