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 ...
随机推荐
- Caused by: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.bj186.crm.pojo.User
原因分析: myBatis在写delete语句的时候,多写了一个属性 错误的代码: <delete id="deleteUser" parameterType="I ...
- mongdb数据库的操作
一.数据库使用 1.使用mongodb服务,必须先开启服务,开启服务使用 mongod --dbpath D:mongdb (D:mongdb 自己所创建数据库的路径, 在cmd窗口中输入) ...
- (14) openssl x509(签署和自签署)
主要用于输出证书信息,也能够签署证书请求文件.自签署.转换证书格式等. openssl x509工具不会使用openssl配置文件中的设定,而是完全需要自行设定或者使用该伪命令的默认值,它就像是一个完 ...
- dell服务器快速设置idrac
前提:将服务器专用的idrac网络接口,连接到网络上 1.登录到服务器(即被监控的服务器). 2.安装客户端工具 yum install OpenIPMI OpenIPMI-devel OpenI ...
- Release Python Program as exe
py2exe 可以用来将python program 以exe的形式发布. 安装py2exe 对于python 3.x pip install py2exe 可以直接安装 对于python 2.7, ...
- css 实践记录
子绝父相 https://developer.mozilla.org/zh-CN/docs/Web/CSS/position 利用子绝父相来实现一种比较老的居中方式:1.明确宽度:2.定位左边到容器的 ...
- C/C++函数指针详解(转)
这两天在研究C/C++的函数指针,找到一篇讲解比较详细的博客,内容有点多,但是讲解得比较详细,适合初学者.特转之: 1. 无处不见的函数指针 使用函数指针可以设计出更优雅的程序,比如设计一个集 ...
- CentOS6.8下安装Docker
原文章链接https://www.cnblogs.com/baolong/p/5743420.html. 由于在自己安装的虚拟机上打开linux终端命令行输入uname -a 以及cat /etc/r ...
- LeetCode03--无重复字符的最长子串
''' 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "ab ...
- 解决safari里面淘宝京东页面无法打开以及打开后乱码的问题!
sqlite3 ~/Library/Keychains/*/ocspcache.sqlite3 'DELETE FROM responses WHERE responderURI LIKE " ...