ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#169837 | #96. leaves | wuzr | 30 | 5ms | 1488kb | C++ | 879b | 2023-03-11 13:39:00 | 2023-03-11 13:39:00 |
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
const int T = 4e6 + 5, N = 2e5 + 5;
int sz, n, s[T], ls[T], rs[N];
ll r, r1, r2;
int ins(int L, int R, int w) {
s[++sz] = 1;
if (L == R) return sz;
int m = L + R >> 1, nw = sz;
w <= m ? ls[nw] = ins(L, m, w) : rs[nw] = ins(m + 1, R, w);
return nw;
}
int merge(int u, int v) {
if (!u || !v) return u | v;
s[u] += s[v];
r1 += 1ll * s[ls[u]] * s[rs[v]];
r2 += 1ll * s[rs[u]] * s[ls[v]];
ls[u] = merge(ls[u], ls[v]);
rs[u] = merge(rs[u], rs[v]);
return u;
}
int build() {
int w;
cin >> w;
if (w) return ins(1, n, w);
int ls = build(), rs = build();
r1 = r2 = 0;
int c = merge(ls, rs);
r += min(r1, r2);
return c;
}
signed main() {
cin >> n;
build();
cout << r;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 1ms
memory: 1208kb
input:
20 0 0 0 0 0 9 0 5 12 4 0 18 0 0 0 10 1 14 16 0 0 0 11 19 0 17 13 6 0 3 0 0 20 8 0 0 15 7 2
output:
58
result:
ok single line: '58'
Test #2:
score: 10
Accepted
time: 2ms
memory: 1488kb
input:
2000 0 0 0 0 0 567 148 217 0 0 1384 0 262 913 0 0 1071 0 0 991 0 732 1283 0 1925 707 0 790 0 24 1342...
output:
947070
result:
ok single line: '947070'
Test #3:
score: 10
Accepted
time: 2ms
memory: 1488kb
input:
2000 0 0 0 0 1763 0 732 173 0 0 0 0 0 0 1661 1361 0 1134 1331 0 0 0 0 1098 0 1051 160 0 0 0 1329 803...
output:
952846
result:
ok single line: '952846'
Test #4:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 156867 0 0 0 0 23121 196056 0 109446 71406 0 0 0 0 127531 154371 ...
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 78069 0 0 0 0 160740 186045 175989 0 0 0 76156 148348 0 67838 50734 0 30667...
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 70057 73304 0 57908 176570 0 0 69608 0 0 199240 86040 0 158835 3918...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17126 14982 0 0 115042 142158 0 107588 59293 0 85718 10...
output:
result:
Test #8:
score: 0
Memory Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 91134 0 0 143868 0 6759 102088 18267 0 153120 0 0 3601 ...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
200000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...