首页 > 其他分享 >CF793G Oleg and chess 题解

CF793G Oleg and chess 题解

时间:2023-02-11 13:33:12浏览次数:48  
标签:dep CF793G return int 题解 flow Oleg const include

\(\text{类似于主席树优化建图,直接放一张图片。}\)

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=3e4+5;
const int oo=5e6;
struct node{
	int nxt;int to;int flow;
}e[N*500];
int head[N*60],tot;
int cur[N*60],dep[N*60];
int id[N*4],tag[N*4];bool islf[N*4];int lid[N*4];
int n,m,S,T,idx;int xa,ya,xb,yb;
struct Mnode{
	int x;int y;int z;
	inline bool operator < (const Mnode &t)const{
		return (t.x^x?t.x<x:(t.y^y?t.y<y:t.z<z));
	}
};
struct Tp{
	int l;int r;int v;
}tmp;
vector<Tp> opt[N];
queue<int> Q;
inline void read(int &x) 
{
	int f=1;char c;
	for(x=0,c=getchar();c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48); x*=f;
} 
inline int mn(int _x,int _y){return _x<_y?_x:_y;}
inline int mx(int _x,int _y){return _x>_y?_x:_y;}
inline int ab(int _x){return _x<0?-_x:_x;}
inline void add(int from,int to,int flow){
	e[++tot].to=to;e[tot].flow=flow;
	e[tot].nxt=head[from];head[from]=tot;
}
inline void lnk(int from,int to,int flow){
	add(from,to,flow);add(to,from,0);return ;
}
inline void pushup(int x){
	if(tag[x]) return ;
	if(islf[x]){lnk(id[x],lid[x]+n,oo);return ;}
	else{
		if(!tag[x<<1])lnk(id[x],id[x<<1],oo);
		if(!tag[x<<1|1]) lnk(id[x],id[x<<1|1],oo);
	}
	return ;
}
inline void build(int x,int l,int r){
	id[x]=++idx;
	if(l==r){islf[x]=true;lid[x]=l;lnk(idx,lid[x]+n,oo);return ;}
	int mid=l+r>>1;
	build(x<<1,l,mid);build(x<<1|1,mid+1,r);
	pushup(x);return ;
}
inline void update(int x,int l,int r,int ql,int qr,int v){
	id[x]=++idx;
	if(ql<=l&&r<=qr){
		tag[x]+=v;pushup(x);return ;
	}
	int mid=l+r>>1;
	if(ql<=mid) update(x<<1,l,mid,ql,qr,v);
	if(qr>mid) update(x<<1|1,mid+1,r,ql,qr,v);
	pushup(x);return ;
}

inline bool bfs(int s,int t){
	for(int i=0;i<=idx;i++){
		cur[i]=head[i];dep[i]=-1;
	}
	while(!Q.empty()) Q.pop();
	dep[s]=0;Q.push(s);
	while(!Q.empty()){
		int tp=Q.front();Q.pop();
		for(int i=head[tp];i;i=e[i].nxt){
			int v=e[i].to;
			if(dep[v]==-1&&e[i].flow>0){
				dep[v]=dep[tp]+1;
				Q.push(v);
				if(v==t) return true;
			}
		}
	}
	if(dep[t]==-1) return false;
	else return true;
}
inline int dfs(int x,int flow,int t){
	if(x==t||flow<=0) return flow;
	int overflow=flow,tmp;
	for(int &i=cur[x];i;i=e[i].nxt){
		int v=e[i].to;
		if(dep[v]==dep[x]+1&&e[i].flow>0){
			tmp=dfs(v,mn(flow,e[i].flow),t);
			if(tmp<=0) dep[v]=-1;
			flow-=tmp;e[i].flow-=tmp;
			e[i^1].flow+=tmp;
			if(flow<=0) break;
		}
	}
	return overflow-flow;
}
inline int Dinic(int s,int t){
	int rest=0;
	while(bfs(s,t)) rest+=dfs(s,oo,t);
	return rest;
}
inline bool cmp(Tp p,Tp q){
	return p.v>q.v;
}
int main()
{
	read(n);read(m);
	S=0;T=2*n+1;idx=2*n+1;tot=1;
	for(int i=1;i<=n;i++) lnk(S,i,1);
	for(int i=1;i<=n;i++) lnk(i+n,T,1);
	build(1,1,n);
	for(int i=1;i<=m;i++){
		read(xa);read(ya);read(xb);read(yb);
		opt[ya].push_back((Tp){xa,xb,1});
		opt[yb+1].push_back((Tp){xa,xb,-1});
	}
	for(int i=1;i<=n;i++){
		sort(opt[i].begin(),opt[i].end(),cmp);
		for(int j=0;j<opt[i].size();j++){
			tmp=opt[i][j];
			update(1,1,n,tmp.l,tmp.r,tmp.v);
		}
		if(!tag[1]) lnk(i,id[1],1);
	}
	printf("%d\n",Dinic(S,T));
	return 0;
}  

