#include <stdio.h>
#define N 1000000
#define K 1000000
#define N_ (1 << 20) /* N_ = pow2(ceil(log2(N))) */
unsigned int Z = 12345;
int rand_() {
return (Z *= 3) >> 1;
}
int st[N_ * 2], n_;
void build(int n) {
int i;
n_ = 1;
while (n_ < n)
n_ <<= 1;
for (i = 0; i < n_; i++)
st[n_ + i] = i < n ? 1 : 0;
for (i = n_ - 1; i > 0; i--)
st[i] = st[i << 1 | 0] + st[i << 1 | 1];
}
void update(int i, int x) {
for (i += n_; i > 0; i >>= 1)
st[i] += x;
}
int query(int k) {
int i = 1;
while (i < n_)
if (k < st[i << 1 | 0])
i = i << 1 | 0;
else
k -= st[i << 1 | 0], i = i << 1 | 1;
return i - n_;
}
int zz[K + 1], ll[K + 1], rr[K + 1], ii[K + 1], u_, l_, r_;
int node(int i) {
static int _ = 1;
zz[_] = rand_();
ii[_] = i;
return _++;
}
void split(int u, int i) {
if (u == 0) {
u_ = l_ = r_ = 0;
return;
}
if (ii[u] < i) {
split(rr[u], i);
rr[u] = l_, l_ = u;
} else if (ii[u] > i) {
split(ll[u], i);
ll[u] = r_, r_ = u;
} else {
u_ = u, l_ = ll[u], r_ = rr[u];
ll[u] = rr[u] = 0;
}
}
int merge(int u, int v) {
if (u == 0)
return v;
if (v == 0)
return u;
if (zz[u] < zz[v]) {
rr[u] = merge(rr[u], v);
return u;
} else {
ll[v] = merge(u, ll[v]);
return v;
}
}
int first(int u) {
return ll[u] == 0 ? ii[u] : first(ll[u]);
}
int last(int u) {
return rr[u] == 0 ? ii[u] : last(rr[u]);
}
int remove_first(int u) {
if (ll[u] == 0)
return rr[u];
ll[u] = remove_first(ll[u]);
return u;
}
int aa[N], dp[N], uu[K + 1], k;
void add(int i) {
int j, l, r;
while (1) {
if (dp[i] == k) {
uu[k++] = node(i);
return;
}
split(uu[dp[i]], i);
if (l_ && aa[last(l_)] < aa[i])
uu[dp[i]++] = merge(l_, r_);
else
break;
}
l = l_, r = r_;
while (r && aa[i] < aa[j = first(r)]) {
r = remove_first(r);
dp[j]++, add(j);
}
uu[dp[i]] = merge(merge(l, node(i)), r);
}
int main() {
static int ii[N], aa_[N];
int n, h, i;
scanf("%d", &n);
for (h = 0; h < n; h++)
scanf("%d%d", &ii[h], &aa_[h]), ii[h]--;
build(n);
for (h = n - 1; h >= 0; h--) {
i = ii[h] = query(ii[h]);
aa[i] = aa_[h];
update(i, -1);
}
for (h = 0; h < n; h++) {
add(ii[h]);
printf("%d\n", k);
}
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:127:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:129:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d%d", &ii[h], &aa_[h]), ii[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
436 KB |
Output is correct |
5 |
Correct |
6 ms |
1688 KB |
Output is correct |
6 |
Correct |
6 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
696 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
5 ms |
608 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
5 ms |
560 KB |
Output is correct |
17 |
Correct |
5 ms |
596 KB |
Output is correct |
18 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
436 KB |
Output is correct |
5 |
Correct |
6 ms |
1688 KB |
Output is correct |
6 |
Correct |
6 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
696 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
5 ms |
608 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
5 ms |
560 KB |
Output is correct |
17 |
Correct |
5 ms |
596 KB |
Output is correct |
18 |
Correct |
5 ms |
596 KB |
Output is correct |
19 |
Correct |
129 ms |
9952 KB |
Output is correct |
20 |
Correct |
105 ms |
10068 KB |
Output is correct |
21 |
Correct |
242 ms |
21900 KB |
Output is correct |
22 |
Correct |
221 ms |
22136 KB |
Output is correct |
23 |
Correct |
212 ms |
10020 KB |
Output is correct |
24 |
Correct |
313 ms |
10008 KB |
Output is correct |
25 |
Correct |
218 ms |
9924 KB |
Output is correct |
26 |
Correct |
239 ms |
10104 KB |
Output is correct |
27 |
Correct |
226 ms |
9928 KB |
Output is correct |
28 |
Correct |
231 ms |
9892 KB |
Output is correct |
29 |
Correct |
225 ms |
10016 KB |
Output is correct |
30 |
Correct |
222 ms |
9908 KB |
Output is correct |
31 |
Correct |
524 ms |
10132 KB |
Output is correct |
32 |
Correct |
501 ms |
10028 KB |
Output is correct |
33 |
Correct |
517 ms |
10036 KB |
Output is correct |
34 |
Correct |
508 ms |
10208 KB |
Output is correct |
35 |
Correct |
500 ms |
10068 KB |
Output is correct |
36 |
Correct |
513 ms |
9932 KB |
Output is correct |
37 |
Correct |
512 ms |
9980 KB |
Output is correct |
38 |
Correct |
509 ms |
10056 KB |
Output is correct |
39 |
Correct |
513 ms |
10012 KB |
Output is correct |
40 |
Correct |
515 ms |
10028 KB |
Output is correct |
41 |
Correct |
89 ms |
8240 KB |
Output is correct |
42 |
Correct |
15 ms |
1732 KB |
Output is correct |
43 |
Correct |
176 ms |
17640 KB |
Output is correct |
44 |
Correct |
114 ms |
12032 KB |
Output is correct |
45 |
Correct |
15 ms |
1212 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
69 ms |
4160 KB |
Output is correct |
48 |
Correct |
47 ms |
2892 KB |
Output is correct |
49 |
Correct |
9 ms |
1040 KB |
Output is correct |
50 |
Correct |
3 ms |
596 KB |
Output is correct |
51 |
Correct |
39 ms |
2640 KB |
Output is correct |
52 |
Correct |
132 ms |
6304 KB |
Output is correct |
53 |
Correct |
111 ms |
5700 KB |
Output is correct |
54 |
Correct |
96 ms |
5068 KB |
Output is correct |
55 |
Correct |
19 ms |
1484 KB |
Output is correct |
56 |
Correct |
154 ms |
4304 KB |
Output is correct |
57 |
Correct |
55 ms |
1904 KB |
Output is correct |
58 |
Correct |
47 ms |
1800 KB |
Output is correct |
59 |
Correct |
10 ms |
724 KB |
Output is correct |
60 |
Correct |
259 ms |
5732 KB |
Output is correct |
61 |
Correct |
2 ms |
468 KB |
Output is correct |
62 |
Correct |
99 ms |
2920 KB |
Output is correct |
63 |
Correct |
47 ms |
1772 KB |
Output is correct |
64 |
Correct |
172 ms |
4600 KB |
Output is correct |
65 |
Correct |
143 ms |
4196 KB |
Output is correct |
66 |
Correct |
28 ms |
1364 KB |
Output is correct |
67 |
Correct |
103 ms |
3060 KB |
Output is correct |
68 |
Correct |
65 ms |
2280 KB |
Output is correct |
69 |
Correct |
145 ms |
4156 KB |
Output is correct |
70 |
Correct |
23 ms |
1108 KB |
Output is correct |
71 |
Correct |
1448 ms |
46240 KB |
Output is correct |
72 |
Correct |
1482 ms |
46476 KB |
Output is correct |
73 |
Correct |
1460 ms |
46440 KB |
Output is correct |
74 |
Correct |
1399 ms |
46220 KB |
Output is correct |
75 |
Correct |
1503 ms |
46392 KB |
Output is correct |
76 |
Correct |
1487 ms |
46336 KB |
Output is correct |