zoj 2706 Thermal Death of the Universe(线段树区间更新+懒惰标记)
来源:互联网 发布:channel.js 下载 编辑:程序博客网 时间:2024/04/28 19:51
Johnie has recently learned about the thermal death concept. Given that the Global Entropy always increases, it will end in the thermal death of the Universe. The idea has impressed him extremely. Johnie does not want the universe to die this way.
So he decided to emulate the process to check how soon the thermal death can occur. He has created the mathematical model of the process in the following way. The universe is represented as an array of n integer numbers. The life of the universe is represented as the sequence of the following entropy operations: take elements from ith to jth and replace them with their average value. Since their average is not necessarily integer, it is rounded.
To keep balance, rounding is performed either up, or down depending on the current sum of all elements of the array. If their sum is less or equal to the sum of the original array, the rounding is performed up, in the other case --- down.
Given the initial array and the sequence of the entropy operations, find the contents of the resulting array.
Input
There are mutiple cases in the input file.
The first line of each case contains n and m --- the size of the array and the number of operations respectively (1 <= m, n <= 30,000 ). The second line contains n integer numbers --- the initial contents of the array, they do not exceed 109 by their absolute value. The following m lines contain two integer numbers each and describe entropy operations.
There is an empty line after each case.
Output
Output n integer numbers --- the contents of the array after all operations.
There should be am empty line after each case.Sample Input
6 41 2 3 4 5 61 22 55 64 6
Sample Output
2 3 3 5 5 5
Source: Andrew Stankevich's Contest #10
分析:注意这里要分类讨论一下正负。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=31000;long long tr[4*N];long long add[4*N];int n,m,x,y;void PushUp(int i){ tr[i]=tr[2*i]+tr[2*i+1];}void PushDown(int i,int len){ if(add[i]) { add[i<<1]=add[i<<1|1]=add[i]; tr[i<<1]=(len-(len>>1))*add[i]; tr[i<<1|1]=(len>>1)*add[i]; add[i]=0; }}void build(int i,int l,int r){ tr[i]=0; add[i]=0; if(l==r) { scanf("%lld",&tr[i]); return ; } int mid=(l+r)/2; build(2*i,l,mid); build(2*i+1,mid+1,r); PushUp(i);}void update(int i,int l,int r,int x,int y,long long c){ if(x<=l&&r<=y) { tr[i]=(r-l+1)*c; add[i]=c; return; } PushDown(i,r-l+1); int mid=(l+r)/2; if(x<=mid) update(2*i,l,mid,x,y,c); if(y>mid) update(2*i+1,mid+1,r,x,y,c); PushUp(i);}long long query(int i,int l,int r,int x,int y){ long long sum=0; if(x<=l&&r<=y) { return tr[i]; } PushDown(i,r-l+1); int mid=(l+r)/2; if(x<=mid) sum+=query(2*i,l,mid,x,y); if(y>mid) sum+=query(2*i+1,mid+1,r,x,y); return sum;}void print(int i,int l,int r){ PushDown(i,r-l+1); if(l==r) { if(l!=n) printf("%lld ",tr[i]); else printf("%lld\n",tr[i]); return; } int mid=(l+r)/2; print(2*i,l,mid); print(2*i+1,mid+1,r);}int main(){ while(~scanf("%d%d",&n,&m)) { build(1,1,n); long long org=tr[1]; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); long long sum=query(1,1,n,x,y); long long dat=sum/(y-x+1); if(sum>0&&query(1,1,n,1,n)<=org) if(sum%(y-x+1)!=0) dat++; if(sum<0&&query(1,1,n,1,n)>org) if(sum%(y-x+1)!=0) dat--; update(1,1,n,x,y,dat); } print(1,1,n); printf("\n"); } return 0;}
- zoj 2706 Thermal Death of the Universe(线段树区间更新+懒惰标记)
- zoj 2706 Thermal Death of the Universe (线段树区间更新,区间求和)
- ZOJ 2706 Thermal Death of the Universe(线段树区间更新)
- 线段树区间更新Thermal Death of the Universe
- ZOJ 2706 Thermal Death of the Universe 线段树
- ZOJ 2706 Thermal Death of the Universe(线段树)
- ZOJ 2706 Thermal Death of the Universe (线段树)
- zoj 2706 Thermal Death of the Universe
- zoj 2706 Thermal Death of the Universe(线段树成段更新)
- ZOJ2706 Thermal Death of the Universe
- ZOJ2706-Thermal Death of the Universe
- hdu 1556 Color the ball(线段树区间更新单点查询+懒惰标记)
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
- NEFU 1111 线段树区间更新+懒惰标记
- HDU1698 线段树+区间更新+懒惰标记-Just a Hook
- [线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记
- hdu1698 Just a Hook(线段树 区间更新+懒惰标记)
- 线段树1(带懒惰标记的区间更新和整棵树的查询)
- 数据库之触发器
- java 再学习之枚举
- [题解] HDU 2128 Tempter of the Bone II(迭代加深)
- jQuery基础(4)
- 博通wifi驱动详解(一)
- zoj 2706 Thermal Death of the Universe(线段树区间更新+懒惰标记)
- java前后端通信常见问题汇总一
- 前后端spring-mvc跨域访问解决方案
- Python函数的用法总结
- react vr 组件之LiveEnvCamera的使用方法
- 学习如何使用Vue
- 【HDU 1257 】 最少拦截系统 【贪心】
- Latex中如何制作参考文献
- 剑指offer--字符串的排列