답안 #345552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
345552 2021-01-07T14:50:42 Z darkxeon Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1102 ms 83444 KB
#include <bits/stdc++.h>
#define sz(x) (long long)(x).size()
 
using namespace std;
 
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
 
const int N = 1e5 + 5, M = 1e6 + 7, SM = 1e3 + 5, logN = 20;
const long long MOD = 1e9 + 7, INF = 1e18 + 9;
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};
 
void debug() {
	cerr << "\n";
}
template<typename Head, typename... Tail>
void debug(Head a, Tail... b) {
	cerr << a << " ";
	debug(b...);
}

struct Query {
	long long l, k, i;
};

vector<Query> q[M];

long long n, m, a[M], t[M], ans[M];

void update(long long i, long long x) {
	i = n - i + 1;
	for(; i <= n; i += i & -i) {
		t[i] = max(t[i], x);
	}
}

long long getMax(long long l) {
	l = n - l + 1;
	long long res = 0;
	for(; l >= 1; l -= l & -l) {
		res = max(res, t[l]);
	}
	return res;
}

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	cin >> n >> m;
	
	for(long long i = 1; i <= n; i++) {
		cin >> a[i];
	}
		
	for(long long i = 1; i <= m; i++) {
		long long l, r, k; cin >> l >> r >> k;
		q[r].push_back({l, k, i});
	}	
	
	/* Main observation: 
	 * We can't re-arrange given segment if and only if, 
	 * there is a pair of indices i and j, which are
	 * i < j, a[i] > a[j] and a[i] + a[j] > k
	 * 
	 * 
	 * @seduneon
	 */
	
	stack<long long> s;
	for(long long r = 1; r <= n; r++) {
		// Stack Trick to find first greater value on the left in O(n)
		while(!s.empty() && a[r] >= a[s.top()]) {
			s.pop();
		}
		if(!s.empty()) {
			// We should keep following sum on left position
			update(s.top(), a[r] + a[s.top()]);
		}
		s.push(r);
		for(auto [l, k, i] : q[r]) {
			ans[i] = (getMax(l) <= k);
		}
	}
	
	for(long long i = 1; i <= m; i++) {
		cout << ans[i] << "\n";
	}
	
	cout << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 18 ms 23940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 18 ms 23940 KB Output is correct
