codeforces 420D Cup Trick

codeforces 420D Cup Trick

题意

题解

官方做法需要用到线段树+平衡树(? 如果数据小的话似乎可以用莫队)。然后代码好长好长。我补了一个只要用到树状数组的做法。

代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N=1000007, S=N-4;

int n,m;
int cnt[N<<1], ans[N], pre[N<<1];
bool vis[N];

void upd(int p,int c) {
	for(int i=p;i<=S+S;i+=(i&-i)) cnt[i]+=c;
}
int qry(int p) {
	int res=0;
	for(int i=p;i;i-=(i&-i)) res+=cnt[i];
	return res;
}

int main() {
	scanf("%d%d",&n,&m);
	rep(i,1,S+1) upd(i+S, 1);
	int p=S;
	rep(i,1,m+1) {
		int v, x;scanf("%d%d",&v,&x);
		int l=1, r=S+S, res=-1;
		while(l<=r) {
			int mid=l+r>>1;
			if(qry(mid)<x) {
				res=mid+1;
				l=mid+1;
			} else {
				r=mid-1;
			}
		}
		if(res>S) {
			if(vis[v]) {
				puts("-1");
				return 0;
			}
			vis[v]=1;
			ans[res-S]=v;
		} else {
			if(pre[res]!=v) {
				puts("-1");
				return 0;
			}
		}
		upd(res, -1);
		upd(p, 1);
		pre[p]=v;
		--p;
	}
	for(int i=1,j=1;i<=n;++i) if(!ans[i]) {
		while(vis[j]) ++j;
		ans[i]=j;
		vis[j]=1;
	}
	rep(i,1,n+1) printf("%d%c",ans[i]," \n"[i==n]); 
	return 0;
}
posted @ 2018-03-26 20:00  yuanyuan-97  阅读(172)  评论(0编辑  收藏  举报