QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#30510#2559. Endless RoadY25tWA 3ms60600kbC++203.0kb2022-04-29 16:28:122022-04-29 16:28:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 16:28:14]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:60600kb
  • [2022-04-29 16:28:12]
  • 提交

answer

#include<bits/stdc++.h>
#define N 500005

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

std::map<int,int> vl,vr;
int val[N],tot;

int T[N];
inline int lb(int x){
	return x&-x;
}
inline void add(int x,int d){
	for(;x<tot;x+=lb(x))
		T[x]+=d;
}
inline int sum(int x){
	int res=0;
	for(x--;x;x-=lb(x))
		res+=T[x];
	return res;
}

int f[N];
inline int fnd(int x){
	return f[x]?f[x]=fnd(f[x]):x;
}
inline void del(int i){
	for(int x=fnd(a[i]);x<b[i];x=fnd(x))
		add(x,-(val[x+1]-val[x])),f[x]=fnd(x+1);
}
inline int len(int i){
	return sum(b[i])-sum(a[i]);
}

struct sq{
	int xl,xr,yl,yr;
	sq(int x=0,int y=0,int z=0,int w=0){
		xl=x,xr=y,yl=z,yr=w;
	}
};
struct node{
	int mn,mxl,mnr;
	node(int x=0,int y=0,int z=0){
		mn=x,mxl=y,mnr=z;
	}
};
node operator + (node x,node y){
	if(!x.mn||!y.mn)
		return x.mn?x:y;
	return node(std::min(x.mn,y.mn),a[x.mxl]>a[y.mxl]?x.mxl:y.mxl,b[x.mnr]<b[y.mnr]?x.mnr:y.mnr);
}
struct tr{
	sq s;
	node x;
};

tr t[N<<2];
inline void build(int p,std::vector<int> V,int o){
	if((int)V.size()==1){
		int i=V.back();
		t[p].s=sq(a[i],a[i],b[i],b[i]);
		t[p].x=node(i,i,i);
		return;
	}
	int mid=V.size()>>1;
	std::nth_element(V.begin(),V.begin()+mid,V.end(),[&](int i,int j){
		return o?a[i]<a[j]:b[i]<b[j];
	});
	build(p<<1,std::vector<int>(V.begin(),V.begin()+mid),o^1);
	build(p<<1|1,std::vector<int>(V.begin()+mid,V.end()),o^1);
	auto uni=[&](sq x,sq y)->sq{
		return {std::min(x.xl,y.xl),std::max(x.xr,y.xr),
				std::min(x.yl,y.yl),std::max(x.yr,y.yr)};
	};
	t[p].s=uni(t[p<<1].s,t[p<<1|1].s);
	t[p].x=t[p<<1].x+t[p<<1|1].x;
}
inline void ers(int p,int x,int y){
	if(x<t[p].s.xl||x>t[p].s.xr||y<t[p].s.yl||y>t[p].s.yr)
		return;
	if(t[p].s.xl==t[p].s.xr&&t[p].s.yl==t[p].s.yr)
		return t[p].x=node(),void();
	ers(p<<1,x,y),ers(p<<1|1,x,y);
	t[p].x=t[p<<1].x+t[p<<1|1].x;
}
inline node sum(int p,sq s){
	if(t[p].s.xl>s.xr||t[p].s.xr<s.xl||t[p].s.yl>s.yr||t[p].s.yr<s.yl)
		return node();
	if(s.xl<=t[p].s.xl&&t[p].s.xr<=s.xr&&s.yl<=t[p].s.yl&&t[p].s.yr<=s.yr)
		return t[p].x;
	return sum(p<<1,s)+sum(p<<1|1,s);
}

#define pii std::pair<int,int>
std::priority_queue<pii,std::vector<pii>,std::greater<pii>> q;
bool vis[N];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vl[a[i]]++,vl[b[i]]++;
	}
	for(auto &[x,y]:vl){
		while(y--)
			val[++tot]=x;
		y=tot;
	}
	vr=vl;
	for(int i=1;i<=n;i++)
		a[i]=vl[a[i]]--,b[i]=vl[b[i]]--;
	for(int i=1;i<tot;i++)
		add(i,val[i+1]-val[i]);
	std::vector<int> V(n);
	std::iota(V.begin(),V.end(),1);
	build(1,V,0);
	for(int i=1;i<=n;i++)
		q.emplace(len(i),i);
	while(q.size()){
		int x=q.top().second;
		q.pop();
		if(vis[x])
			continue;
		printf("%d ",x);
		vis[x]=1,del(x),ers(1,a[x],b[x]);
		int y=sum(1,sq(1,vr[a[x]],vl[b[x]],tot)).mn;
		if(y)
			q.emplace(len(y),y);
		y=sum(1,sq(1,vr[a[x]],1,vr[b[x]])).mxl;
		if(y)
			q.emplace(len(y),y);
		y=sum(1,sq(vl[a[x]],tot,vl[b[x]],tot)).mnr;
		if(y)
			q.emplace(len(y),y);
	}
	puts("");
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 60600kb

input:

6
1 2
2 3
3 4
4 5
1 3
3 5

output:

1 2 3 5 4 6 

result:

wrong answer 3rd words differ - expected: '5', found: '3'