ZOJ Problem Set - 2706 Thermal Death of the Universe
Chieh
posted @ 2015年8月19日 16:43
in NO Answer No Speak
, 218 阅读
/* Author:Chieh Because Of Coding */ #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=3*12345; struct he { int l,r; LL lazy; LL sum; } Sky[maxn*4]; void BuildTree(int l,int r,int now) { Sky[now].l=l; Sky[now].r=r; Sky[now].lazy=0; Sky[now].sum=0; if(l==r)return; int mid=(l+r)/2; BuildTree(l,mid,now*2); BuildTree(mid+1,r,now*2+1); } void Push(int now) { if(Sky[now].lazy==0)return; int nl=Sky[now].l; int nr=Sky[now].r; if(nl==nr)return; LL v=Sky[now].lazy; int mid=(nl+nr)/2; Sky[now*2].lazy=v; Sky[now*2].sum=v*(mid-nl+1); Sky[now*2+1].lazy=v; Sky[now*2+1].sum=v*(nr-mid); Sky[now].lazy=0; } void Update(int l,int r,int now,LL val) { if(Sky[now].l==l&&Sky[now].r==r) { Sky[now].lazy=val; Sky[now].sum=val*(r-l+1); return; } int mid=(Sky[now].l+Sky[now].r)/2; if(r<=mid) { Update(l,r,now*2,val); } else if(l>mid) { Update(l,r,now*2+1,val); } else { Update(l,mid,now*2,val); Update(mid+1,r,now*2+1,val); } Sky[now].sum=Sky[now*2].sum+Sky[now*2+1].sum; } LL Query(int l,int r,int now) { if(Sky[now].l==l&&Sky[now].r==r) { return Sky[now].sum; } int mid=(Sky[now].l+Sky[now].r)/2; LL o1=0; LL o2=0; Push(now); if(r<=mid) { o1=Query(l,r,now*2); } else if(l>mid) { o2=Query(l,r,now*2+1); } else { o1=Query(l,mid,now*2); o2=Query(mid+1,r,now*2+1); } return o1+o2; } int n,m; LL Calc(LL val, int dn, bool isok) { if(val>=0){ int p=val/dn; if(isok)return p+1; else return p; } else{ int p=val/dn; if(isok)return p; else return p-1; } } void init() { BuildTree(1,n,1); int t; LL sum=0; for(int i=1; i<=n; i++) { scanf("%d",&t); Update(i,i,1,t); sum+=t; } int l,r; while(m--) { scanf("%d%d",&l,&r); if(l>r)swap(l,r); LL p=Query(l,r,1); int len=(r-l+1); if(p%len==0) { Update(l,r,1,p/len); } else { if(Sky[1].sum<=sum) { Update(l,r,1,Calc(p,r-l+1,1)); } else { Update(l,r,1,Calc(p,r-l+1,0)); } } } } void play() { bool first=1; for(int i=1; i<=n; i++) { LL p=Query(i,i,1); if(!first)printf(" "); first=0; printf("%lld",p); } printf("\n"); printf("\n"); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); play(); } // cout << "Hello world!" << endl; return 0; }