MLE 求调
查看原帖
MLE 求调
1129446
nksunhaolan楼主2024/12/25 17:06
#include<bits/stdc++.h>
#define mxup 1000000000
using namespace std;

const int N=6e6+5,M=7e7+5,K=3e5+5;
int n,s,m,k,dis[N],A[N],in[N];
int idx,root,head[N],cnt;
int B[K];bool vis[N],dq[N];
struct nnd {
	int ls,rs;
}tree[N];
struct mmb {
	int to,next,w;
}eg[N];
void init(){
	idx=n;
	for(int i=1;i<N;i++){
		dis[i]=mxup;
		head[i]=-1;
	}
}
inline void add(const int& from,const int& to,const int& w){
	in[to]++;
	eg[cnt].to=to;eg[cnt].w=w;
	eg[cnt].next=head[from];
	head[from]=cnt++;
}
void build_tree(int& p,const int& x,const int& y){
	if(x==y){
		p=x;
		return;
	}
	if(!p)p=++idx;
	int mid=x+y>>1;
	build_tree(tree[p].ls,x,mid);
	build_tree(tree[p].rs,mid+1,y);
	add(p,tree[p].ls,0);
	add(p,tree[p].rs,0);
}
void change(const int& p,const int& x,const int& y,const int& l,const int& r){
	if(x<=l && r<=y){
		for(int i=1;i<=k;i++){
			add(B[i],p,1);
		}
		return;
	}
	int ls=tree[p].ls,rs=tree[p].rs,mid=l+r>>1;
	if(x<=mid){
		change(ls,x,y,l,mid);
	}
	if(mid<y){
		change(rs,x,y,mid+1,r);
	}
}
queue<int> q;bool noi;
inline void dfs(const int& x){
	vis[x]=1;dq[x]=1;
	for(int i=head[x];i!=-1;i=eg[i].next){
		int to=eg[i].to;
		if(dq[to])noi=1;
		if(noi)return;
		if(!vis[to])dfs(to);
		if(noi)return;
	}
	dq[x]=0;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	cin>>n>>s>>m;
	int x,z;init();
	for(int i=1;i<=s;i++){
		cin>>x>>z;
		A[x]=dis[x]=z;
	}
	build_tree(root,1,n);
	for(int i=1;i<=m;i++){
		int l,r;cin>>l>>r>>k;
		for(int j=1;j<=k;j++){
			cin>>B[j];
		}
		for(int j=2;j<=k;j++){
			if(B[j-1]+1<=B[j]-1)change(root,B[j-1]+1,B[j]-1,1,n);
		} 
		if(l<=B[1]-1)change(root,l,B[1]-1,1,n);
		if(B[k]+1<=r)change(root,B[k]+1,r,1,n);
	}
	for(int i=1;i<=idx;i++){
		if(!vis[i]){
			dfs(i);
			if(noi){
				cout<<"NIE";
				return 0;
			}
		}
		if(!in[i])q.push(i);
	}
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=head[x];i!=-1;i=eg[i].next){
			int to=eg[i].to,w=eg[i].w;
			if(A[to]>dis[x]-w){
				cout<<"NIE";
				return 0;
			}
			dis[to]=min(dis[to],dis[x]-w);
			in[to]--;
			if(!in[to])q.push(to);
			if(dis[to]<1){
				cout<<"NIE";
				return 0;
			}
		}
	}
	cout<<"TAK\n";
	for(int i=1;i<=n;i++){
		cout<<dis[i]<<" ";
	}
}
2024/12/25 17:06
加载中...