QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#429468#8567. Cheap Constructionucup-team2526#WA 1ms3832kbC++202.6kb2024-06-02 15:50:072024-06-02 15:50:07

Judging History

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

  • [2024-06-02 15:50:07]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3832kb
  • [2024-06-02 15:50:07]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) ((x)&-(x))
using namespace std;
#define dbg(x...) \
do { \
	cout << #x << " -> "; \
	err(x); \
} while (0)
 
void err() {
	cout << endl;
}
 
template<class T, class... Ts>
void err(T arg, Ts ... args) {
	cout << fixed << setprecision(10) << arg << ' ';
	err(args...);
}
void solve(){
	int n;cin>>n;
	vector<int>a(2*n+1);
	for(int i=1;i<=2*n;i++)cin>>a[i];
	vector<int>c(n+1);
	{
		vector<int>tr(4*n);
		auto bulid = [&](auto self,int l,int r,int p)->void{
			if(l==r){
				tr[p]=1;
				return ;
			}
			int mid=(l+r)>>1;
			self(self,l,mid,p<<1);
			self(self,mid+1,r,p<<1|1);
			tr[p]=tr[p<<1]+tr[p<<1|1];
			return ;
		};
		auto updata = [&](auto self,int l,int r,int x,int val,int p)->void{
			if(l==r){
				tr[p]=0;
				return ;
			}
			int mid=(l+r)>>1;
			if(x<=mid)self(self,l,mid,x,val,p<<1);
			else self(self,mid+1,r,x,val,p<<1|1);
			tr[p]=tr[p<<1]+tr[p<<1|1];
		};
		auto query = [&](auto self,int l,int r,int k,int p)->int{
			if(l==r){
				return l;
			}
			int mid=(l+r)>>1;
			int num=tr[p<<1];
			if(num>=k)return self(self,l,mid,k,p<<1);
			else return self(self,mid+1,r,k-num,p<<1|1);
		};
		bulid(bulid,1,n,1);
		for(int i=2*n;i>=1;i-=2){
			int p=a[i-1];
			int h=a[i];
			int ps=query(query,1,n,p,1);
			c[ps]=h;
			updata(updata,1,n,ps,0,1);
		}
	}
	vector st(25,vector<int>(n+1));
	vector pose(25,vector<int>(n+1));
	for(int i=1;i<=n;i++){
		st[0][i]=c[i];
		pose[0][i]=i;
	}
	for(int s=1;(1ll<<s)<=n;s++){
		for(int i=1;i+(1ll<<s)<=n+1;i++){
			if(st[s-1][i]>st[s-1][i+(1ll<<(s-1))]){
				pose[s][i]=pose[s-1][i+(1ll<<(s-1))];
			}else{
				pose[s][i]=pose[s-1][i];
			}
			st[s][i]=min(st[s-1][i],st[s-1][i+(1ll<<(s-1))]);
		}
	}
	auto get = [&](int l,int r)->int{
		int len=r-l+1;
		int d=__lg(len);
		int pp;
		if(st[d][r-(1ll<<d)+1]<=st[d][l]){
			pp=pose[d][r-(1ll<<d)+1];
		}else pp=pose[d][l];
		return pp;
	};
	vector<int>f(n+1);
	auto Set = [&](int x)->void{
		while(x<=n){
			f[x]++;
			x+=lowbit(x);
		}
		return ;
	};
	auto ask = [&](int x)->int{
		int res=0;
		while(x){
			res+=f[x];
			x-=lowbit(x);
		}
		return res;
	};
	auto Query = [&](int l,int r)->int{
		return ask(r)-ask(l-1);
	};
	auto dfs = [&](auto &&self, int l, int r)->void{
		if(l > r) return ;
		int p = get(l, r);
		Set(p);
		std::cout << std::format("{} {}\n", Query(1, p - 1) + 1, c[p]);
		if(p != l) self(self, l, p - 1);
		if(p != r) self(self, p + 1, r);

	};
	dfs(dfs, 1, n);
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t=1;cin>>t;
	while(t--)solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
3
1 1
2 2
1 3

output:

1 1
1 3
3 2

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3832kb

input:

2
3
1 1
1 2
3 1
3
1 3
2 1
3 3

output:

1 1
1 2
3 1
1 1
1 3
3 3

result:

wrong answer 2nd lines differ - expected: '1 1', found: '1 2'