POJ_3468_A Simple Problem_线段树
来源:互联网 发布:js设置div display 编辑:程序博客网 时间:2024/06/16 08:27
快两点了还不困,明天要死。
题意:
区间更新,加上一个值,区间查询区间和。
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.
线段树的区间更新,但是看Discuss里面大部分都是splay。。。。
书上用的是树状数组的变形。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<ctime>#include<algorithm>#include<cstdlib>#include<fstream>using namespace std;#define mxn 100010#define mxq 100010int n,q;long long a[mxn];class node{public:int ll,rr;long long sum;long long flag;node(){}};class segment_tree{private:node nd[mxn<<2];public:void build(int l,int r,int id){nd[id].ll=l;nd[id].rr=r;nd[id].flag=0;if(l==r){nd[id].sum=a[l];return;}int m=(l+r)>>1,ls=id<<1,rs=ls|1;build(l,m,ls);build(m+1,r,rs);nd[id].sum=nd[ls].sum+nd[rs].sum;}void down(int);void update(int l,int r,long long x,int id){if(l==nd[id].ll&&r==nd[id].rr){nd[id].flag+=x;nd[id].sum+=x*(r-l+1);return;}if(nd[id].flag)down(id);int m=(nd[id].ll+nd[id].rr)>>1,ls=id<<1,rs=ls|1;if(r<=m)update(l,r,x,ls);else if(l>m)update(l,r,x,rs);else{update(l,m,x,ls);update(m+1,r,x,rs);}nd[id].sum=nd[ls].sum+nd[rs].sum;}long long find(int l,int r,int id){if(nd[id].ll==l&&nd[id].rr==r)return nd[id].sum;if(nd[id].flag)down(id);int m=(nd[id].ll+nd[id].rr)>>1,ls=id<<1,rs=ls|1;if(r<=m)return find(l,r,ls);else if(l>m)return find(l,r,rs);else return find(l,m,ls)+find(m+1,r,rs);}void printAns(int l,int r){long long ans=find(l,r,1);printf("%I64d\n",ans);}}Tree;void segment_tree::down(int id){if(nd[id].ll==nd[id].rr)return;int m=(nd[id].ll+nd[id].rr)>>1,ls=id<<1,rs=ls|1;update(nd[id].ll,m,nd[id].flag,ls);update(m+1,nd[id].rr,nd[id].flag,rs);nd[id].flag=0;}int main(){scanf("%d%d",&n,&q);for(int i=0;i<n;++i)scanf("%I64d",&a[i]);Tree.build(0,n-1,1);int a,b;char type;long long c;for(int i=0;i<q;++i){getchar();scanf("%c",&type);if(type=='Q'){scanf("%d%d",&a,&b);Tree.printAns(a-1,b-1);}else{scanf("%d%d%I64d",&a,&b,&c);Tree.update(a-1,b-1,c,1);}}//system("pause");return 0;}
0 0
- POJ_3468_A Simple Problem_线段树
- POJ_3468_A Simple Problem with Integers_TLE
- hdu 1757 A Simple Math Problem_矩阵快速幂
- BNU A Simple Tree Problem 线段树
- ZOJ3696-A Simple Tree Problem 线段树
- A Simple Problem with Integers(线段树)
- hdu4973 A simple simulation problem. 线段树
- HDU4973:A simple simulation problem.(线段树)
- poj3468A Simple Problem with Integers 线段树
- POJ3468A Simple Problem with Integers线段树
- hdu4267A Simple Problem with Integers 线段树
- hdu4973A simple simulation problem. 线段树
- A Simple Problem with Integers----线段树
- 线段树_poj_3468_A Simple Problem with Integers
- 线段树 A Simple Problem with Integers
- A Simple Problem with Integers_poj3468_线段树
- 线段树 A Simple Problem with Integers
- poj3468 Simple Problem with Integers (线段树)
- opengl编程指南 第七版 源码有bug Page35 lines.c 红宝书
- 插入排序之算法研究
- 【android】Application 使用分析
- Neo4j数据库简介
- socket缓存的研究
- POJ_3468_A Simple Problem_线段树
- 使用dr.memory在win32环境下调试 cocos2dx 项目的内存异常
- Objective-c下具有下载功能的NSURLCache类CustomURLCache
- 今天开通了博客
- 经典算法归并排序的分析及PHP实现
- Linux,IP归属地查询(nali)
- Android ,在争议中逃离 Linux 内核的 GPL 约束
- 集成 admob Banner广告出错之一NSInvalidArgumentException
- Ant 多渠道 自动打包 混淆代码 引入第三方项目