poj3468 A Simple Problem with Integers (线段树区间最大值)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 92127 | Accepted: 28671 | |
Case Time Limit: 2000MS |
描述
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.
输入
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.
输出
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
Source
#include<cstdio>
#include<iostream>
#define LL long long
#define R(u) (u<<1|1)
#define L(u) (u<<1)
using namespace std;
const int maxx=100005;
LL a[maxx];
int n,m;
struct Node{
int r,l;
LL add,sum;
}node[maxx<<2];
void Pushup(int u)
{
node[u].sum=node[L(u)].sum+node[R(u)].sum;
return;
}
void Pushdown(int u)
{
node[L(u)].add+=node[u].add;
node[R(u)].add+=node[u].add; node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add; node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
node[u].add=0; //一定记得清零
}
void Build(int u,int left,int right)
{
node[u].l=left,node[u].r=right;
node[u].add=0;
if(left==right)
{
node[u].sum=a[left];
return;
}
int mid=(left+right)>>1;
Build(L(u),left,mid);
Build(R(u),mid+1,right);
Pushup(u);
}
void update(int u,int left,int right,LL val)
{
if(left==node[u].l&&node[u].r==right)
{
node[u].add+=val;
node[u].sum+=(node[u].r-node[u].l+1)*val;
return;
}
node[u].sum+=(right-left+1)*val;// 当更新区间小于这段
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) update(L(u),left,right,val);//在左边
else if(mid<left) update(R(u),left,right,val);
else {
update(L(u),left,mid,val);
update(R(u),mid+1,right,val);
}
//Pushup(u);前面已经直接算出sum后面不用再Pushup了
}
LL Qurey(int u,int left,int right)
{
if(left==node[u].l&&node[u].r==right)
return node[u].sum;
if(node[u].add)Pushdown(u);
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) Qurey(L(u),left,right);
else if(mid<left) Qurey(R(u),left,right);
else return (Qurey(L(u),left,mid)+Qurey(R(u),mid+1,right));
//Pushup(u);
}
int main()
{
cin>>n>>m;
LL c;
for(int i=1;i<=n;i++)
scanf("%I64d",a+i);
Build(1,1,n);
while(m--)
{char x;int ai,an;
scanf("%c %d %d",&x,&ai,&an);
cin>>x>>ai>>an;
if(x=='C')
{
scanf("%I64d",&c);
update(1,ai,an,c);
}
else
printf("%I64d\n",Qurey(1,ai,an));
}
return 0;
}
poj3468 A Simple Problem with Integers (线段树区间最大值)的更多相关文章
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- [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 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 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 (线段树区间更新求和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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
随机推荐
- hive和ORACLE语法对比
- 洛谷P3368 【模板】树状数组 2
P3368 [模板]树状数组 2 102通过 206提交 题目提供者HansBug 标签 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 如题,已知一个数列,你需要进行下面两 ...
- 2016-08-05:samba服务器配置
centos samba服务器配置 配置smb.conf文件 [share] path = /home/lee writable = yes 添加smb用户 smbpasswd -a root 启动s ...
- C#如何定义全局变量
C#中没有全局变量的概念,可以定义一个common类,通过静态变量来存放所有需要的全局变量,调用的时候通过common来调用即可. 例如: public static class common // ...
- 【C#基础】System.Reflection (反射)
在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于"自成一体"的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息.在System. ...
- js分辨浏览器类别和版本
function BrowserInfo() { var ua = navigator.userAgent.toLowerCase(); var Sys = {}; var s; (s = ua.ma ...
- ps 进程查看器
命令参数 a 显示所有进程 -a 显示同一终端下的所有程序 -A 显示所有进程 c 显示进程的真实名称 -N 反向选择 -e 等于"-A" e 显示环境变量 f 显示程序间的关系 ...
- 随笔—邀请赛前训— Codeforces Round #330 (Div. 2) B题
题意: 这道英文题的题意稍稍有点复杂. 找长度为n的数字序列有多少种.这个序列可以分为n/k段,每段k个数字.k个数可以变成一个十进制的数Xi.要求对这每n/k个数,剔除Xi可被ai整除的情况,剔除X ...
- 循序渐进之Spring AOP(6) - 使用@Aspect注解
前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...
- C++ dll 通用dll编写
头文件 extern "C" _declspec(dllexport)void AddFunction(); cpp文件 extern "C" _declspe ...