CF 1557D(Ezzat and Grid-线段覆盖)

Posted nike0good

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 1557D(Ezzat and Grid-线段覆盖)相关的知识,希望对你有一定的参考价值。

一个 n ∗ m n*m nm的01矩阵,初始为全0。
有m个条件 ( i , j , k ) (i,j,k) (i,j,k),表示把第 i i i [ j , k ] [j,k] [j,k]列设为1。
问至少删除几列,使对任意相邻行,至少存在一列,其值均为 1 1 1

线段覆盖,考虑每次必须在2个 ( i , j , k ) (i,j,k) (i,j,k)之间转移,且可以转移的前提是有一个位置有公共 1 1 1.

在这里插入代码片#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i>0;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \\
						For(j,m-1) cout<<a[i][j]<<' ';\\
						cout<<a[i][m]<<endl; \\
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (300000+10)
class seg{
public:
	int i,l,r;
	friend bool operator<(seg a,seg b) {
		if(a.l!=b.l) return a.l<b.l;
		return a.r<b.r;
	}
	void pri() {
		printf("(%d,%d,%d)",i,l,r);
	}
}a[MAXN];
int cmp(seg a,seg b) {
	if(a.i!=b.i) return a.i<b.i;
	if(a.l!=b.l) return a.l<b.l;
	return a.r<b.r;
}
int n,m;
set<seg> S;
int f[MAXN]={},pr[MAXN]={};
int cross(seg A,seg B) {
	bool fl=max(A.l,B.l)<=min(A.r,B.r);
	if(fl &&A.i!=B.i) {
		if(A.i>B.i) swap(A,B);
		if(f[A.i]+1>f[B.i]) {
			gmax(f[B.i],f[A.i]+1)
			pr[B.i]=A.i;	
		}
	}
	return fl;
}
bool b[MAXN]={};
int main()
{
//	freopen("d.in","r",stdin);
//	freopen(".out","w",stdout);
	n=read();m=read();
	For(i,n) f[i]=1,pr[i]=i;
    For(i,m) {
    	a[i].i=read();
    	a[i].l=read();
    	a[i].r=read();
	}
	sort(a+1,a+1+m,cmp);
	For(i,m) {
		seg now=a[i];
		set<seg>::iterator it2,it=S.upper_bound(seg{0,a[i].l,INF});
		if(it!=S.begin()) {
			it--;
		}
		
		if(it!=S.end()) {
			it2=it; ++it2;
			if(cross(*it,now)) {
				seg iit=*it;
				S.erase(it);
				if(iit.l<now.l && now.l<=iit.r) {
					S.insert(seg{iit.i,iit.l,now.l-1});
				}
				if(now.r<iit.r && now.r>=iit.l) {
					S.insert(seg{iit.i,now.r+1,iit.r});
				}
			}	
			it=it2;
			while(it!=S.end()) {
				if(!cross((*it),now)) break;
				{
					it2=it; ++it2;
					seg iit=*it;
					S.erase(it);
					if(iit.l<now.l && now.l<=iit.r) {
						S.insert(seg{iit.i,iit.l,now.l-1});
					}
					if(now.r<iit.r && now.r>=iit.l) {
						S.insert(seg{iit.i,now.r+1,iit.r});
					}
					it=it2;
				}				
			}		
						
		}
		S.insert(now);
	}
	int ans=-1,id=0;For(i,n) {
		if(ans<f[i])ans=f[i],id=i;	
	}
	cout<<n-ans<<endl;
	b[id]=1;
	while(pr[id]!=id) {
		id=pr[id];b[id]=1;
	}
	bool fl=0;
	For(i,n) if(!b[i]) {
		if(!fl) fl=1;else putchar(' ');
		printf("%d",i);
	}
	
	return 0;
}


以上是关于CF 1557D(Ezzat and Grid-线段覆盖)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 1557D Ezzat and Grid(线段树+dp)

CF1557D.Ezzat and Grid(2200分线段树)

cf1557D. Ezzat and Grid

Codeforces Round #737 (Div. 2) Ezzat and Grid(线段树优化dp)

CF #737(div2)A. Ezzat and Two Subsequences,贪心

Codeforces Round #737 (Div. 2) D. Ezzat and Grid(线段树,dp)