# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
129897 | alexpetrescu | Untitled (POI11_rot) | C++14 | 1082 ms | 65540 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <algorithm>
//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout
#define LOGN 18
long long ans;
struct myc {
int sz;
std::vector < int > v[LOGN + 1];
};
inline long long calc(myc &a, myc &b) {
long long ans = 0;
for (int i = 0; (1 << i) <= a.sz; i++) {
for (auto &x : a.v[i]) {
for (int j = 0; (1 << j) <= b.sz; j++) {
if (!b.v[j].empty() && b.v[j][0] < x) {
int rez = 0;
for (int pas = (1 << j) / 2; pas; pas >>= 1)
if (b.v[j][rez + pas] < x)
rez += pas;
ans += rez + 1;
}
}
}
}
return ans;
}
inline void goMerge(std::vector < int > &a, std::vector < int > &b) {
if (a.empty()) {
a = b;
return ;
}
if (b.empty())
return ;
int i = 0, j = 0, k = 0;
std::vector < int > c(a.size() + b.size());
while (i < (int)a.size() && j < (int)b.size()) {
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < (int)a.size())
c[k++] = a[i++];
while (j < (int)b.size())
c[k++] = b[j++];
a = c;
}
myc dfs() {
int x;
fscanf(fin, "%d", &x);
myc a;
if (x) {
a.sz = 1;
a.v[0].push_back(x);
return a;
}
myc b = dfs();
myc c = dfs();
long long r;
if (b.sz < c.sz)
r = calc(b, c);
else
r = calc(c, b);
ans += std::min(r, 1LL * b.sz * c.sz - r);
a.sz = b.sz + c.sz;
std::vector < int > tr;
for (int i = 0; (1 << i) <= a.sz; i++) {
goMerge(tr, b.v[i]);
goMerge(tr, c.v[i]);
if (a.sz & (1 << i)) {
for (int j = 0; j < (1 << i); j++) {
a.v[i].push_back(tr.back());
tr.pop_back();
}
std::reverse(a.v[i].begin(), a.v[i].end());
}
}
return a;
}
int main() {
int degeaba;
fscanf(fin, "%d", °eaba);
myc a = dfs();
fprintf(fout, "%lld\n", ans);
return 0;
}
Compilation message (stderr)
# | 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... |