标签:dep,CF793G,return,int,题解,flow,Oleg,const,include
From: https://www.cnblogs.com/infinite-nebula/p/17111300.html

相关文章

  • 【题解】CF997C Sky Full of Stars
    简记一下高维二项式反演的套路。思路高维二项式反演。首先意识到\(n\leq10^6\)且计数,并且求“至少”,所以考虑用二项式反演处理。这里如果用一维的二项式反演,可能......
  • 【题解】P4464 [国家集训队] JZPKIL
    仙气飘飘莫反题。显现出了很多推式子习惯的问题。思路莫反+伯努利数+Pr。首先根据\(\operatorname{lcm}(x,y)=\frac{xy}{\gcd(x,y)}\)可以化简:\(\sum\limits......
  • P9033题解
    P9033「KDOI-04」XORSum题解题目链接传送门题意简述构造一个长度为\(n\),值域为\([0,m]\)的异或和为\(k\)的序列,如果不存在则输出\(-1\)。题目分析首先很容易......
  • CF1268B题解
    CF1268B题解题目翻译给你一个杨表,用一个有\(n\)个元素的数组\(a\)表示杨表每一列的高度。你需要用\(1\times2\)或\(2\times1\)的骨牌填充这个杨表,求出最多......
  • 题解:[PA2021] Drzewo czerwono-czarne
    题目链接:[PA2021]Drzewoczerwono-czarne首先对于起始和终止相同以及起始中只有一种颜色并且终止和起始不相同这两种情况是平凡的。考虑最后一步,一定是将某一条边上的一......
  • CF1296D 题解
    题目传送门简单题做了好久,哈哈。题目分析首先,对于单个怪物,先将它的血量通过取余处理到小于\(a+b\)的时候,因为无论怪物血量多少,如果大于\(a+b\),显然不可能出现最后一......
  • 关于node-sass和sass-loader版本不兼容的问题解决
    安装node-sass和sass-loader时,提示我版本不兼容如:ValidationError:Invalidoptionsobject.SassLoaderhasbeeninitializedusinganoptionsobjectth......尝试......
  • 问题解决:WARNING!The remote SSH server rejected X11 forwarding request.
    截图解决X11forwarding依赖xorg-x11-xauth软件包,安装xorg-x11-xauth软件包。yuminstallxorg-x11-xauth-y安装后重新连接即可......
  • 【题解】P5278 算术天才⑨与等差数列
    有趣的乱搞做法和一个没想到的trick,一起记一下。思路线段树+哈希/trick.首先是乱搞做法。意识到可以像P3792由乃与大母神原型和偶像崇拜那个被疯狂hack的题......
  • Codeforces Round #851 (Div. 2) 题解
    CodeforcesRound#851(Div.2)题解A.OneandTwo取\(\log_2\),变成加号,前缀和枚举\(s[i]=\dfrac{s[n]}{2}\)。B.SumofTwoNumbers对于每一位,如果是偶数则平均......