其他分享
首页 > 其他分享> > 题解 切题

题解 切题

作者:互联网

传送门

暴力的话其实是网络流的板子,但我没有看出来
于是想了一个贪心的部分分解法,每次取余量最多的b

正解的话
首先若是check一个固定的 \(a, b\),就是check能否满流

特别注意一下我把 \(n, m\) 打反了能有95pts就离谱,调了一下午才发现

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 250010
#define ll long long
#define fir first
#define sec second 
#define make make_pair
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m, q;
int a[N], b[N];

namespace task1{
	int sta[N], top;
	priority_queue<int> q2;
	bool check() {
		top=0;
		while (q2.size()) q2.pop();
		for (int i=1; i<=m; ++i) if (b[i]) q2.push(b[i]);
		for (int i=1; i<=n; ++i) {
			for (int j=1,tem; j<=a[i]; ++j) {
				if (q2.empty()) return 0;
				tem=q2.top(); q2.pop();
				if (--tem>0) sta[++top]=tem;
			}
			while (top) q2.push(sta[top--]);
		}
		return 1;
	}
	void solve() {
		q=read();
		for (int i=1,op,x; i<=q; ++i) {
			// cout<<"i: "<<i<<endl;
			op=read(); x=read();
			if (op==1) ++a[x];
			else if (op==2) --a[x];
			else if (op==3) ++b[x];
			else --b[x];
			puts(check()?"1":"0");
		}
	}
}

namespace task{
	int tl[N<<2], tr[N<<2], dat[N<<2], tag[N<<2], a2[N], bit[N], c[N], sum[N];
	#define tl(p) tl[p]
	#define tr(p) tr[p]
	#define dat(p) dat[p]
	#define tag(p) tag[p]
	#define pushup(p) dat(p)=min(dat(p<<1), dat(p<<1|1))
	inline void upd(int i, int dat) {i+=2; for (; i; i-=i&-i) bit[i]+=dat;}
	inline int query(int i) {i+=2; int ans=0; for (; i<N; i+=i&-i) ans+=bit[i]; return ans;}
	void spread(int p) {
		if (!tag(p)) return ;
		dat(p<<1)+=tag(p); tag(p<<1)+=tag(p);
		dat(p<<1|1)+=tag(p); tag(p<<1|1)+=tag(p);
		tag(p)=0;
	}
	void build(int p, int l, int r) {
		tl(p)=l; tr(p)=r; dat(p)=-INF;
		if (l==r) {dat(p)=sum[l]; return ;}
		int mid=(l+r)>>1;
		build(p<<1, l, mid);
		build(p<<1|1, mid+1, r);
		pushup(p);
	}
	void upd(int p, int l, int r, int val) {
		// if (l>r) cout<<"lr: "<<l<<' '<<r<<endl;
		// assert(l<=r);
		if (l>r) return ;
		if (l<=tl(p) && r>=tr(p)) {dat(p)+=val; tag(p)+=val; return ;}
		spread(p);
		int mid=(tl(p)+tr(p))>>1;
		if (l<=mid) upd(p<<1, l, r, val);
		if (r>mid) upd(p<<1|1, l, r, val);
		pushup(p);
	}
	void show(int p) {
		if (tl(p)==tr(p)) {printf("%d ", dat(p)); return ;}
		spread(p);
		show(p<<1); show(p<<1|1);
	}
	void solve() {
		for (int i=1; i<=n; ++i) a2[i]=a[i], upd(a[i], 1);
		sort(a2+1, a2+n+1, [](int a, int b){return a>b;});
		int limb=0;
		for (int i=1; i<=m; ++i) ++c[b[i]], limb=max(limb, b[i]);
		for (int i=limb; i; --i) c[i]+=c[i+1];
		for (int i=1; i<=n; ++i) sum[i]=sum[i-1]+c[i]-a2[i];
		build(1, 1, n);
		q=read();
		// cout<<"show: "; show(1); printf("\n");
		for (int i=1,op,x,tem; i<=q; ++i) {
			// cout<<"i: "<<i<<endl;
			op=read(); x=read();
			if (op==1) {
				upd(1, query(a[x]+1)+1, n, -1);
				upd(a[x], -1); upd(++a[x], 1);
			}
			else if (op==2) {
				upd(1, query(a[x]), n, 1);
				upd(a[x], -1); upd(--a[x], 1);
			}
			else if (op==3) upd(1, ++b[x], n, 1);
			else upd(1, b[x]--, n, -1);
			puts((dat(1)>=0)?"1":"0");
			// cout<<"show: "; show(1); printf("\n");
		}
	}
}

signed main()
{
	freopen("problem.in", "r", stdin);
	freopen("problem.out", "w", stdout);

	n=read(); m=read();
	for (int i=1; i<=n; ++i) a[i]=read();
	for (int i=1; i<=m; ++i) b[i]=read();
	task::solve();
	
	return 0;
}

标签:满流,q2,limits,int,题解,sum,切题,define
来源: https://www.cnblogs.com/narration/p/15366870.html