ZOJ 2706 Thermal Death of the Universe (线段树)
题目链接:ZOJ 2706 Thermal Death of the Universe (线段树)
题意:n个数。m个操作。
每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的数列总和小于等于原数列总和。取平均值的上界,反之。取下界。
注意有负数的情况。
AC代码:
#include<stdio.h>
#include <math.h>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=30010;
const LL INF=0xffffffffffff;
struct node
{
LL l,r,laz;
LL sum;
LL mid()
{
return (l+r)/2;
}
};
struct node tree[maxn<<2];
LL pre;
void build(LL l,LL r,LL rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].laz=INF;
if(tree[rt].l==tree[rt].r)
{
scanf("%lld",&tree[rt].sum);
return ;
}
LL m=tree[rt].mid();
build(lson);
build(rson);
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
} void PushDown(LL rt,LL m)
{
if(tree[rt].laz!=INF)
{
tree[rt<<1].laz=tree[rt].laz;
tree[rt<<1|1].laz=tree[rt].laz;
tree[rt<<1].sum=(m-(m>>1))*tree[rt].laz;
tree[rt<<1|1].sum=(m>>1)*tree[rt].laz;
tree[rt].laz=INF;
}
} void updata(LL L,LL R,LL add,LL rt)
{
if(L<=tree[rt].l && tree[rt].r<=R)
{
tree[rt].laz=add;
tree[rt].sum=(tree[rt].r-tree[rt].l+1)*add;
return ;
}
LL m=tree[rt].mid();
PushDown(rt,tree[rt].r-tree[rt].l+1); if(R<=m)
updata(L,R,add,rt<<1);
else if(L>m)
updata(L,R,add,rt<<1|1);
else
{
updata(L,R,add,rt<<1);
updata(L,R,add,rt<<1|1);
}
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
} LL query(LL L,LL R,LL rt)
{
if(L<=tree[rt].l && tree[rt].r<=R)
{
return tree[rt].sum;
}
LL m=tree[rt].mid();
PushDown(rt,tree[rt].r-tree[rt].l+1);
LL ret=0;
if(R<=m)
ret+=query(L,R,rt<<1);
else if(L>m)
ret+=query(L,R,rt<<1|1);
else
{
ret+=query(L,R,rt<<1);
ret+=query(L,R,rt<<1|1);
}
return ret;
} int main()
{
LL a,b,i;
LL n,m;
LL ans;
double ave;
//printf("%lld\n",INF);
while(scanf("%lld %lld",&n,&m)!=EOF)
{
//memset(tree,0,sizeof tree);
build(1,n,1);
pre=0;
for(i=1;i<=n;i++)
pre+=query(i,i,1);
while(m--)
{
scanf("%lld%lld",&a,&b);
LL sum=query(a,b,1);
ave=1.0*sum/(b-a+1);
if(query(1,n,1)<=pre)
ans=(LL)ceil(ave);
else
ans=(LL)floor(ave);
updata(a,b,ans,1);
}
for(i=1;i<n;i++)
printf("%lld ",query(i,i,1));
printf("%lld\n\n",query(i,i,1));
}
return 0;
}
/*
6 1
1 2 3 4 5 6
1 6
6 2
1 2 3 4 5 6
2 6
1 5
1 1 1 1
1
1 1 6 2
1 1 1 3 1 1
3 4
4 5 6 4
1 2 3 4 5 6
1 2
2 6
1 3
2 5 3 3
-1 -1 -2
1 2
2 3
1 3
*/
ZOJ 2706 Thermal Death of the Universe (线段树)的更多相关文章
- ZOJ 2706 Thermal Death of the Universe
Thermal Death of the Universe Time Limit: 10 Seconds Memory Limi ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- ZOJ 3686 A Simple Tree Problem(线段树)
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- 143 - ZOJ Monthly, October 2015 I Prime Query 线段树
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
- zoj 3511 Cake Robbery(线段树)
problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
随机推荐
- N皇后递归
问题: n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在 n*n的棋盘上,互相不能攻击,输出全部方案. #include <iostream> using namespace std ...
- Centos 7 编译nginx 1.14.0
步骤一:下载nginx安装包 wget https://nginx.org/download/nginx-1.14.0.tar.gz 步骤二:安装nginx依赖包 yum install -y gcc ...
- 如何用纯 CSS 创作一个充电 loader 特效
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/deNqdV 可交互视频教程 此视 ...
- apidoc利用代码注释书写文档
个人博客同步文章 https://mr-houzi.com/2018/07/... apidoc是一款利用源代码中注释来创建RESTful Web API文档的工具.apidoc可用于C#,Go,Da ...
- python_OS 模块
os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...
- springMVC中处理静态资源的几种方案
处理静态资源方案一:在web.xml文件中配置如下: <!-- <!–解决静态资源方案–> <servlet-mapping> <servlet-name>d ...
- tcpcopy简单用法
这篇文章介绍下网易开源的流量重放(replay)工具TCPCopy,说是简单介绍,绝对不是谦虚,因为自己了解的确实也不多.为什么不甚了解呢,大家可以到TCPCopy的官方仓库看看,https://gi ...
- luogu3313 [SDOI2014]旅行
对每一个宗教建一棵线段树,然后树剖搞搞 #include <iostream> #include <cstdio> using namespace std; int n, m, ...
- HDU-1061-Rightmost Digit,快速幂水过!~~
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- datatable生成easyui的json格式汇总( 转)
转自 http://www.cnblogs.com/WikStone/archive/2012/07/02/2573137.html 目前项目没有使用第三方的json转换库,都是根据json格式进行字 ...