POJ3468(树状数组区间维护)
2024-09-03 11:54:53
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 89818 | Accepted: 27967 | |
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
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int MAXN=;
struct Node{
ll sum,lazy;
int l,r;
};
struct SegmentTree{
private:
Node a[MAXN*];
public:
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r)
{
scanf("%lld",&a[rt].sum);
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
build(rt<<,l,mid);
build(rt*+,mid+,r);
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
void pushDown(int rt)
{
int mid=(a[rt].l+a[rt].r)>>;
a[rt<<].sum+=(mid-a[rt].l+)*a[rt].lazy;
a[rt*+].sum+=(a[rt].r-mid)*a[rt].lazy;
a[rt<<].lazy+=a[rt].lazy;
a[rt*+].lazy+=a[rt].lazy;
a[rt].lazy=;
}
void change(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].sum+=(r-l+)*val;
a[rt].lazy+=val;
return ;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
change(rt<<,l,r,val);
}
else if(mid<l)
{
change(rt*+,l,r,val);
}
else
{
change(rt<<,l,mid,val);
change(rt*+,mid+,r,val);
}
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
ll query(int rt,int l,int r)
{
if(a[rt].l==l&&a[rt].r==r)
{
return a[rt].sum;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
return query(rt<<,l,r);
else if(mid<l)
return query(rt*+,l,r);
else
{
return query(rt<<,l,mid)+query(rt*+,mid+,r);
}
}
};
SegmentTree solver;
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
solver.build(,,n);
for(int i=;i<m;i++)
{
scanf("%*c");
char op;
scanf("%c",&op);
if(op=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",solver.query(,l,r));
}
else
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
solver.change(,l,r,val);
}
}
} return ;
}
最新文章
- hdu 1106 排序(水题)
- PSP
- .Net分页实现
- 问题-PopupMenu是哪个控件调用弹出的?
- Windows XP系统安装SQL Server 2005(开发版)图解
- Oracle EBS-SQL (AR-1):检查应收收款发生额
- Javascript高级程序设计学习笔记一
- 【Alpha】Daily Scrum Meeting——Day5
- CentOS 7 安装Graphite
- makefile的命令包定义及使用
- Pandas逐行读取Dateframe并转为list
- 解决VS2015单元测试“未能设置用于运行测试的执行上下文”问题
- 二叉查找树及B-树、B+树、B*树变体
- Opencv基本数据类型
- More Effective C++ Item14:明智运用exception specifications
- socket 中午吃的啥 socket 并发服务器 fork
- Nodejs -- 使用koa2搭建数据爬虫
- [ios]";The identity used to sign the executable is no longer valid";错误解决方法
- nginx信号量
- jmeter+ant+jenkins+mac报告优化(二):添加90% Line和QPS