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 ...
随机推荐
- QT+信号有参数与无参数的实现+QT4和QT5在信号和槽使用上的区别
在QT5中,信号有参数和无参数 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QWidget> #include <QPushB ...
- 4.关于while循环的基础小练习
1)使用while.if循环输入123456 8910 count = 0 while count < 10: count += 1 if count == 7: print('') else: ...
- Spoj8093 Sevenk Love Oimaster
题目描述 题解: 对于所有n串建广义后缀自动机. (广义后缀自动机唯一区别就是每次将las附成1,并不需要在插入时特判) 建完后再建出parent树,然后用dfs序+树状数组搞区间不同种类. 其实就是 ...
- laravel使用总结(一)
安装 composer create-project laravel/laravel learnlaravel5 --prefer-dist v5.3.* 安装成功之后会自动生成一个key > ...
- Linux基础(Ubuntu)
更换apt源为清华源 1.备份 /etc/apt/sources.list 2.vim 编辑 /etc/apt/sources.list 添加 deb http://mirrors.tuna.tsin ...
- uwsgs loading shared libraries: libicui18n.so.58 异常处理
背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...
- 大数据学习——采集目录到HDFS
采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大要素 l 采集源,即source——监控文件目录 : spoold ...
- centos 7 下vnc弹出窗口太小解决方法
使用以下参数启动 : vncserver :2 -geometry 800x600
- POJ 1724 二维费用最短路
题目大意:有N个城市,编号1-N有R条路,每条路(单向)的起点为Si,终点为Di,长度为Li,如果要走这条路需要花Ti的钱现在你只有K元钱,求在不超支的前提下,从1走到N需要的最短距离 这里总是希望路 ...
- Pytho操作MySQL
一.相关代码 数据库配置类 MysqlDBConn.py 01 #encoding=utf-8 02 ''' 03 Created on 2012-11-12 04 05 @author: St ...