线段树 POJ 3468 A Simple Problem with Integers
来源:互联网 发布:高校综合数据分析平台 编辑:程序博客网 时间:2024/04/30 14:22
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 abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;long long a[100217*4];long long col[100217*4];int temp=1;void build(int rt, int begin, int end){ int m=(begin+end)/2; if(begin==end) { scanf("%I64d",&a[rt]); return ; } build( rt << 1, begin, m); build( 1 + (rt << 1), m + 1,end); a[rt] = a[rt << 1] + a[1 + (rt << 1)];}void update(int rt, int begin, int end, int x, int y, int v){ int m=(begin+end)/2; if(x>end||y<begin) return ; if(end<=y&&begin>=x) { a[rt]+=(end-begin+1)*v; col[rt]+=v; //printf("%d %d %d %d %d\n",col[rt], begin, end, rt, a[rt]); return ; } if(col[rt]!=0) { a[rt<<1] += (m-begin+1)*col[rt]; a[(rt<<1)+1] += (end-(m+1)+1)*col[rt]; col[rt<<1] += col[rt] ; col[(rt<<1)+1] += col[rt] ; col[rt] = 0; } update( rt << 1, begin, m, x, y, v); update( 1 + (rt << 1), m + 1,end, x, y, v); a[rt] = a[rt << 1] + a[1 + (rt << 1)];}long long query(int rt, int begin, int end, int x, int y){ int m=(begin+end)/2; if(x>end||y<begin) return 0; if(end<=y&&begin>=x) { //printf("%d-\n",rt); //printf("%d\n",a[rt]); long long t=a[rt]; return t; } if(col[rt]!=0) { a[rt<<1 ]+= (m-begin+1)*col[rt]; a[(rt<<1)+1] += (end-(m+1)+1)*col[rt]; col[rt<<1] += col[rt] ; col[(rt<<1)+1] += col[rt] ; col[rt] = 0; } long long p1 = 0,p2 = 0; p1 = query( rt << 1, begin, m, x, y); p2 = query( 1 + (rt << 1), m + 1,end, x, y); //printf("%d %d\n",p1,p2); return p1+p2; //a[rt]=a[rt << 1] + a[1 + (rt << 1)];}int main(){ int T,n,m; while(scanf("%d%d",&n,&m)!=-1) { build(1,1,n); // puts("1"); memset(col,0,sizeof(col)); // getchar(); // printf("%d\n",a[1]); while(m--) { //getchar(); int x,y,v; char ch[10]; scanf("%s",&ch); //printf("%c\n",ch); if(ch[0]=='Q') { scanf("%d%d", &x, &y); printf("%lld\n", query(1,1,n,x,y)); // puts("y");// for(int i=1;i<n*4;i++)// printf("%I64d %d\n",a[i],i); } else { //puts("x"); scanf("%d%d%d", &x, &y, &v); update(1,1,n,x,y,v); //printf("%d\n",a[1]);// for(int i=1;i<n*4;i++)// printf("%I64d %d\n",a[i],i); } } } return 0;}
0 0
- 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(线段树)
- poj-3468-A Simple Problem with Integers(线段树)
- html5绘制图形渐变-径向渐变
- GDCPC2016 参赛总结
- 三层架构详解(服务员-厨师-采购员)
- 垂直居中大全
- 名称空间
- 线段树 POJ 3468 A Simple Problem with Integers
- 贪心算法介绍与使用
- 微信公众平台开发-自定义菜单
- HTML5之地理信息
- 利用本地服务器发布网站,IIS配置+花生壳映射服务(附带视频)
- 安装JDK
- UVA_191_Intersection
- android property
- CodeForces 149D Coloring Brackets