Submission #906028

# Submission time Handle Problem Language Result Execution time Memory
906028 2024-01-13T10:34:19 Z atom RMQ (NOI17_rmq) C++17
100 / 100
40 ms 10332 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 1e5 + 5;

int n, q;
vector <pair <int, int>> qs[N];
int ans[N];
set <int> idx;
int lst;
// Storing indexes
bool vis[N];
void impossible() {
	for (int i = 1; i <= n; ++i) cout << "-1 ";
	exit(0);
}
void brute(int cur, int id) {
	while (lst >= 0 && vis[lst]) --lst;
	if (lst <= cur) impossible();
	ans[id] = lst;
	vis[lst] = 1;
}	

void solve(int x, int l, int r, int L, int R) {
	if (l > r) impossible();
	auto p = idx.lower_bound(L);
	auto i = p;
	// debug(*p);
	bool done = 0;
	while (p != idx.end() && *p <= R) {
		// find minimum range where x is the minimum for all range of [L, R]
		if (l <= *p && *p <= r && !done) {
			ans[*p] = x;
			vis[x] = 1;
			done = 1;
			// debug(l, r, *p);
		}
		else {
			brute(x, *p);
		}
		++p;
	}
	// for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n];
	// remove processed range
	if (!done) impossible();
	debug(*i, *p);
	idx.erase(i, p);
}	


signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif

    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
    	int l, r, x;
    	cin >> l >> r >> x;
    	++l; ++r;
    	qs[x].push_back({l, r});
    }

    lst = n - 1;
    for (int i = 1; i <= n; ++i) idx.insert(i);
    
    for (int x = n - 1; x >= 0; --x) {
    	if (!qs[x].empty()) {
    		int l = -1, L = 1e9;
    		int r = 1e9, R = -1;
    		for (auto& [lb, rb] : qs[x]) {
    			l = max(l, lb); L = min(L, lb);
    			r = min(r, rb); R = max(R, rb);
    		}
    		solve(x, l, r, L, R);
    	}
    }
    for (int i : idx) {
    	brute(-1, i);
    }
    for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n];
}

Compilation message

rmq.cpp: In function 'void solve(int, int, int, int, int)':
rmq.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
rmq.cpp:54:2: note: in expansion of macro 'debug'
   54 |  debug(*i, *p);
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2904 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2904 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 3160 KB Output is correct
13 Correct 2 ms 3416 KB Output is correct
14 Correct 1 ms 3164 KB Output is correct
15 Correct 1 ms 3160 KB Output is correct
16 Correct 1 ms 3164 KB Output is correct
17 Correct 2 ms 3164 KB Output is correct
18 Correct 2 ms 3164 KB Output is correct
19 Correct 1 ms 3164 KB Output is correct
20 Correct 2 ms 3160 KB Output is correct
21 Correct 2 ms 3164 KB Output is correct
22 Correct 2 ms 3132 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 2 ms 3164 KB Output is correct
25 Correct 2 ms 2904 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2904 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 3160 KB Output is correct
13 Correct 2 ms 3416 KB Output is correct
14 Correct 1 ms 3164 KB Output is correct
15 Correct 1 ms 3160 KB Output is correct
16 Correct 1 ms 3164 KB Output is correct
17 Correct 2 ms 3164 KB Output is correct
18 Correct 2 ms 3164 KB Output is correct
19 Correct 1 ms 3164 KB Output is correct
20 Correct 2 ms 3160 KB Output is correct
21 Correct 2 ms 3164 KB Output is correct
22 Correct 2 ms 3132 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 2 ms 3164 KB Output is correct
25 Correct 2 ms 2904 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 35 ms 9024 KB Output is correct
28 Correct 40 ms 9096 KB Output is correct
29 Correct 30 ms 8652 KB Output is correct
30 Correct 36 ms 9500 KB Output is correct
31 Correct 28 ms 8108 KB Output is correct
32 Correct 29 ms 8136 KB Output is correct
33 Correct 21 ms 7524 KB Output is correct
34 Correct 13 ms 5976 KB Output is correct
35 Correct 24 ms 8284 KB Output is correct
36 Correct 34 ms 9816 KB Output is correct
37 Correct 34 ms 10332 KB Output is correct
38 Correct 12 ms 6488 KB Output is correct
39 Correct 33 ms 8884 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 2 ms 2908 KB Output is correct