题目链接: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. 筛选法 || POJ 1356 Prime Land

    英文题读不懂题==质数幂的形式给你一个数 把它减一再用质数幂的形式表示出来 *解法:质数从小到大模拟除一遍,输入有点别扭 #include <iostream> #include < ...

  2. JetBrains系列产品激活

    注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.codebeta.cn https://s.tuzhi ...

  3. PHP19 PHPStorm2018和GitHub的使用

    目的 使用GitHub的代码仓库进行项目代码托管. 准备工作 1.在GitHub注册账号 https://github.com/ 2.Start a Project 登陆后创建一个项目 3.创建版本仓 ...

  4. sqlserver 分页问题

    1.top 主要是在sql server 2000中使用,效率较差 2.row_number函数 这种方法是sql server 2005以后,支持了row_number函数后,才开始使用的. dec ...

  5. centos7 install vim8

    centos7 install vim8 Git and dependency Git: https://github.com/vim/vim # yum install -y perl-devel ...

  6. 最小生成树 Prim算法 Kruskal算法实现

    最小生成树定义 最小生成树是一副连通加权无向图中一棵权值最小的生成树. 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即,而 w(u, v) 代表此边的 ...

  7. centeros 6 远程升级ssl ssh 的shell脚本

    变量说明 SSL_N=openssl-1.0.2p #ssl 版本SSH_N=openssh-7.9p1 #ssh 版本ZLIB_N=zlib-1.2.11 # zlib 版本 脚本分为两个,因为升级 ...

  8. python-爬虫学习(文字、图片、视频)

    爬虫-文字爬取 import re import requests respone = requests.get('https://ishuo.cn/') ##获取网站url data = respo ...

  9. Django 1.8.11 REST风格路由

    # -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...

  10. js总结(二):函数、作用域和this

    function Container( properties ) { var objthis = this; for ( var i in properties ) { (function(){ // ...