Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
119144 | 陈家宝 | [Cqoi2011]动态逆序对 | C++ | 通过 | 100 | 411 MS | 3580 KB | 2434 | 2024-01-04 13:27:49 |
#include<bits/stdc++.h> using namespace std; int read() { int x(0); char c(getchar()); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x; } const int MAXN = 1e5 + 10; inline int lb(int x) { return x & -x; } struct Tree { vector<int> tree; Tree(int SZ) { tree.resize(SZ + 1); } void add(int pos, int val) { while (pos < tree.size()) tree[pos] += val, pos += lb(pos); } int sum(int pos) { int res(0); while (pos) res += tree[pos], pos -= lb(pos); return res; } }; struct Element { int b, c; explicit Element(int _b = 0, int _c = 0): b(_b), c(_c) {} }; bool operator<(Element lhs, Element rhs) { return lhs.b < rhs.b; } void cdq(vector<Element>& eles, int l, int r, vector<int>& ans, vector<Element>& usls, Tree& tree) { if (r - l == 1)return; int mid((l + r) >> 1); cdq(eles, l, mid, ans, usls, tree), cdq(eles, mid, r, ans, usls, tree); vector<Element>::iterator li(eles.begin() + l); for (vector<Element>::iterator ri(eles.begin() + mid); ri != eles.begin() + r; ++ri) { while (li != eles.begin() + mid && li->b < ri->b) tree.add((li++)->c, 1); ans[ri->c] += tree.sum(ri->c); } for (vector<Element>::iterator i(eles.begin() + l); i != li; ++i) tree.add(i->c, -1); merge(eles.begin() + l, eles.begin() + mid, eles.begin() + mid, eles.begin() + r, usls.begin() + l); while (l != r) eles[l] = usls[l], ++l; } int main() { int N(read()), M(read()); vector<Element> eles1(N), eles2(N), usls(N); vector<int> ans1(M + 2), ans2(M + 2); long long res(0); Tree t1(N), t2(M); for (int i(N), x; i; --i) { x = read() - 1; eles1[x].b = i; eles1[x].c = 1; eles2[x].b = N - i + 1; eles2[x].c = 1; res += N - i - t1.sum(x + 1); t1.add(x + 1, 1); } for (int i(M + 1), x; i > 1; --i) { x = read() - 1; eles1[x].c = eles2[x].c = i; } reverse(eles2.begin(), eles2.end()); cdq(eles1, 0, N, ans1, usls, t2); cdq(eles2, 0, N, ans2, usls, t2); for (int i(0); i != M; ++i) { cout << res << endl; res -= ans1[M - i + 1]; res -= ans2[M - i + 1]; } return 0; }