poj 3468 A Simple Problem with Integers经典区间更新区间查询
来源:互联网 发布:自动顶贴软件 编辑:程序博客网 时间:2024/06/01 07:53
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
Hint
Source
题目很经典但有些地方要注意。详细见代码。
#include <iostream>#include<string.h>#include<stdio.h>using namespace std;const int maxn=100100;int n,q;typedef __int64 ll;struct node{ int l,r; ll addv; ll sum;} smt[3*maxn];void btree(int l,int r,int k){ int ls,rs,mid; smt[k].l=l; smt[k].r=r; smt[k].addv=0; if(smt[k].l==smt[k].r) { scanf("%I64d",&smt[k].sum); return; } ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; btree(l,mid,ls); btree(mid+1,r,rs); smt[k].sum=smt[ls].sum+smt[rs].sum;}void update(int l,int r,int k,int c){ int ls,rs,mid; if(smt[k].l==l&&smt[k].r==r) { smt[k].addv+=c; return; } ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; if(smt[k].addv) { smt[ls].addv+=smt[k].addv; smt[rs].addv+=smt[k].addv; smt[k].addv=0; } if(l>mid) update(l,r,rs,c); else if(r<=mid) update(l,r,ls,c); else { update(l,mid,ls,c); update(mid+1,r,rs,c); } smt[k].sum=smt[ls].sum+(smt[ls].r-smt[ls].l+1)*smt[ls].addv+smt[rs].sum+(smt[rs].r-smt[rs].l+1)*smt[rs].addv;}ll qu(int l,int r,int k){ int ls,rs,mid; if(smt[k].l==l&&smt[k].r==r) return smt[k].sum+(smt[k].r-smt[k].l+1)*smt[k].addv; ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; if(smt[k].addv) { smt[ls].addv+=smt[k].addv; smt[rs].addv+=smt[k].addv; smt[k].sum+=(smt[k].r-smt[k].l+1)*smt[k].addv;//下传后要注意更新sum不然下次查询时会出问题 smt[k].addv=0; } if(l>mid) return qu(l,r,rs); else if(r<=mid) return qu(l,r,ls); else return qu(l,mid,ls)+qu(mid+1,r,rs);}int main(){ int a,b,c; char com[10]; while(~scanf("%d%d",&n,&q)) { btree(1,n,1); gets(com); while(q--) { scanf("%s",com); if(com[0]=='C') { scanf("%d%d%d",&a,&b,&c); update(a,b,1,c); } else { scanf("%d%d",&a,&b); printf("%I64d\n",qu(a,b,1)); } } } return 0;}
10 221 2 3 4 5 6 7 8 9 10Q 4 4C 1 10 3C 6 10 3C 6 9 3C 8 9 -100C 7 9 3C 7 10 3C 1 10 3Q 6 10Q 6 9Q 8 9Q 7 9Q 7 10Q 1 10Q 2 4C 3 6 3Q 9 9Q 1 1Q 5 5Q 6 6Q 7 7Q 6 8ans4-82-104-147-122-100-3727-737142125-28
- poj 3468 A Simple Problem with Integers经典区间更新区间查询
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
- POJ 3468 A Simple Problem with Integers 树状数组(区间更新,区间查询)
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
- poj 3468 A Simple Problem with Integers(区间查询区间更新)
- 线段树 (更新区间查询区间)poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers(线段树+树状数组区间更新区间查询)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 :A Simple Problem with Integers——区间更新线段树经典题目
- POJ 3468 A Simple Problem with Integers (线段树区间更新经典)
- poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
- POJ 3468 A Simple Problem with Integers 线段树成段更新+区间查询
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- POJ - 3468 A Simple Problem with Integers(线段树成段更新,查询区间和)
- poj 1523 SPF(无向图点的连通性问题)
- Eclipse中处理图片遇到的问题
- 引导蜘蛛抓取文章的方法分享
- u-boot_smdkv210 分析四:启动代码汇编部分总结
- 八皇后问题(递归)
- poj 3468 A Simple Problem with Integers经典区间更新区间查询
- 结构体内存分布,指针,指针的指针。
- HDOJ搜索题(总计100题)
- nyoj,283,对称排序
- Linux基础编程-文件操作open,lseek,read,write,close
- 关于/usr/bin/env
- Android中文语音合成(TTS)
- ios中子线程如何加载图片
- 并查集