이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define file(s) if(fopen(s".in","r")) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define tm (tl + tr >> 1)
#define ls v << 1, tl, tm
#define rs v << 1 | 1, tm + 1, tr
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;
const int MOD = 1e9 + 7;
const int N = 4e5 + 7;
const int P = 911;
const ll INF = 1e18;
int rnd() {
int x = rand() << 15;
return x ^ rand();
}
int n, t[N << 2], sz[N], l[N], r[N], a[N], ans = 0;
void rec(int idx, int val, int v = 1, int tl = 1, int tr = n) {
// cout << "rec " << v << ' ' << tl << ' ' << tr << '\n';
if (tl == tr) {
t[v] += val;
return;
}
if (tl > tr) return;
if (idx <= tm) rec(idx, val, ls);
else rec(idx, val, rs);
t[v] = t[v << 1] + t[v << 1 | 1];
}
int get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (l > r || r < tl || tr < l) return 0;
if (l <= tl && tr <= r) return t[v];
return get(l, r, ls) + get(l, r, rs);
}
void add(int v, int &cur1, int &cur2) {
// cout << "add " << v << '\n';
if (a[v]) {
cur1 += get(1, a[v] - 1);
cur2 += get(a[v] + 1, n);
// cout << v << ' ' << cur1 << ' ' << cur2 << '\n';
return;
}
add(l[v], cur1, cur2);
add(r[v], cur1, cur2);
}
void upd(int v, int ty) {
// cout << "upd " << v << '\n';
if (a[v]) {
rec(a[v], ty);
return;
}
upd(l[v], ty);
upd(r[v], ty);
}
void dfs(int v, bool clr) {
// cout << "dfs " << v << '\n';
if (a[v]) {
if (!clr) {
rec(a[v], 1, 1, 1, n);
// cout << "upd " << v << ' ' << a[v] << ' ' << get(1, n) << '\n';
}
return;
}
if (sz[l[v]] > sz[r[v]]) swap(l[v], r[v]);
dfs(l[v], 1);
dfs(r[v], 0);
int cur1 = 0, cur2 = 0;
add(l[v], cur1, cur2);
// cout << v << ' ' << cur1 << ' ' << cur2 << ' ' << get(1, n) << '\n';
ans += min(cur1, cur2);
if (clr) upd(r[v], -1);
else upd(l[v], 1);
}
int temp = 1;
void input(int v = 1) {
cin >> a[v];
sz[v] = 1;
if (!a[v]) {
l[v] = ++temp;
input(temp);
sz[v] += sz[l[v]];
r[v] = ++temp;
input(temp);
sz[v] += sz[r[v]];
}
}
void GazizMadi() {
cin >> n;
input();
dfs(1, 0);
cout << ans;
}
const bool Cases = 0;
signed main() {
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
// srand(time(0));
int TT = 1;
if (Cases) cin >> TT;
for (int i = 1; i <= TT; i++) {
//cout << "Case " << i << ": ";
GazizMadi();
}
}
컴파일 시 표준 에러 (stderr) 메시지
rot.cpp: In function 'void rec(long long int, long long int, long long int, long long int, long long int)':
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
rot.cpp:53:13: note: in expansion of macro 'tm'
53 | if (idx <= tm) rec(idx, val, ls);
| ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
rot.cpp:14:24: note: in expansion of macro 'tm'
14 | #define ls v << 1, tl, tm
| ^~
rot.cpp:53:31: note: in expansion of macro 'ls'
53 | if (idx <= tm) rec(idx, val, ls);
| ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
rot.cpp:15:24: note: in expansion of macro 'tm'
15 | #define rs v << 1 | 1, tm + 1, tr
| ^~
rot.cpp:54:21: note: in expansion of macro 'rs'
54 | else rec(idx, val, rs);
| ^~
rot.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
rot.cpp:14:24: note: in expansion of macro 'tm'
14 | #define ls v << 1, tl, tm
| ^~
rot.cpp:61:19: note: in expansion of macro 'ls'
61 | return get(l, r, ls) + get(l, r, rs);
| ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
rot.cpp:15:24: note: in expansion of macro 'tm'
15 | #define rs v << 1 | 1, tm + 1, tr
| ^~
rot.cpp:61:35: note: in expansion of macro 'rs'
61 | return get(l, r, ls) + get(l, r, rs);
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |