QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163272 | #7108. Couleur | ucup-team061 | TL | 0ms | 3448kb | C++20 | 3.9kb | 2023-09-03 23:16:34 | 2023-09-03 23:16:35 |
Judging History
answer
#include <bits/stdc++.h>
//#define int long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define eb emplace_back
#define eps 1e-6
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, M = 1e7 + 10;
int n, m, q, mx;
int t[M], ls[M], rs[M], rt[N], tot;
int update(int pre, int l, int r, int pos, int x){
int p = ++ tot;
t[p] = t[pre] + x;
ls[p] = ls[pre], rs[p] = rs[pre];
if(l == r) return p;
int mid = (l + r) >> 1;
if(pos <= mid) ls[p] = update(ls[pre], l, mid, pos, x);
else rs[p] = update(rs[pre], mid + 1, r , pos, x);
return p;
}
int query(int p, int l, int r, int ql, int qr){
if(ql > r || qr < l) return 0;
if(ql <= l && qr >= r) return t[p];
int mid = (l + r) >> 1;
return query(ls[p], l, mid, ql, qr) + query(rs[p], mid + 1, r, ql, qr);
}
int a[N], p[N];
int cal_rev(int l, int r){
int s = 0;
for(int i = l + 1; i <= r; i++){
s += query(rt[i], 1, mx, a[i] + 1, mx) - query(rt[l - 1], 1, mx, a[i] + 1, mx);
}
return s;
}
int cal_sum(int l1, int r1, int l2, int r2, int tp){
int s = 0;
if(tp == 1){//遍历左区间,从右区间找比左边元素小的个数
for(int i = l1; i <= r1; i++){
s += query(rt[r2], 1, mx, 1, a[i] - 1) - query(rt[l2 - 1], 1, mx, 1, a[i] - 1);
}
}else{//遍历右区间,从左区间找比右边元素大的个数
for(int i = l2; i <= r2; i++){
s += (query(rt[r1], 1, mx, 1, mx) - query(rt[r1], 1, mx, 1, a[i]));
s -= (query(rt[l1 - 1], 1, mx, 1, mx) - query(rt[l1 - 1], 1, mx, 1, a[i]));
}
}
return s;
}
int seg[N];
void clear_all(){
for(int i = 0; i <= n; i++){
seg[i] = 0, rt[i] = 0, a[i] = 0, p[i] = 0;
}
for(int i = 0; i <= tot; i++){
t[i] = 0, ls[i] = 0, rs[i] = 0;
}
tot = 0, mx = 0;
}
void solve(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], mx = max(mx, a[i]);
for(int i = 1; i <= n; i++) cin >> p[i];
int s = 0;
for(int i = 1; i <= n; i++){
rt[i] = update(rt[i - 1], 1, mx, a[i], 1);
s += i - query(rt[i], 1, mx, 1, a[i]);
}
set<int> pl;
map<pair<int, int>, int> mp;
multiset<int> sum;
mp[{1, n}] = s;
sum.insert(s);
seg[1] = n;
pl.insert(1), pl.insert(n + 1);
for(int i = 1; i <= n; i++){
s = *sum.rbegin();
cout << s << ' ';
p[i] ^= s;
int pos = *prev(upper_bound(pl.begin(), pl.end(), p[i]));
if(!seg[pos] || seg[pos] == pos) continue;
int l = pos, r = seg[pos];
//cout << "\n ## No." << i << ": " << l << ' ' << r << '\n';
auto ele = lower_bound(sum.begin(), sum.end(), mp[{l, r}]);
sum.erase(ele);
pl.erase(l);
int stot = mp[{l, r}], sl = 0, sr = 0, smid = 0;
if(p[i] - l <= r - p[i]){//左边小
sl = cal_rev(l, p[i] - 1);
smid = cal_sum(l, p[i] - 1, p[i] + 1, r, 1) + cal_sum(p[i], p[i], p[i] + 1, r, 1) + cal_sum(l, p[i] - 1, p[i], p[i], 2);
sr = stot - sl - smid;
}else{//右边小
sr = cal_rev(p[i] + 1, r);
smid = cal_sum(l, p[i] - 1, p[i] + 1, r, 2) + cal_sum(p[i], p[i], p[i] + 1, r, 1) + cal_sum(l, p[i] - 1, p[i], p[i], 2);
sl = stot - sr - smid;
}
//cout << "p = " << p[i] << " sl = " << sl << " sr = " << sr << " smid = " << smid << '\n';
mp[{l, p[i] - 1}] = sl;
mp[{p[i] + 1, r}] = sr;
sum.insert(sl), sum.insert(sr);
if(l < p[i]) pl.insert(l), seg[l] = p[i] - 1;
if(r > p[i]) pl.insert(p[i] + 1), seg[p[i] + 1] = r;
}
cout << '\n';
clear_all();
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3448kb
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
Time Limit Exceeded
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 ...