답안 #899451

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
899451 2024-01-06T08:02:46 Z pan RMQ (NOI17_rmq) C++17
100 / 100
77 ms 18496 KB
#include <bits/stdc++.h>
//#include "bits_stdc++.h"
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
ll const INF = 1e9;
ll n,q;
ll minl ,maxl, minr, maxr;
vector<vector<pi> >queries;
vector<vector<ll> > newq;
vector<ll> ans;
struct node{
    int s, e, m; //range is [s,e], m is the middle point
    ll val, pos; //sum of [s,e]
    ll lazy; //lazy tag of [s,e]
    node *l, *r; //create two children l and r, where l is [s,m] and [m+1,e]
    node (int S, int E){ //constructor called node
       s = S, e = E, m = (s+e)/2;
       val = 0; 
       pos = s;//initially all values are 0
       lazy = 0; //lazy tag of 0 will mean there is no update (sentinel value)
       l=r=nullptr;

	}
	void create()
	{
		if(s != e && l==nullptr){ //node is not yet a leaf, so create two children
		     l = new node(s, m); //create left child
             r = new node(m+1, e); //create right child
		 }
	}
 
    void propogate(){
       if (lazy==0) return; //nothing happens
       create();
       val+=lazy; //(e-s+1) is the length of the range
       if (s != e){ //not a leaf, send lazy tags to children
           l->lazy+=lazy;
           r->lazy+=lazy;
       }
       lazy=0; //set our lazy tag value back to the sentinel
    }
    void update(int S, int E, ll V){ //increment [S,E] by V
	   propogate();
       if(s==S && e==E) lazy += V; //update covers range, update lazy tag
       else{ //go we have to go deeper
		   create();
           if(E <= m) l->update(S, E, V); //[S,E] is in the left child
           else if (m < S) r->update(S, E, V); //[S,E] is in the right child
           else l->update(S, m, V),r->update(m+1, E, V);
           l->propogate(),r->propogate();
           //remember to propogate your children before update yourself
           if (l->val<=r->val) {val = l->val, pos = l-> pos;}
           else {val = r->val, pos = r-> pos;}
       }
    }
    pi query(int S, int E){
       propogate(); //remember to propogate
       if(s == S && e == E) {return mp(val, pos); }//case 1
       create();
       if(E <= m) {return l->query(S, E);} //case 2, recurse to left child
       else if(S >= m+1) {return r->query(S, E);} //case 3, recurse to right child
       else //case 4, split the query range, recurse to both childs
       {
			pi a = l->query(S, m), b = r->query(m+1, E); 
			if (a.f<=b.f) return a;
			else return b;
	   }
			
}
} *root;

void merge(pi a)
{
	minl = min(minl, a.f);
	maxl = max(maxl, a.f);
	minr = min(minr, a.s);
	maxr = max(maxr, a.s);
}

void end()
{
	while (n--) cout << -1 << ' ';
}

int main()
{
	cin >> n >> q;
	queries.resize(n);
	newq.resize(n);
	ans.resize(n);
	for (ll i=0; i<q; ++i)
	{
		ll a, b ,c;
		cin >> a >> b >> c;
		queries[c].pb(mp(a,b));
	}
	root = new node(0, n-1);
	for (ll i=n-1; i>=0; --i)
	{
		if (!queries[i].empty())
		{
			minl=INF, maxl = -1, minr= INF, maxr = -1;
			for (pi u: queries[i]) merge(u);
			//show3(minl, maxl, minr);
			//show(maxr);
			if (maxl>minr) {end(); return 0;}
			root->update(minl, maxr, 1);
			newq[i].pb(minl);
			newq[i].pb(maxl);
			newq[i].pb(minr);
			newq[i].pb(maxr);
		}
	}
	
	for (ll i=0; i<n; ++i)
	{
		if (!newq[i].empty())
		{
			root->update(newq[i][0], newq[i][3], -1);
			pi ret = root->query(newq[i][1], newq[i][2]);
			if (ret.f>0) {end(); return 0;}
			root->update(ret.s, ret.s, INF);
			ans[ret.s] = i;
		}
		else
		{
			pi ret = root->query(0, n-1);
			if (ret.f>0) {end(); return 0;}
			root->update(ret.s, ret.s, INF);
			ans[ret.s] = i;
		}
		
	}
	for (ll u: ans) cout << u << ' ';
	
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 436 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 436 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 77 ms 16140 KB Output is correct
28 Correct 73 ms 17468 KB Output is correct
29 Correct 54 ms 16548 KB Output is correct
30 Correct 71 ms 18496 KB Output is correct
31 Correct 57 ms 14264 KB Output is correct
32 Correct 62 ms 13764 KB Output is correct
33 Correct 23 ms 15452 KB Output is correct
34 Correct 15 ms 10648 KB Output is correct
35 Correct 30 ms 18008 KB Output is correct
36 Correct 41 ms 8020 KB Output is correct
37 Correct 59 ms 8784 KB Output is correct
38 Correct 6 ms 4444 KB Output is correct
39 Correct 46 ms 7500 KB Output is correct
40 Correct 0 ms 428 KB Output is correct
41 Correct 0 ms 344 KB Output is correct