QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591245 | #7108. Couleur | feng | RE | 0ms | 0kb | C++20 | 2.9kb | 2024-09-26 14:56:27 | 2024-09-26 14:56:28 |
answer
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define endl '\n'
#define PII pair<int,int>
using namespace std;
const int maxn = 1e5;
vector<int> A(maxn + 5), p(maxn + 5);
set<int> pos;
int idx = 0;
struct node {
int ch[2];
int s;
};
vector<node> tree(maxn * 20 + 5);
vector<int> root(maxn + 5);
//vector<int> test(maxn);
map<int, ll> mp;
multiset<ll> s;
void insert(int x, int& y, int num, int l, int r) {
y = ++idx;
tree[y] = tree[x];
tree[y].s++;
if (l == r) return;
int mid = l + r >> 1;
if (num <= mid) insert(tree[x].ch[0], tree[y].ch[0], num, l, mid);
else insert(tree[x].ch[1], tree[y].ch[1], num, mid + 1, r);
}
ll query(int x, int y, int l, int r, int u, int v) {
if (u > v) return 0;
if (u <= l && v >= r)
return tree[y].s - tree[x].s;
int mid = l + r >> 1;
ll sum = 0;
if (u <= mid) sum += query(tree[x].ch[0], tree[y].ch[0], l, mid, u, v);
if (v > mid) sum += query(tree[x].ch[1], tree[y].ch[1], mid + 1, r, u, v);
return sum;
}
void solve() {
s.clear();
pos.clear();
mp.clear();
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> A[i];
for (int i = 1; i <= n; ++i) cin >> p[i];
int idx = 0;
for (int i = 1; i <= n; ++i)
insert(root[i - 1], root[i], A[i], 1, n);
ll now = 0;
int l = 1, r = n;
for (int i = 1; i < n; ++i) {
if (A[i] == 1) continue;
now += query(root[i], root[n], 1, n, 1, A[i] - 1);
}
s.insert(now);
mp[1] = now;
mp[n] = 0;
pos.insert(0);
pos.insert(n + 1);
cout << now;
for (int i = 1; i < n; ++i) {
int x = now ^ p[i];
auto it = pos.lower_bound(x);
r = *it;
l = *prev(it);
l++; r--;
ll sum = 0;
ll old = mp[l];
s.erase(s.find(old));
if (A[x] != n) sum += query(root[l - 1], root[x - 1], 1, n, A[x] + 1, n);
if (A[x] != 1) sum += query(root[x], root[r], 1, n, 1, A[x] - 1);
if (x - l > r - x) {
//枚举右边
ll a = 0;
for (int j = x + 1; j < r; ++j) {
if (A[j] == 1) continue;
a += query(root[j], root[r], 1, n, 1, A[j] - 1);
}
for (int j = x + 1; j <= r; ++j) {
if (A[j] == n) continue;
sum += query(root[l - 1], root[x - 1], 1, n, A[j] + 1, n);
}
if(x + 1 <= r) mp[x + 1] = a;
if(l <= x - 1) mp[l] = mp[l] - a - sum;
s.insert(mp[l]);
s.insert(mp[x + 1]);
}
else {
ll a = 0;
for (int j = l; j < x - 1; ++j) {
if (A[j] == 1) continue;
a += query(root[j], root[x - 1], 1, n, 1, A[j] - 1);
}
for (int j = l; j <= x - 1; ++j) {
if (A[j] == 1) continue;
sum += query(root[x], root[r], 1, n, 1, A[j] - 1);
}
if(l <= x - 1) mp[l] = a;
if(x + 1 <= r) mp[x + 1] = old - a - sum;
s.insert(mp[l]);
s.insert(mp[x + 1]);
}
pos.insert(x);
now = *prev(s.end());
cout << " " << now;
}
cout << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(2);
int T; cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
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