QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#162038 | #7108. Couleur | ucup-team1198# | AC ✓ | 1606ms | 65744kb | C++20 | 4.1kb | 2023-09-03 03:44:39 | 2023-09-03 03:44:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()
struct Node{
int sum;
int left;
int right;
Node(): sum(0), left(-1), right(-1) {};
Node(int sum): sum(sum), left(-1), right(-1) {};
};
Node nodes[5'000'000];
int nodes_cnt = 0;
int node(int sum) {
nodes[nodes_cnt] = Node(sum);
++nodes_cnt;
return nodes_cnt - 1;
}
int build(int left, int right) {
int ans = node(0);
if (left + 1 == right)
return ans;
int mid = (left + right) / 2;
nodes[ans].left = build(left, mid);
nodes[ans].right = build(mid, right);
return ans;
}
int add(int t, int left, int right, int i, int val) {
int ans = node(nodes[t].sum + val);
if (left + 1 == right)
return ans;
int mid = (left + right) / 2;
nodes[ans].left = nodes[t].left;
nodes[ans].right = nodes[t].right;
if (i < mid)
nodes[ans].left = add(nodes[t].left, left, mid, i, val);
else
nodes[ans].right = add(nodes[t].right, mid, right, i, val);
return ans;
}
int get_sum(int t1, int t2, int left, int right, int x, int y) {
if (y <= left || right <= x)
return 0;
if (x <= left && right <= y)
return nodes[t2].sum - nodes[t1].sum;
int mid = (left + right) / 2;
return get_sum(nodes[t1].left, nodes[t2].left, left, mid, x, y) +
get_sum(nodes[t1].right, nodes[t2].right, mid, right, x, y);
}
void solve() {
nodes_cnt = 0;
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; ++i) {
cin >> A[i];
--A[i];
}
vector<int> trees(n + 1);
trees[0] = build(0, n);
for (int i = 0; i < n; ++i)
trees[i + 1] = add(trees[i], 0, n, A[i], 1);
long long cur_sum = 0;
for (int i = 0; i < n; ++i)
cur_sum += get_sum(trees[0], trees[i], 0, n, A[i] + 1, n);
multiset<long long> all_vals;
all_vals.emplace(cur_sum);
map<pair<int, int>, long long> cur_segms;
cur_segms[make_pair(0, n)] = cur_sum;
for (int _ = 0; _ < n; ++_) {
long long pre = *all_vals.rbegin();
cout << pre;
if (_ + 1 != n)
cout << ' ';
long long z;
cin >> z;
long long i = z ^ pre;
--i;
//cerr << "i: " << i << '\n';
auto tmp = --cur_segms.lower_bound(make_pair(i + 1, i + 1));
auto [l, r] = tmp->first;
long long was = tmp->second;
cur_segms.erase(tmp);
all_vals.erase(all_vals.find(was));
if (i - l <= r - i - 1) {
long long new_val = 0;
for (int j = l; j < i; ++j) {
was -= get_sum(trees[j], trees[r], 0, n, 0, A[j]);
new_val += get_sum(trees[l], trees[j], 0, n, A[j] + 1, n);
}
was -= get_sum(trees[i], trees[r], 0, n, 0, A[i]);
if (i + 1 != r) {
cur_segms[make_pair(i + 1, r)] = was;
all_vals.emplace(was);
//cout << i + 1 << ',' << r << ' ' << was << '\n';
}
if (i != l) {
cur_segms[make_pair(l, i)] = new_val;
all_vals.emplace(new_val);
//cout << l << ',' << i << ' ' << new_val << '\n';
}
} else {
long long new_val = 0;
for (int j = r - 1; j > i; --j) {
was -= get_sum(trees[l], trees[j], 0, n, A[j] + 1, n);
new_val += get_sum(trees[j], trees[r], 0, n, 0, A[j]);
}
was -= get_sum(trees[l], trees[i], 0, n, A[i] + 1, n);
if (i + 1 != r) {
cur_segms[make_pair(i + 1, r)] = new_val;
all_vals.emplace(new_val);
//cout << i + 1 << ',' << r << ' ' << new_val << '\n';
}
if (i != l) {
cur_segms[make_pair(l, i)] = was;
all_vals.emplace(was);
//cout << l << ',' << i << ' ' << was << '\n';
}
}
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 62180kb
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: 0
Accepted
time: 1606ms
memory: 65744kb
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 0 0 0 0 0 0 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed