找出最高的木块,假设在这块木块上无限加水,就会形成一些水池,然后才向两侧溢出

用并查集维护当前在某个位置使水向左/右流动,水会流向哪个水池或从某一侧溢出浪费,当某个水池满时更新并查集

#include<cstdio>
int n,m,mx=,n2;
double ws[],hs[],sum[],al=,ar=;
int f[],ss[],sp=;
int get(int x){
int a=x,c;
while(x!=f[x])x=f[x];
while(x!=f[a])c=f[a],f[a]=x,a=c;
return x;
}
void cal(int w){
if(w<n2)f[w]=w+n2;
else f[w]=w-n2;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i){
scanf("%lf%lf",ws+i,hs+i);
if(hs[i]>hs[mx])mx=i;
}
ss[sp=]=mx;
for(int i=mx-;i;--i){
while(hs[ss[sp]]<hs[i])--sp;
ss[++sp]=i;
}
ss[++sp]=;
n2=n+;
for(int i=sp;i;--i){
for(int j=ss[i];j<ss[i-];++j)f[j+]=f[j+n2]=ss[i]+n2,sum[ss[i]]+=ws[j]*(hs[ss[i]]-hs[j]);
}
ss[sp=]=mx;
for(int i=mx+;i<=n;++i){
while(hs[ss[sp]]<hs[i])--sp;
ss[++sp]=i;
}
ss[++sp]=n+;
for(int i=sp;i;--i){
for(int j=ss[i];j>ss[i-];--j)f[j-+n2]=f[j]=ss[i],sum[ss[i]]+=ws[j]*(hs[ss[i]]-hs[j]);
}
for(int i=;i<=m;++i){
int x;double s,sl,sr;
scanf("%d%lf",&x,&s);
sl=sr=s/;
while(sl){
int w=get(x);
if(w%n2==){
al+=sl;
sl=;
}else if(w%n2==n+){
ar+=sl;
sl=;
}else if(sum[w%n2]>sl){
sum[w%n2]-=sl;
sl=;
}else{
sl-=sum[w%n2];
sum[w%n2]=;
cal(w);
}
}
while(sr){
int w=get(x+n2);
if(w%n2==){
al+=sr;
sr=;
}else if(w%n2==n+){
ar+=sr;
sr=;
}else if(sum[w%n2]>sr){
sum[w%n2]-=sr;
sr=;
}else{
sr-=sum[w%n2];
sum[w%n2]=;
cal(w);
}
}
}
printf("%.3f\n%.3f",al,ar);
return ;
}

bzoj1904: Musical Water-fence的更多相关文章

  1. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  2. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

  3. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  4. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  5. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  7. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  8. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

随机推荐

  1. 第二章 XHTML 基础

    元素与标签术语,HTML/XHTMLXHTML之间的联系区别在XHTML中,所有元素之间必须完成正确的嵌套,元素必须是闭合的,必须小写.必须有个跟元素HTML. 标题标<h1>语法:< ...

  2. javascript中值传递与值引用的研究

    今天重新看了一下<javascript高级程序设计>,其中讲到了javascript中的值传递和值引用,所以就自己研读了一下,但是刚开始没有明白函数中的参数只有值传递,有的场景好像参数是以 ...

  3. java集合类的学习(一)

    为何要用集合类:可以储存不同类型的数据,可以进行动态的删除和修改,不用考虑数组越界的问题. 软件开发常用的集合类:Vector,ArrayList,Stack,HashMap,Hashtable. 3 ...

  4. BZOJ 1597 土地购买

    斜率优化... #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

  5. UE4 创建进程,打开额外程序 方法 笔记

    FPlatformProcess::CreateProc(TEXT("程序路径");

  6. 连接WCF报EntityFramework.SqlServer 错误的解决方法

    现象: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFram ...

  7. Unity 3D 基础知识方法

    A. 组件中默认的方法有如下:            Awake,Start,Update,OnGUI,OnDisable,OnEnable,OnDestory,LateUpdate,FixedUpd ...

  8. 部署点评Cat监控项目(转)

    原文地址:http://www.bubuko.com/infodetail-986338.html 在项目中监控代码运行的状况,可以采用点评的Cat项目来监控整个项目,但是按照官方的文档来部署cat, ...

  9. Hyper-V安装Oracle Linux6_4 Oracle db 12c并使用rman做异机恢复

    本文记录在Windows Server 2012 R2上安装Oracle Enterprise Linux 6.4以及使用RMAN进行进行异机恢复的过程. Windows服务器增加Hyper-V功能 ...

  10. 测试加载jquery

    Hello world! <h1 id="main-heading">Hello world!</h1> <p><img onclick= ...