【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 ...
随机推荐
- thinkphp3.2 控制器导入模型
方法一: public function index(){ $Member = new MemberModel(); $money = $Member->Money(); print_r($mo ...
- VCL 中的 Windows API 函数(4): AdjustWindowRectEx
AdjustWindowRectEx 用在了 Forms.DBCtrls 单元. AdjustWindowRectEx 可以根据窗口样式获取的边缘尺寸. 测试: var R: TRect; beg ...
- python,如何获取字符串中的子字符串,部分字符串
说明: 比如有一个字符串,python,如何就获取前3位,或者后2位.在此记录下. 操作过程: 1.通过分割符的方式,下标的方式,获取字符串中的子串 >>> text = 'pyth ...
- [Bayesian] “我是bayesian我怕谁”系列 - Exact Inference
要整理这部分内容,一开始我是拒绝的.欣赏贝叶斯的人本就不多,这部分过后恐怕就要成为“从入门到放弃”系列. 但,这部分是基础,不管是Professor Daphne Koller,还是统计学习经典,都有 ...
- PHP易混淆函数的区别及用法汇总
本文实例分析了PHP易混淆函数的区别及用法.分享给大家供大家参考.具体分析如下: 1.echo和print的区别PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.ech ...
- Eclipse------如何将项目通过maven编译并打包
1.右击项目>>>点击Debug As>>>点击 Maven install进行编译,编译成功后入图 2.右击项目>>>点击Debug As> ...
- Ubuntu 16.04服务器 配置
1. 修改用户名称:切换到root打开如下两个配置文件 sudo vim /etc/passwd 把我想改的"xxx"这个用户名改为"way"了,保存并退出 s ...
- LHC大神问的矩阵转置问题
数学中线性代数中提到的矩阵转置,其实在我们的业务场景中也有需要的地方,比如LHC大神问到的这个问题 那么如何进行行列转换呢? 代码如下: <?php $array=array( '部门1'=&g ...
- ios开发之--使用UILabel Category 计算UILabel内容大小
在此仅做记录,代码如下:
- python运行显示编码错误
python中运行显示编码错误一般有2种原因: 编码与译码的方式不一致 在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息: SyntaxError: Non-ASCII chara ...