题目链接: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 (线段树)的更多相关文章

  1. ZOJ 2706 Thermal Death of the Universe

    Thermal Death of the Universe Time Limit: 10 Seconds                                     Memory Limi ...

  2. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  3. 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 ...

  4. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  5. 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 ...

  6. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  7. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  8. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  9. ZOJ 1859 Matrix Searching(二维线段树)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...

随机推荐

  1. java登录拦截Filter

    此例子为一个简单的登录拦截. 首先在web.xml中配置拦截类. <filter-mapping> <filter-name>SessionFilter</filter- ...

  2. java 数据库

    1.数据的概述 数据(data)是事实或观察的结果,是对客观事物的逻辑归纳,是用于表示客观事物的未经加工的的原始素材. 数据是信息的表现形式和载体,可以是符号.文字.数字.语音.图像.视频等.数据和信 ...

  3. sublime text 3 安装Nodejs插件

    如题 1)集成Nodejs插件到sublime,地址:https://github.com/tanepiper/SublimeText-Nodejs2)解压zip文件, 并重命名文件夹“Nodejs” ...

  4. IOS学习笔记37——ViewController生命周期详解

    在我之前的学习笔记中讨论过ViewController,过了这么久,对它也有了新的认识和体会,ViewController是我们在开发过程中碰到最多的朋友,今天就来好好认识一下它.ViewContro ...

  5. Android Studio中出现Gradle's dependency cache may be corrupt错误的解决办法

    起因 某次打开AS,提示升级AS,升级后,提示升级gradle,选择升级. 结果在升级gradle时耗时较久,没有耐心,点击停止升级gradle, 还是停在那里,然后关闭AS,还是没反应,启动任务管理 ...

  6. Ubuntu 开机出现 "Your system is running in low-graphics mode"

    Ubuntu 开机出现 "Your system is running in low-graphics mode" 可能是权限问题 按网上的方法发现sudo命令无法使用,且系统变为 ...

  7. python字符串的格式化

    # -*- coding:utf-8 -*- """ @Author:janson @Date:2018/8/1 @File:StrFormat.py "&qu ...

  8. <c:foreach> <c:forTokens>

    <c:choose>标签与Java switch语句的功能一样,用于在众多选项中做出选择. switch语句中有case,而<c:choose>标签中对应有<c:when ...

  9. C++实现链队类——合肥工业大学数据结构实验5:链式队列

    实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 5.2 实验要求 5.2.1链队列实验要 ...

  10. bootspring网站项目,Date类型插入数据库始终比正确时间早一天问题的解决

    bug描述 昨天的Date插入不进去问题解决后,一直没发现其实插入的时间一直比正确的时间早一天 输出sql语句,发现insert语句还是对的,不知道为什么插入数据库之后结果就早了一天 https:// ...