poj 3468 A Simple Problem with Integers 线段树
来源:互联网 发布:买车贷款和全款 知乎 编辑:程序博客网 时间:2024/06/14 12:03
#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;struct point { long long l; long long r; long long w; long long m;}p[400002];void init(ll l,ll r,ll k){ p[k].l=l; p[k].r=r; p[k].w=0; p[k].m=0; if(l==r) return ; ll m=(l+r)/2; init(l,m,2*k); init(m+1,r,2*k+1);}ll ans;void query(ll l,ll r,ll k){ if(p[k].l==l&&p[k].r==r) { ans+=p[k].w; return ; } if(p[k].m) { p[2*k].w+=(p[2*k].r-p[2*k].l+1)*p[k].m; p[2*k].m=p[2*k].m+p[k].m; p[2*k+1].w+=(p[2*k+1].r-p[2*k+1].l+1)*p[k].m; //当时少了一个加号,错的一塌糊涂 p[2*k+1].m+=p[k].m; p[k].m=0; } ll m=(p[k].l+p[k].r)/2; if(l>m) { query(l,r,2*k+1); } else if(r<=m) { query(l,r,2*k); } else { query(l,m,2*k); query(m+1,r,2*k+1); }}void change(ll l,ll r,ll k,ll w){ if(p[k].l==l&&p[k].r==r) { p[k].w+=w*(r-l+1); p[k].m+=w; return ; } ll m=(p[k].l+p[k].r)/2; p[k].w+=(r-l+1)*w; if(r<=m) { change(l,r,2*k,w); } else if(l>m) { change(l,r,2*k+1,w); } else { change(l,m,2*k,w); change(m+1,r,2*k+1,w); }}int main(){ std::ios::sync_with_stdio(false); ll n,m; cin>>n>>m; init(1,n,1); ll i,j; char ch; long long x,y,z; for(i=1;i<=n;i++) { cin>>x; change(i,i,1,x); } for(i=0;i<m;i++) { cin>>ch; if(ch=='Q') { cin>>x>>y; ans=0; query(x,y,1); cout<<ans<<endl; } else if(ch=='C') { cin>>x>>y>>z; change(x,y,1,z); } }}
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(线段树)
- POJ 1432 Decoding Morse Sequences
- hdu1254推箱子
- css基础学习之--inline-block
- K-means算法
- HDU-3074-Multiply game-单点更新
- poj 3468 A Simple Problem with Integers 线段树
- int,bigint,smallint、tinyint、varchar
- POJ 1661 Help Jimmy
- 编程式使用IoC容器
- 欧拉函数+费马定理 POJ3358
- Linux下编译并使用ZeroMQ
- 动态调试so中init段下断点问题
- 特殊布局
- searchAPI(iOS 9新api)