POJ_3468_A Simple Problem_线段树
2014-11-08 01:42
288 查看
快两点了还不困,明天要死。
题意:
区间更新,加上一个值,区间查询区间和。
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。。。。
书上用的是树状数组的变形。
题意:
区间更新,加上一个值,区间查询区间和。
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 100010 int 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; }
相关文章推荐
- 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(线段树 成端增减,区间求和)
- [ACM] 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(线段树——区间更新)