【CF903G】Yet Another Maxflow Problem 线段树
【CF903G】Yet Another Maxflow Problem
题意:一张图分为两部分,左边有n个点A,右边有m个点B,所有Ai->Ai+1有边,所有Bi->Bi+1有边,某些Ai->Bj有边,每条边都有一定的容量。
先要求你支持两种操作:
1.修改某条Ai->Ai+1的边的容量
2.询问从A1到Bm的最大流
n,m<=100000,流量<=10^9
题解:很有思维含量的题。
首先,我们把求最大流变成求最小割。容易发现,我们最多只会割一条Ai->Ai+1的边和一条Bi->Bi+1的边。
假设我们割掉了Ai->Ai+1的边,如果右边割掉的是Bj->Bj+1的边,那么我们还要割掉所有Ax->By的边(x<=i,y>j)。我们可以先预处理出对于每个i,最优的j是哪个。只需要从n到1枚举每个i,然后将从i连出去的边一条一条加进去,并用线段树维护区间加,全局最值即可。
由于每次只修改A的值,所以每个i选择的j是不变的,所以用堆维护一下最大值即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=200010;
typedef long long ll;
ll s[maxn<<2],tag[maxn<<2],f[maxn],A[maxn],B[maxn];
vector<int> C[maxn],D[maxn];
vector<int>::iterator ci,di;
int n,m,q;
struct heap
{
priority_queue<ll,vector<ll>,greater<ll> > p1,p2;
inline void erase(ll x) {p2.push(x);}
inline void push(ll x) {p1.push(x);}
inline ll top()
{
while(!p2.empty()&&p1.top()==p2.top()) p1.pop(),p2.pop();
return p1.top();
}
}p;
void build(int l,int r,int x)
{
if(l==r)
{
s[x]=B[l];
return ;
}
int mid=(l+r)>>1;
build(l,mid,lson),build(mid+1,r,rson);
s[x]=min(s[lson],s[rson]);
}
void updata(int l,int r,int x,int a,int b,int c)
{
if(a<=l&&r<=b)
{
s[x]+=c,tag[x]+=c;
return ;
}
if(tag[x]) s[lson]+=tag[x],s[rson]+=tag[x],tag[lson]+=tag[x],tag[rson]+=tag[x],tag[x]=0;
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,c);
if(b>mid) updata(mid+1,r,rson,a,b,c);
s[x]=min(s[lson],s[rson]);
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd(),q=rd();
int i,a;
for(i=1;i<n;i++) A[i]=rd(),B[i+1]=rd();
for(i=1;i<=m;i++) a=rd(),C[a].push_back(rd()),D[a].push_back(rd());
build(1,n,1);
for(i=1;i<=n;i++)
{
for(ci=C[i].begin(),di=D[i].begin();ci!=C[i].end();ci++,di++) updata(1,n,1,1,*ci,*di);
f[i]=s[1],p.push(A[i]+f[i]);
}
printf("%I64d\n",p.top());
for(i=1;i<=q;i++)
{
a=rd(),p.erase(A[a]+f[a]),A[a]=rd(),p.push(A[a]+f[a]);
printf("%I64d\n",p.top());
}
return 0;
}
【CF903G】Yet Another Maxflow Problem 线段树的更多相关文章
- Codeforces 903G Yet Another Maxflow Problem - 线段树
题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- Codeforces 803G Periodic RMQ Problem 线段树
Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the ...
- FZU2013 A short problem —— 线段树/树状数组 + 前缀和
题目链接:https://vjudge.net/problem/FZU-2013 Problem 2013 A short problem Accept: 356 Submit: 1083Ti ...
- hud-5475 An easy problem(线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- hdu 4973 A simple simulation problem. (线段树)
题目链接 题意: 给定n长的序列 m个操作 序列默认为 1, 2, 3···n 操作1:D [l,r] 把[l,r]区间增长 :( 1,2,3,4 进行 D [1,3]变成 1,1,2,2,3,3,4 ...
随机推荐
- linux echo命令提示权限不够的解决办法
该文章转载于此:http://blog.csdn.net/u010780613/article/details/51491237 问题描述: 在使用Ubuntu系统的时候,遇到这样的一个问题 ...
- level 6 - unit 2 - 情态动词May
情态动词May 正式的请求/许可 意思为可以 例如我们在机场候机室听广播的时候,在播报航班前经常听到: may i have your attention ,please ! (请注意听) 在机场过安 ...
- Python 面向对象详解
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过面向对象的编程语言,那你 ...
- c++友元函數---16
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 有些情况下,允许特定的非成员函数访问一个类的私有成员,同时仍阻止一般的访问,这是很方便做到的.例 ...
- 一个非常好的C#字符串操作处理类StringHelper.cs
/// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:http://www.sufeinet.c ...
- libevent 入门教程:Echo Server based on libevent(转)
下面假定已经学习过基本的socket编程(socket, bind, listen, accept, connect, recv, send, close),并且对异步/callback有基本的认识. ...
- NetBpm 安装篇(1)
尊重别人劳动成果 转载注明出处:http://www.cnblogs.com/anbylau2130/p/3875718.html 官方主页 http://www.netbpm.org/docs/in ...
- 在SSH框架中,如何得到POST请求的URL和参数列表
在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...
- iOS开发之-- 字符串的操作,去掉某一个字符或者替换成其他字符
一个简单的操作,记录下: NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withS ...
- CPU特性漏洞测试(Meltdown and Spectre)
2018年1月4日,国外安全研究人员披露了名为"Meltdown"和"Spectre"两组CPU特性漏洞,该漏洞波及到近20年的Intel, AMD, Qual ...