Submission #425451

# Submission time Handle Problem Language Result Execution time Memory
425451 2021-06-13T03:59:14 Z errorgorn RMQ (NOI17_rmq) C++17
100 / 100
668 ms 32528 KB
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

struct Q{
	ll l,r,val;
	
	Q(int a,int b,int c){
		l=a,r=b,val=c;
	}
};

struct node{
	int s,e,m;
	ii val;
	ll lazy=0;
	node *l,*r;
	
	node (int _s,int _e){
		s=_s,e=_e,m=s+e>>1;
		val=ii(0,s);
		
		if (s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void propo(){
		if (lazy){
			val.fi+=lazy;
			if (s!=e){
				l->lazy+=lazy;
				r->lazy+=lazy;
			}
			lazy=0;
		}
	}
	
	void update(int i,int j,ll k){
		if (s==i && e==j) lazy+=k;
		else{
			if (j<=m) l->update(i,j,k);
			else if (m<i) r->update(i,j,k);
			else l->update(i,m,k),r->update(m+1,j,k);
			
			l->propo(),r->propo();
			val=min(l->val,r->val);
		}
	}
	
	ii query(int i,int j){
		propo();
		
		if (s==i && e==j) return val;
		else if (j<=m) return l->query(i,j);
		else if (m<i) return r->query(i,j);
		else return min(l->query(i,m),r->query(m+1,j));
	}
} *root=new node(0,100005);

struct node2{
	int s,e,m;
	int mx=-1;
	node2 *l,*r;
	
	node2 (int _s,int _e){
		s=_s,e=_e,m=s+e>>1;
		
		if (s!=e){
			l=new node2(s,m);
			r=new node2(m+1,e);
		}
	}
	
	void update(int i,int j,int k){
		if (s==i && e==j) mx=max(mx,k);
		else if (j<=m) l->update(i,j,k);
		else if (m<i) r->update(i,j,k);
		else l->update(i,m,k),r->update(m+1,j,k);
	}
	
	int query(int i){
		if (s==e) return mx;
		else if (i<=m) return max(mx,l->query(i));
		else return max(mx,r->query(i));
	}
} *root2=new node2(0,100005);


int n,q;

ii range[100005];
int cnt[100005];

int ans[100005];

void rage(){
	rep(x,0,n) cout<<"-1 ";
	exit(0);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	memset(ans,-1,sizeof(ans));
	
	cin>>n>>q;
	
	rep(x,0,n) range[x]=ii(-1,100005);
	
	vector<Q> qu;
	
	ll a,b,c;
	while (q--){
		cin>>a>>b>>c;
		
		qu.push_back(Q(a,b,c));
		root->update(a,b,1);
		root2->update(a,b,c);
		
		range[c].fi=max(range[c].fi,a);
		range[c].se=min(range[c].se,b);
		cnt[c]++;
	}
	
	sort(all(qu),[](Q i,Q j){
		return i.val>j.val;
	});
	
	while (!qu.empty()){
		int lo=qu.back().val;
		
		if (range[lo].fi>range[lo].se) rage();
		
		ii temp=root->query(range[lo].fi,range[lo].se);
		
		//cout<<temp.fi<<" "<<temp.se<<endl;
		
		if (temp.fi!=cnt[lo]) rage();
		
		int pos=temp.se;
		
		ans[pos]=lo;
		
		while (!qu.empty() && qu.back().val==lo){
			root->update(qu.back().l,qu.back().r,-1);
			qu.pop_back();
		}
	}
	
	
	vector<int> proc;
	set<int> avail;
	
	rep(x,0,n) avail.insert(x);
	rep(x,0,n){
		if (ans[x]==-1) proc.push_back(x);
		else avail.erase(ans[x]);
	}
	
	sort(all(proc),[](int i,int j){
		return root2->query(i)<root2->query(j);
	});
	
	for (auto &it:proc){
		if (*avail.begin()<root2->query(it)) rage();
		
		ans[it]=*avail.begin();
		avail.erase(*avail.begin());
	}
	
	rep(x,0,n) cout<<ans[x]<<" ";
}

Compilation message

rmq.cpp: In constructor 'node::node(int, int)':
rmq.cpp:49:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |   s=_s,e=_e,m=s+e>>1;
      |               ~^~
rmq.cpp: In constructor 'node2::node2(int, int)':
rmq.cpp:97:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   97 |   s=_s,e=_e,m=s+e>>1;
      |               ~^~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 22604 KB Output is correct
2 Correct 23 ms 22596 KB Output is correct
3 Correct 20 ms 22624 KB Output is correct
4 Correct 21 ms 22628 KB Output is correct
5 Correct 21 ms 22604 KB Output is correct
6 Correct 27 ms 22528 KB Output is correct
7 Correct 27 ms 22552 KB Output is correct
8 Correct 22 ms 22512 KB Output is correct
9 Correct 21 ms 22596 KB Output is correct
10 Correct 22 ms 22604 KB Output is correct
11 Correct 22 ms 22572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 22604 KB Output is correct
2 Correct 23 ms 22596 KB Output is correct
3 Correct 20 ms 22624 KB Output is correct
4 Correct 21 ms 22628 KB Output is correct
5 Correct 21 ms 22604 KB Output is correct
6 Correct 27 ms 22528 KB Output is correct
7 Correct 27 ms 22552 KB Output is correct
8 Correct 22 ms 22512 KB Output is correct
9 Correct 21 ms 22596 KB Output is correct
10 Correct 22 ms 22604 KB Output is correct
11 Correct 22 ms 22572 KB Output is correct
12 Correct 28 ms 22632 KB Output is correct
13 Correct 28 ms 22664 KB Output is correct
14 Correct 25 ms 22592 KB Output is correct
15 Correct 22 ms 22728 KB Output is correct
16 Correct 23 ms 22664 KB Output is correct
17 Correct 25 ms 22708 KB Output is correct
18 Correct 22 ms 22652 KB Output is correct
19 Correct 21 ms 22580 KB Output is correct
20 Correct 26 ms 22604 KB Output is correct
21 Correct 21 ms 22604 KB Output is correct
22 Correct 24 ms 22672 KB Output is correct
23 Correct 22 ms 22656 KB Output is correct
24 Correct 26 ms 22604 KB Output is correct
25 Correct 23 ms 22660 KB Output is correct
26 Correct 24 ms 22616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 22604 KB Output is correct
2 Correct 23 ms 22596 KB Output is correct
3 Correct 20 ms 22624 KB Output is correct
4 Correct 21 ms 22628 KB Output is correct
5 Correct 21 ms 22604 KB Output is correct
6 Correct 27 ms 22528 KB Output is correct
7 Correct 27 ms 22552 KB Output is correct
8 Correct 22 ms 22512 KB Output is correct
9 Correct 21 ms 22596 KB Output is correct
10 Correct 22 ms 22604 KB Output is correct
11 Correct 22 ms 22572 KB Output is correct
12 Correct 28 ms 22632 KB Output is correct
13 Correct 28 ms 22664 KB Output is correct
14 Correct 25 ms 22592 KB Output is correct
15 Correct 22 ms 22728 KB Output is correct
16 Correct 23 ms 22664 KB Output is correct
17 Correct 25 ms 22708 KB Output is correct
18 Correct 22 ms 22652 KB Output is correct
19 Correct 21 ms 22580 KB Output is correct
20 Correct 26 ms 22604 KB Output is correct
21 Correct 21 ms 22604 KB Output is correct
22 Correct 24 ms 22672 KB Output is correct
23 Correct 22 ms 22656 KB Output is correct
24 Correct 26 ms 22604 KB Output is correct
25 Correct 23 ms 22660 KB Output is correct
26 Correct 24 ms 22616 KB Output is correct
27 Correct 591 ms 31776 KB Output is correct
28 Correct 584 ms 31920 KB Output is correct
29 Correct 494 ms 31116 KB Output is correct
30 Correct 668 ms 32528 KB Output is correct
31 Correct 489 ms 30128 KB Output is correct
32 Correct 588 ms 30628 KB Output is correct
33 Correct 232 ms 28604 KB Output is correct
34 Correct 169 ms 26688 KB Output is correct
35 Correct 305 ms 29696 KB Output is correct
36 Correct 191 ms 26964 KB Output is correct
37 Correct 274 ms 28716 KB Output is correct
38 Correct 27 ms 23808 KB Output is correct
39 Correct 241 ms 28232 KB Output is correct
40 Correct 21 ms 22604 KB Output is correct
41 Correct 22 ms 22520 KB Output is correct