QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#459071#8546. Min or Max 2zyz07WA 280ms3600kbC++174.4kb2024-06-29 21:58:382024-06-29 21:58:39

Judging History

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

  • [2024-06-29 21:58:39]
  • 评测
  • 测评结果:WA
  • 用时:280ms
  • 内存:3600kb
  • [2024-06-29 21:58:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
const int N=5e5+5;
int T,n,a[N],b[N],ans[N];
struct Info{
	int l,r;
	Info operator+(const Info& rhs) const{
		if(r<rhs.l){
			return {rhs.l,rhs.l};
		}
		if(l>rhs.r){
			return {rhs.r,rhs.r};
		}
		return {max(l,rhs.l),min(r,rhs.r)};
	}
	Info& operator+=(const Info& rhs){
		return *this=*this+rhs;
	}
};
struct SegmentTree{
	Info val[N*4];
	void init(int p=1,int L=1,int R=n){
		if(L==R){
			val[p]={1,b[L]};
			return;
		}
		int mid=(L+R)/2;
		init(p*2,L,mid);
		init(p*2+1,mid+1,R);
		val[p]=val[p*2]+val[p*2+1];
	}
	void modify(int pos,const Info& x,int p=1,int L=1,int R=n){
		if(L==R){
			val[p]=x;
			return;
		}
		int mid=(L+R)/2;
		if(pos<=mid){
			modify(pos,x,p*2,L,mid);
		}else{
			modify(pos,x,p*2+1,mid+1,R);
		}
		val[p]=val[p*2]+val[p*2+1];
	}
	void query(int l,int r,Info& ans,int p=1,int L=1,int R=n){
		if(l<=L&&R<=r){
			ans+=val[p];
			return;
		}
		int mid=(L+R)/2;
		if(l<=mid){
			query(l,r,ans,p*2,L,mid);
		}
		if(r>mid){
			query(l,r,ans,p*2+1,mid+1,R);
		}
	}
}seg;
struct SegmentTree2{
	struct Node{
		int cnt,mn,mx;
		Info tag;
	}t[N*4];
	void apply(int p,const Info& x){
		if(t[p].cnt){
			t[p].mn=clamp(t[p].mn,x.l,x.r);
			t[p].mx=clamp(t[p].mx,x.l,x.r);
		}
		t[p].tag+=x;
	}
	void push(int p){
		apply(p*2,t[p].tag);
		apply(p*2+1,t[p].tag);
		t[p].tag={0,n+1};
	}
	void pull(int p){
		t[p].cnt=t[p*2].cnt+t[p*2+1].cnt;
		t[p].mn=min(t[p*2].mn,t[p*2+1].mn);
		t[p].mx=max(t[p*2].mx,t[p*2+1].mx);
	}
	void init(int p=1,int L=1,int R=n){
		t[p]={0,int(1e9),0,{0,n+1}};
		if(L==R) return;
		int mid=(L+R)/2;
		init(p*2,L,mid);
		init(p*2+1,mid+1,R);
	}
	void modify(int pos,int mn,int mx,int p=1,int L=1,int R=n){
		if(L==R){
			t[p].cnt=1;
			t[p].mn=mn;
			t[p].mx=mx;
			return;
		}
		push(p);
		int mid=(L+R)/2;
		if(pos<=mid){
			modify(pos,mn,mx,p*2,L,mid);
		}else{
			modify(pos,mn,mx,p*2+1,mid+1,R);
		}
		pull(p);
	}
	void range_apply(int l,int r,const Info& x,int p=1,int L=1,int R=n){
		if(l<=L&&R<=r){
			apply(p,x);
			return;
		}
		push(p);
		int mid=(L+R)/2;
		if(l<=mid){
			range_apply(l,r,x,p*2,L,mid);
		}
		if(r>mid){
			range_apply(l,r,x,p*2+1,mid+1,R);
		}
		pull(p);
	}
	void query(int l,int r,int& mn,int& mx,int p=1,int L=1,int R=n){
		if(l<=L&&R<=r){
			mn=min(mn,t[p].mn);
			mx=max(mx,t[p].mx);
			return;
		}
		push(p);
		int mid=(L+R)/2;
		if(l<=mid){
			query(l,r,mn,mx,p*2,L,mid);
		}
		if(r>mid){
			query(l,r,mn,mx,p*2+1,mid+1,R);
		}
	}
}seg2;
int pos[N];
Info info[N];
void solve(){
	For(i,1,n){
		pos[a[i]]=i;
	}
	seg.init();
	For(x,1,n){
		int i=pos[x];
		info[i]={0,n+1};
		if(i<n){
			seg.query(i+1,n,info[i]);
		}
		seg.modify(i,{b[i],n});
	}
	seg2.init();
	For(i,1,n){
		if(i==1){
			seg2.modify(a[i],b[i],b[i]);
		}else{
			int mn1=1e9,mx1=0,mn2=1e9,mx2=0;
			seg2.query(1,a[i],mn1,mx1);
			seg2.query(a[i],n,mn2,mx2);
			if(mn1<int(1e9)){
				mn1=max(mn1,b[i]);
				mx1=max(mx1,b[i]);
			}
			if(mn2<int(1e9)){
				mn2=min(mn2,b[i]);
				mx2=min(mx2,b[i]);
			}
			seg2.modify(a[i],min(mn1,mn2),max(mx1,mx2));
			if(a[i]>1){
				seg2.range_apply(1,a[i]-1,{1,b[i]});
			}
			if(a[i]<n){
				seg2.range_apply(a[i]+1,n,{b[i],n});
			}
		}
		auto [L,R]=info[i];
		int mn=1e9,mx=0;
		seg2.query(a[i],a[i],mn,mx);
		if(mn<L){
			++ans[abs(a[i]-L)];
		}
		if(mx>R){
			++ans[abs(a[i]-R)];
		}
	}
}
int a_pmn[N],a_pmx[N];
int b_pmn[N],b_pmx[N];
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>T;
	while(T--){
		cin>>n;
		For(i,1,n){
			cin>>a[i];
		}
		For(i,1,n){
			cin>>b[i];
		}
		solve();
		a_pmn[0]=b_pmn[0]=1e9;
		For(i,1,n){
			a_pmn[i]=min(a_pmn[i-1],a[i]);
			a_pmx[i]=max(a_pmx[i-1],a[i]);
			b_pmn[i]=min(b_pmn[i-1],b[i]);
			b_pmx[i]=max(b_pmx[i-1],b[i]);
		}
		For(i,1,n){
			if(i==1||(a[i]>a_pmn[i-1]&&b[i]>b_pmn[i-1])||(a[i]<a_pmx[i-1]&&b[i]<b_pmx[i-1])){
				ans[abs(a[i]-b[i])]+=(info[i].l<=b[i]&&b[i]<=info[i].r);
			}
		}
		swap_ranges(a+1,a+n+1,b+1);
		solve();
		swap_ranges(a+1,a+n+1,b+1);
		For(i,0,n-1){
			cout<<ans[i]<<" \n"[i==n-1];
		}
		fill(ans,ans+n,0);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

input:

4
2
1 2
2 1
5
2 4 1 5 3
2 4 1 5 3
5
1 2 3 4 5
5 4 3 2 1
8
5 8 3 4 2 7 1 6
4 6 3 8 5 1 2 7

output:

2 0
5 0 0 0 0
2 2 2 2 0
5 5 2 2 1 0 0 0

result:

ok 20 numbers

Test #2:

score: -100
Wrong Answer
time: 280ms
memory: 3520kb

input:

66664
7
4 2 6 5 7 1 3
6 5 3 1 4 7 2
10
6 8 10 7 5 1 4 3 9 2
5 10 3 8 6 7 2 9 1 4
9
3 2 4 8 7 6 9 1 5
8 1 2 9 6 7 4 3 5
10
4 3 9 6 7 2 10 1 8 5
3 5 4 1 2 7 10 9 6 8
5
3 4 1 2 5
5 1 3 2 4
5
2 4 3 5 1
2 3 1 4 5
6
2 6 1 3 4 5
6 4 5 1 3 2
10
10 1 2 7 5 8 4 3 9 6
9 4 2 3 6 1 7 8 5 10
5
1 2 4 5 3
4 1 2 5 3...

output:

5 4 2 2 1 0 0
7 9 3 3 2 1 0 0 0 0
5 6 3 2 1 0 0 0 0
4 4 5 3 2 1 0 0 0 0
5 3 0 0 0
2 2 2 2 0
3 3 3 1 0 0
8 9 5 2 1 0 0 0 0 0
5 2 0 0 0
6 3 0 0 0 0
3 3 2 0 0
6 4 3 1 0 0 0
3 2 3 1 0 0
4 7 3 0 0 0 0
3 4 3 2 1 0 0
3 2 2 2 2 2 2 1 0
4 6 3 1 0 0 0
4 5 3 2 3 3 1 0 0 0
10 5 0 0 0 0 0 0
8 8 3 1 0 0 0 0 0 0
5...

result:

wrong answer 1st numbers differ - expected: '4', found: '5'