# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
693092 | 2023-02-02T11:21:49 Z | whqkrtk04 | Ancient Books (IOI17_books) | C++17 | 298 ms | 106096 KB |
#include "books.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, pll> plll; #define fi first #define se second const int INF = 1e9+1; const int P = 1000000007; const ll LLINF = (ll)1e18+1; template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; } template <typename node_seg, typename node_query = node_seg, typename index_t = int> class Segtree { private: const size_t n; std::vector<node_seg> seg; void init(const size_t i, const index_t s, const index_t e, const std::vector<node_seg> &A) { if(s+1 == e) seg[i] = A[s]; else { init(i<<1, s, s+e>>1, A); init(i<<1|1, s+e>>1, e, A); seg[i] = seg[i<<1]+seg[i<<1|1]; } } void update(const size_t i, const index_t s, const index_t e, const index_t j, const node_query &x) { if(j >= e || s > j) return; if(s+1 == e) seg[i] += x; else { update(i<<1, s, s+e>>1, j, x); update(i<<1|1, s+e>>1, e, j, x); seg[i] = seg[i<<1]+seg[i<<1|1]; } } node_seg query(const size_t i, const index_t s, const index_t e, const index_t l, const index_t r) const { if(e <= l || r <= s) return node_seg::inf(); if(l <= s && e <= r) return seg[i]; return query(i<<1, s, s+e>>1, l, r)+query(i<<1|1, s+e>>1, e, l, r); } public: Segtree(const int n) : n(n) { seg.resize(4*n, node_seg::inf()); } Segtree(const std::vector<node_seg> &A) : n(A.size()) { seg.resize(4*n, node_seg::inf()); init(1, 0, n, A); } void update(const index_t j, const node_query &x) { update(1, 0, n, j, x); } node_seg query(const index_t l, const index_t r) const { return query(1, 0, n, l, r); } }; struct Node_min { int x; static Node_min inf() { return {INF}; } Node_min operator+(const Node_min &y) { return {min(x, y.x)}; } void operator+=(const Node_min &y) { x = min(x, y.x); } }; struct Node_max { int x; static Node_max inf() { return {-INF}; } Node_max operator+(const Node_max &y) { return {max(x, y.x)}; } void operator+=(const Node_max &y) { x = max(x, y.x); } }; ll minimum_walk(vector<int> p, int s) { ll ans = 0LL; for(int i = 0; i < p.size(); i++) ans += abs(i-p[i]); int n = p.size(); vector<vector<int>> A; vector<int> B(n); vector<bool> vis(n, false); for(int i = 0; i < n; i++) { if(vis[i]) continue; vector<int> T; int tmp = i; do { T.push_back(tmp); B[tmp] = A.size(); vis[tmp] = true; tmp = p[tmp]; } while(tmp != i); A.push_back(T); } vector<Node_min> min_nodes(n, Node_min::inf()); vector<Node_max> max_nodes(n, Node_max::inf()); for(int i = 0; i < A.size(); i++) { int l = *min_element(A[i].begin(), A[i].end()); int r = *max_element(A[i].begin(), A[i].end()); for(auto j : A[i]) { min_nodes[j].x = l; max_nodes[j].x = r+1; } } Segtree<Node_min> seg_min(min_nodes); Segtree<Node_max> seg_max(max_nodes); int l = s, r = s+1; int lc = 0, rc = 0; while(l > 0 && p[l-1] == l-1) l--, lc++; while(r < n && p[r] == r) r++, rc++; while(l > 0 || r < n) { int mi = seg_min.query(l, r).x; int ma = seg_max.query(l, r).x; if(mi == l && ma == r) { if(l && (r == n || lc < rc)) { ans += (lc+1)*2; l--; lc = 0; } else { ans += (rc+1)*2; r++; rc = 0; } } else { if(mi != l) lc = 0, l = mi; if(ma != r) rc = 0, r = ma; } while(l > 0 && p[l-1] == l-1) l--, lc++; while(r < n && p[r] == r) r++, rc++; } return ans; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 212 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 212 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 0 ms | 212 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 0 ms | 212 KB | Output is correct |
14 | Correct | 0 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 0 ms | 212 KB | Output is correct |
17 | Correct | 0 ms | 212 KB | Output is correct |
18 | Correct | 0 ms | 212 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 212 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 212 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 0 ms | 212 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 0 ms | 212 KB | Output is correct |
14 | Correct | 0 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 0 ms | 212 KB | Output is correct |
17 | Correct | 0 ms | 212 KB | Output is correct |
18 | Correct | 0 ms | 212 KB | Output is correct |
19 | Correct | 0 ms | 340 KB | Output is correct |
20 | Correct | 0 ms | 340 KB | Output is correct |
21 | Correct | 0 ms | 340 KB | Output is correct |
22 | Correct | 1 ms | 340 KB | Output is correct |
23 | Correct | 1 ms | 340 KB | Output is correct |
24 | Correct | 1 ms | 340 KB | Output is correct |
25 | Correct | 1 ms | 340 KB | Output is correct |
26 | Correct | 0 ms | 340 KB | Output is correct |
27 | Correct | 0 ms | 340 KB | Output is correct |
28 | Correct | 0 ms | 340 KB | Output is correct |
29 | Correct | 1 ms | 340 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 212 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 212 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 0 ms | 212 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 0 ms | 212 KB | Output is correct |
14 | Correct | 0 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 0 ms | 212 KB | Output is correct |
17 | Correct | 0 ms | 212 KB | Output is correct |
18 | Correct | 0 ms | 212 KB | Output is correct |
19 | Correct | 0 ms | 340 KB | Output is correct |
20 | Correct | 0 ms | 340 KB | Output is correct |
21 | Correct | 0 ms | 340 KB | Output is correct |
22 | Correct | 1 ms | 340 KB | Output is correct |
23 | Correct | 1 ms | 340 KB | Output is correct |
24 | Correct | 1 ms | 340 KB | Output is correct |
25 | Correct | 1 ms | 340 KB | Output is correct |
26 | Correct | 0 ms | 340 KB | Output is correct |
27 | Correct | 0 ms | 340 KB | Output is correct |
28 | Correct | 0 ms | 340 KB | Output is correct |
29 | Correct | 1 ms | 340 KB | Output is correct |
30 | Correct | 225 ms | 55228 KB | Output is correct |
31 | Correct | 187 ms | 56060 KB | Output is correct |
32 | Correct | 206 ms | 106096 KB | Output is correct |
33 | Correct | 298 ms | 89208 KB | Output is correct |
34 | Correct | 279 ms | 89196 KB | Output is correct |
35 | Correct | 286 ms | 81648 KB | Output is correct |
36 | Correct | 209 ms | 66712 KB | Output is correct |
37 | Correct | 136 ms | 56480 KB | Output is correct |
38 | Correct | 131 ms | 55404 KB | Output is correct |
39 | Correct | 128 ms | 55416 KB | Output is correct |
40 | Correct | 131 ms | 55432 KB | Output is correct |
41 | Correct | 147 ms | 55564 KB | Output is correct |
42 | Correct | 165 ms | 55572 KB | Output is correct |
43 | Correct | 205 ms | 91896 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 340 KB | 3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 212 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 212 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 0 ms | 212 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 0 ms | 212 KB | Output is correct |
14 | Correct | 0 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 0 ms | 212 KB | Output is correct |
17 | Correct | 0 ms | 212 KB | Output is correct |
18 | Correct | 0 ms | 212 KB | Output is correct |
19 | Correct | 0 ms | 340 KB | Output is correct |
20 | Correct | 0 ms | 340 KB | Output is correct |
21 | Correct | 0 ms | 340 KB | Output is correct |
22 | Correct | 1 ms | 340 KB | Output is correct |
23 | Correct | 1 ms | 340 KB | Output is correct |
24 | Correct | 1 ms | 340 KB | Output is correct |
25 | Correct | 1 ms | 340 KB | Output is correct |
26 | Correct | 0 ms | 340 KB | Output is correct |
27 | Correct | 0 ms | 340 KB | Output is correct |
28 | Correct | 0 ms | 340 KB | Output is correct |
29 | Correct | 1 ms | 340 KB | Output is correct |
30 | Correct | 225 ms | 55228 KB | Output is correct |
31 | Correct | 187 ms | 56060 KB | Output is correct |
32 | Correct | 206 ms | 106096 KB | Output is correct |
33 | Correct | 298 ms | 89208 KB | Output is correct |
34 | Correct | 279 ms | 89196 KB | Output is correct |
35 | Correct | 286 ms | 81648 KB | Output is correct |
36 | Correct | 209 ms | 66712 KB | Output is correct |
37 | Correct | 136 ms | 56480 KB | Output is correct |
38 | Correct | 131 ms | 55404 KB | Output is correct |
39 | Correct | 128 ms | 55416 KB | Output is correct |
40 | Correct | 131 ms | 55432 KB | Output is correct |
41 | Correct | 147 ms | 55564 KB | Output is correct |
42 | Correct | 165 ms | 55572 KB | Output is correct |
43 | Correct | 205 ms | 91896 KB | Output is correct |
44 | Incorrect | 1 ms | 340 KB | 3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
45 | Halted | 0 ms | 0 KB | - |