QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#260960 | #7108. Couleur | ricofx | RE | 1ms | 3356kb | C++20 | 3.8kb | 2023-11-22 17:05:34 | 2023-11-22 17:05:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
int n, a[N], p[N];
int Tr[N];
void add(int x, int v) {
for (; x <= n; x += x & -x) Tr[x] += v;
}
int qry(int x) {
int res = 0;
for (; x; x -= x & -x) res += Tr[x];
return res;
}
ll Calc(int l, int r) {
ll res = 0;
for (int i = l; i <= r; i++) {
add(a[i], 1);
res += i - l + 1 - qry(a[i]);
}
for (int i = l; i <= r; i++) {
add(a[i], -1);
}
return res;
}
#define mid ((l + r) >> 1)
int rt[N], tot;
struct Node {
int ls, rs, sum;
} tr[N * 30];
void insert(int &p, int l, int r, int pos) {
tr[++tot] = tr[p];
p = tot;
if (l == r) {
tr[p].sum++;
return;
}
if (pos <= mid) insert(tr[p].ls, l, mid, pos);
else insert(tr[p].rs, mid + 1, r, pos);
tr[p].sum = tr[tr[p].ls].sum + tr[tr[p].rs].sum;
}
int qry(int p, int l, int r, int L, int R) {
if (l > R || r < L || !p) return 0;
if (l >= L && r <= R) return tr[p].sum;
return qry(tr[p].ls, l, mid, L, R) + qry(tr[p].rs, mid + 1, r, L, R);
}
int qry(int L, int R, int l, int r) {
R = min(R, n);
if (L > R) return 0;
return qry(rt[R], 1, n, l, r) - qry(rt[L - 1], 1, n, l, r);
}
struct node {
int l, r;
mutable ll id;
node(int L, int R = -1, ll ID = -1) : l(L), r(R), id(ID) {}
inline bool operator < (const node &x) const {
return l < x.l;
}
};
#define IT set<node>::iterator
set<node> s;
IT split(int pos) {
IT it = s.lower_bound(node(pos));
if(it -> l == pos && it != s.end()) return it;
--it;
int v = it->id, l = it->l, r = it->r;
s.erase(it); s.insert(node(l, pos - 1, v));
return s.insert(node(pos, r, v)).first;
}
void assign(int l, int r, int id) {
if (l > r) return;
IT itr = split(r + 1), itl = split(l);
s.erase(itl, itr);
s.insert(node(l, r, id));
}
IT find(int pos) {
IT it = s.lower_bound(node(pos));
if(it -> l == pos && it != s.end()) return it;
--it;
return it;
}
multiset<ll> mx;
void solve() {
cin >> n;
s.clear(), mx.clear();
for (int i = 0; i <= tot; i++) tr[i].ls = tr[i].rs = tr[i].sum = 0;
tot = 0;
for (int i = 1; i <= n; i++) {
rt[i] = rt[i - 1];
cin >> a[i];
insert(rt[i], 1, n, a[i]);
}
for (int i = 1; i <= n; i++) cin >> p[i];
ll now = Calc(1, n), ttmp;
s.insert(node(1, n, now));
s.insert(node(n + 1, n + 1, -1));
mx.insert(now);
for (int i = 1; i <= n; i++) {
now = *mx.rbegin();
cout << now << ' ';
//return;
p[i] ^= now;
IT cur = find(p[i]);
int L = cur->l, R = cur->r, l, r;
ll V = cur->id;
mx.erase(mx.lower_bound(V));
if (p[i] - L < R - p[i]) {
l = L, r = p[i] - 1;
now = Calc(l, r);
ttmp = now;
now += qry(l, r, a[p[i]] + 1, n) + qry(p[i] + 1, R, 1, a[p[i]] - 1);
for (int j = l; j <= r; j++) {
now += qry(p[i] + 1, R, 1, a[j] - 1);
}
assign(l, r, ttmp), assign(p[i], p[i], 0), assign(p[i] + 1, R, V - now);
mx.insert(ttmp), mx.insert(V - now);
} else {
l = p[i] + 1, r = R;
now = Calc(l, r);
ttmp = now;
now += qry(L, p[i] - 1, a[p[i]] + 1, n) + qry(l, r, 1, a[p[i]] - 1);
for (int j = l; j <= r; j++) {
now += qry(L, p[i] - 1, a[j] + 1, n);
}
assign(l, r, ttmp), assign(p[i], p[i], 0), assign(L, p[i] - 1, V - now);
mx.insert(ttmp), mx.insert(V - now);
}
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3356kb
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10
output:
7 0 0 0 0 20 11 7 2 0 0 0 0 0 0 42 31 21 14 14 4 1 1 1 0 0 0 0 0 0
result:
ok 3 lines
Test #2:
score: -100
Runtime Error
input:
11116 10 10 5 10 3 6 4 8 5 9 8 31 27 24 11 12 3 0 2 3 1 10 8 2 7 2 8 10 1 10 9 10 6 5 2 13 2 1 0 1 3 1 10 7 10 7 6 1 3 10 6 7 9 21 18 10 1 6 5 4 8 9 10 10 2 10 4 8 8 5 7 2 6 7 20 10 9 1 15 0 4 2 9 7 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 6 3 5 2 7 10 9 1 4 8 10 1 10 1 3...
output:
21 18 16 12 10 6 4 1 1 0 12 12 10 10 4 4 4 2 1 0 20 16 9 5 3 3 3 0 0 0 22 14 8 8 5 5 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 12 7 4 4 2 2 1 0 0 20 18 8 3 1 1 0 0 0 0 45 21 21 10 3 3 3 0 0 0 17 11 8 2 1 1 1 0 0 0 13 4 1 0 0 0 0 0 0 0 29 27 22 15 9 7 4 3 1 0 26 16 9 2 1 1 1 1 1 ...