博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
阅读量:4668 次
发布时间:2019-06-09

本文共 3030 字,大约阅读时间需要 10 分钟。

A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3468

Description

You have
N integers,
A
1,
A
2, ... ,
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
A
1,
A
2, ... ,
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

 

 

 

题意

 

 区间加,区间查询和

 

题解:

 

线段树,记住懒操作~

 

代码:

//qscqesze#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 200001#define mod 10007#define eps 1e-9int Num;char CH[20];//const int inf=0x7fffffff; //нчоч╢Сconst int inf=0x3f3f3f3f;/*inline void P(int x){ Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}*///**************************************************************************************inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}inline void P(int x){ Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}struct node{ int l,r; ll sum,add; void fun(ll tmp) { add+=tmp; sum+=(r-l+1)*tmp; }}a[maxn*4];ll d[maxn];void relax(int x){ if(a[x].add) { a[x<<1].fun(a[x].add); a[x<<1|1].fun(a[x].add); a[x].add=0; }}void build(int x,int l,int r){ a[x].l=l,a[x].r=r; if(l==r) { a[x].sum=d[l]; } else { int mid=(l+r)>>1; build(x<<1,l,mid); build(x<<1|1,mid+1,r); a[x].sum=a[x<<1].sum+a[x<<1|1].sum; }}void update(int x,int st,int ed,ll c){ int l=a[x].l,r=a[x].r; if(st<=l&&r<=ed) a[x].fun(c); else { relax(x); int mid=(l+r)>>1; if(st<=mid)update(x<<1,st,ed,c); if(ed>mid) update(x<<1|1,st,ed,c); a[x].sum=a[x<<1].sum+a[x<<1|1].sum; }}ll query(int x,int st,int ed){ int l=a[x].l,r=a[x].r; if(st<=l&&r<=ed) return a[x].sum; else { relax(x); int mid=(l+r)>>1; ll sum1=0,sum2=0; if(st<=mid) sum1=query(x<<1,st,ed); if(ed>mid) sum2=query(x<<1|1,st,ed); return sum1+sum2; }}int main(){ int n=read(),m=read(); for(int i=1;i<=n;i++) d[i]=read(); build(1,1,n); char s[3]; int bb,cc,dd; for(int i=0;i

 

转载于:https://www.cnblogs.com/qscqesze/p/4447564.html

你可能感兴趣的文章
并查集详解 (转)
查看>>
Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法
查看>>
jQuery Post
查看>>
从总数中生成一定数量的随机数
查看>>
Strut2页面传参跳转 --Struts2
查看>>
5.User Interface/ActionBar
查看>>
Integer 与 int 中的 ==
查看>>
ReactJS实用技巧(1):JSX与HTML的那些不同
查看>>
java语言程序设计(基础篇) 第2章 基本程序设计 课本源代码
查看>>
装饰者模式 详解
查看>>
【模板】卢卡斯定理
查看>>
[POJ 1273]Drainage Ditches
查看>>
[CODEVS 1036]商务旅行
查看>>
编写高质量代码改善C#程序的157个建议——建议50:在Dispose模式中应区别对待托管资源和非托管资源...
查看>>
MySQL安装与操作总结
查看>>
python 中time, datetime的用法
查看>>
python中将函数赋值给变量时需要注意的一些问题
查看>>
SAS数据挖掘实战篇【五】
查看>>
如何成为合格的数据分析师
查看>>
ArcGIS10.5资源分享
查看>>