답안 #630167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630167 2022-08-15T19:34:28 Z ptrtp RMQ (NOI17_rmq) C++14
100 / 100
122 ms 20972 KB
#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 18;
const int off = 1 << logo;
const int treesiz = off << 1;

int n, q;
int l[maxn], r[maxn], val[maxn];
int mini[maxn], maxi[maxn];
multiset< int > s;
vector< int > events[maxn];
vector< int > pos[maxn];
int sol[maxn];
int tour[treesiz];

int query(int a, int b, int l, int r, int node) {
	if (l > b || r < a) return inf;
	if (l >= a && r <= b) return tour[node];

	int mid = (l + r) / 2;
	return min(query(a, b, l, mid, node * 2), query(a, b, mid + 1, r, node * 2 + 1));
}

int main() {
	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; i++) maxi[i] = n - 1;
	for (int i = 1; i <= q; i++) {
		scanf("%d%d%d", l+i, r+i, val+i);
		mini[val[i]] = max(mini[val[i]], l[i]);
		maxi[val[i]] = max(maxi[val[i]], r[i]);

		events[l[i]].push_back(i);
		events[r[i] + 1].push_back(-i);
	}

	s.insert(0);
	for (int i = 0; i < n; i++) {
		for (int tren : events[i]) {
			if (tren > 0) s.insert(val[tren]);
			else s.erase(s.find(val[-tren]));
		}
		pos[*s.rbegin()].push_back(i);
	}

	s.clear();
	for (int i = 0; i < n; i++) {
		for (int tren : pos[i]) s.insert(tren);
		auto iter = s.lower_bound(mini[i]);

		if (iter == s.end() || *iter > maxi[i]) {
			for (int j = 0; j < n; j++) 
				printf("-1 ");
			printf("\n");
			return 0;
		} else {
			sol[*iter] = i;
			s.erase(iter); 
		}
	}

	for (int i = 0; i < n; i++) tour[i + off] = sol[i];
	for (int i = off - 1; i > 0; i--) tour[i] = min(tour[i * 2], tour[i * 2 + 1]);

	for (int i = 1; i <= q; i++) {
		if (query(l[i], r[i], 0, off - 1, 1) != val[i]) {
			for (int j = 0; j < n; j++) 
				printf("-1 ");
			printf("\n");
			return 0;
		}
	}

	for (int i = 0; i < n; i++) {
		printf("%d ", sol[i]);
	}
	printf("\n");
	return 0;
}

Compilation message

rmq.cpp: In function 'int main()':
rmq.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
rmq.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |   scanf("%d%d%d", l+i, r+i, val+i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10708 KB Output is correct
2 Correct 7 ms 10732 KB Output is correct
3 Correct 6 ms 10708 KB Output is correct
4 Correct 6 ms 10708 KB Output is correct
5 Correct 6 ms 10728 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 10708 KB Output is correct
10 Correct 5 ms 9704 KB Output is correct
11 Correct 5 ms 9688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10708 KB Output is correct
2 Correct 7 ms 10732 KB Output is correct
3 Correct 6 ms 10708 KB Output is correct
4 Correct 6 ms 10708 KB Output is correct
5 Correct 6 ms 10728 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 10708 KB Output is correct
10 Correct 5 ms 9704 KB Output is correct
11 Correct 5 ms 9688 KB Output is correct
12 Correct 6 ms 10732 KB Output is correct
13 Correct 7 ms 10836 KB Output is correct
14 Correct 7 ms 10736 KB Output is correct
15 Correct 6 ms 10776 KB Output is correct
16 Correct 7 ms 10832 KB Output is correct
17 Correct 7 ms 10876 KB Output is correct
18 Correct 6 ms 10836 KB Output is correct
19 Correct 6 ms 10708 KB Output is correct
20 Correct 6 ms 10728 KB Output is correct
21 Correct 5 ms 9688 KB Output is correct
22 Correct 5 ms 9688 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 6 ms 10748 KB Output is correct
25 Correct 5 ms 9688 KB Output is correct
26 Correct 5 ms 9692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10708 KB Output is correct
2 Correct 7 ms 10732 KB Output is correct
3 Correct 6 ms 10708 KB Output is correct
4 Correct 6 ms 10708 KB Output is correct
5 Correct 6 ms 10728 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 10708 KB Output is correct
10 Correct 5 ms 9704 KB Output is correct
11 Correct 5 ms 9688 KB Output is correct
12 Correct 6 ms 10732 KB Output is correct
13 Correct 7 ms 10836 KB Output is correct
14 Correct 7 ms 10736 KB Output is correct
15 Correct 6 ms 10776 KB Output is correct
16 Correct 7 ms 10832 KB Output is correct
17 Correct 7 ms 10876 KB Output is correct
18 Correct 6 ms 10836 KB Output is correct
19 Correct 6 ms 10708 KB Output is correct
20 Correct 6 ms 10728 KB Output is correct
21 Correct 5 ms 9688 KB Output is correct
22 Correct 5 ms 9688 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 6 ms 10748 KB Output is correct
25 Correct 5 ms 9688 KB Output is correct
26 Correct 5 ms 9692 KB Output is correct
27 Correct 122 ms 20076 KB Output is correct
28 Correct 102 ms 20444 KB Output is correct
29 Correct 87 ms 19752 KB Output is correct
30 Correct 117 ms 20972 KB Output is correct
31 Correct 90 ms 18856 KB Output is correct
32 Correct 104 ms 20600 KB Output is correct
33 Correct 30 ms 16348 KB Output is correct
34 Correct 22 ms 14528 KB Output is correct
35 Correct 39 ms 17868 KB Output is correct
36 Correct 56 ms 15840 KB Output is correct
37 Correct 85 ms 17404 KB Output is correct
38 Correct 8 ms 10584 KB Output is correct
39 Correct 75 ms 19896 KB Output is correct
40 Correct 5 ms 9712 KB Output is correct
41 Correct 5 ms 9708 KB Output is correct