11 Correct 19 ms 24168 KB Output is correct
12 Correct 18 ms 24172 KB Output is correct
13 Correct 20 ms 24172 KB Output is correct
14 Correct 20 ms 24300 KB Output is correct
15 Correct 20 ms 24300 KB Output is correct
16 Correct 22 ms 24172 KB Output is correct
17 Correct 19 ms 24172 KB Output is correct
18 Correct 19 ms 24172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1061 ms 82028 KB Output is correct
2 Correct 1075 ms 82660 KB Output is correct
3 Correct 1077 ms 82676 KB Output is correct
4 Correct 1102 ms 82652 KB Output is correct
5 Correct 934 ms 75116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 29932 KB Output is correct
2 Correct 82 ms 30188 KB Output is correct
3 Correct 80 ms 29292 KB Output is correct
4 Correct 92 ms 29804 KB Output is correct
5 Correct 82 ms 29676 KB Output is correct
6 Correct 66 ms 29804 KB Output is correct
7 Correct 64 ms 29292 KB Output is correct
8 Correct 72 ms 28832 KB Output is correct
9 Correct 54 ms 28064 KB Output is correct
10 Correct 75 ms 28832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 18 ms 23940 KB Output is correct
11 Correct 19 ms 24168 KB Output is correct
12 Correct 18 ms 24172 KB Output is correct
13 Correct 20 ms 24172 KB Output is correct
14 Correct 20 ms 24300 KB Output is correct
15 Correct 20 ms 24300 KB Output is correct
16 Correct 22 ms 24172 KB Output is correct
17 Correct 19 ms 24172 KB Output is correct
18 Correct 19 ms 24172 KB Output is correct
19 Correct 194 ms 36076 KB Output is correct
20 Correct 219 ms 36516 KB Output is correct
21 Correct 160 ms 36844 KB Output is correct
22 Correct 157 ms 36332 KB Output is correct
23 Correct 154 ms 36332 KB Output is correct
24 Correct 138 ms 34924 KB Output is correct
25 Correct 143 ms 35692 KB Output is correct
26 Correct 205 ms 35052 KB Output is correct
27 Correct 174 ms 34540 KB Output is correct
28 Correct 168 ms 34540 KB Output is correct
29 Correct 174 ms 34412 KB Output is correct
30 Correct 171 ms 34924 KB Output is correct
31 Correct 172 ms 34924 KB Output is correct
32 Correct 172 ms 35052 KB Output is correct
33 Correct 176 ms 34540 KB Output is correct
34 Correct 135 ms 34284 KB Output is correct
35 Correct 133 ms 34412 KB Output is correct
36 Correct 126 ms 34796 KB Output is correct
37 Correct 130 ms 34796 KB Output is correct
38 Correct 132 ms 34924 KB Output is correct
39 Correct 158 ms 34208 KB Output is correct
40 Correct 131 ms 32668 KB Output is correct
41 Correct 151 ms 33568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 18 ms 23940 KB Output is correct
11 Correct 19 ms 24168 KB Output is correct
12 Correct 18 ms 24172 KB Output is correct
13 Correct 20 ms 24172 KB Output is correct
14 Correct 20 ms 24300 KB Output is correct
15 Correct 20 ms 24300 KB Output is correct
16 Correct 22 ms 24172 KB Output is correct
17 Correct 19 ms 24172 KB Output is correct
18 Correct 19 ms 24172 KB Output is correct
19 Correct 1061 ms 82028 KB Output is correct
20 Correct 1075 ms 82660 KB Output is correct
21 Correct 1077 ms 82676 KB Output is correct
22 Correct 1102 ms 82652 KB Output is correct
23 Correct 934 ms 75116 KB Output is correct
24 Correct 86 ms 29932 KB Output is correct
25 Correct 82 ms 30188 KB Output is correct
26 Correct 80 ms 29292 KB Output is correct
27 Correct 92 ms 29804 KB Output is correct
28 Correct 82 ms 29676 KB Output is correct
29 Correct 66 ms 29804 KB Output is correct
30 Correct 64 ms 29292 KB Output is correct
31 Correct 72 ms 28832 KB Output is correct
32 Correct 54 ms 28064 KB Output is correct
33 Correct 75 ms 28832 KB Output is correct
34 Correct 194 ms 36076 KB Output is correct
35 Correct 219 ms 36516 KB Output is correct
36 Correct 160 ms 36844 KB Output is correct
37 Correct 157 ms 36332 KB Output is correct
38 Correct 154 ms 36332 KB Output is correct
39 Correct 138 ms 34924 KB Output is correct
40 Correct 143 ms 35692 KB Output is correct
41 Correct 205 ms 35052 KB Output is correct
42 Correct 174 ms 34540 KB Output is correct
43 Correct 168 ms 34540 KB Output is correct
44 Correct 174 ms 34412 KB Output is correct
45 Correct 171 ms 34924 KB Output is correct
46 Correct 172 ms 34924 KB Output is correct
47 Correct 172 ms 35052 KB Output is correct
48 Correct 176 ms 34540 KB Output is correct
49 Correct 135 ms 34284 KB Output is correct
50 Correct 133 ms 34412 KB Output is correct
51 Correct 126 ms 34796 KB Output is correct
52 Correct 130 ms 34796 KB Output is correct
53 Correct 132 ms 34924 KB Output is correct
54 Correct 158 ms 34208 KB Output is correct
55 Correct 131 ms 32668 KB Output is correct
56 Correct 151 ms 33568 KB Output is correct
57 Correct 1036 ms 83172 KB Output is correct
58 Correct 1028 ms 82336 KB Output is correct
59 Correct 975 ms 83180 KB Output is correct
60 Correct 1004 ms 83228 KB Output is correct
61 Correct 1006 ms 83180 KB Output is correct
62 Correct 1004 ms 83444 KB Output is correct
63 Correct 680 ms 77268 KB Output is correct
64 Correct 688 ms 77384 KB Output is correct
65 Correct 945 ms 75512 KB Output is correct
66 Correct 894 ms 75940 KB Output is correct
67 Correct 902 ms 75372 KB Output is correct
68 Correct 941 ms 75232 KB Output is correct
69 Correct 928 ms 75000 KB Output is correct
70 Correct 918 ms 74516 KB Output is correct
71 Correct 945 ms 75200 KB Output is correct
72 Correct 925 ms 75160 KB Output is correct
73 Correct 580 ms 72812 KB Output is correct
74 Correct 653 ms 73072 KB Output is correct
75 Correct 575 ms 72824 KB Output is correct
76 Correct 570 ms 73964 KB Output is correct
77 Correct 614 ms 72804 KB Output is correct
78 Correct 836 ms 74776 KB Output is correct
79 Correct 648 ms 66048 KB Output is correct
80 Correct 819 ms 71296 KB Output is correct