QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217519 | #7108. Couleur | Yangmf | AC ✓ | 3234ms | 64072kb | C++17 | 4.0kb | 2023-10-16 22:33:10 | 2023-10-16 22:33:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define error \
{ \
cout << "No\n"; \
return; \
}
#define all(x) (x.begin(), x.end())
const int N = 1e6 + 10;
int a[N], p[N];
int n, m;
int res;
map<int, int> mp; // 以l为索引
multiset<int> st;
struct SegT
{
int lc, rc;
int su;
#define lc(p) t[p].lc
#define rc(p) t[p].rc
#define su(p) t[p].su
} t[N << 2];
int tot;
void up(int p)
{
su(p) = su(lc(p)) + su(rc(p));
}
int build(int l, int r)
{
int p = ++tot;
if (l == r)
{
return p;
}
int mid = (l + r) >> 1;
t[p].lc = build(l, mid);
t[p].rc = build(mid + 1, r);
up(p);
return p;
}
int insert(int now, int l, int r, int x, int val)
{
int p = ++tot;
t[p] = t[now];
if (l == r)
{
t[p].su++;
return p;
}
int mid = (l + r) >> 1;
if (x <= mid)
{
lc(p) = insert(lc(now), l, mid, x, val);
}
else
{
rc(p) = insert(rc(now), mid + 1, r, x, val);
}
up(p);
return p;
}
int ask(int p, int L, int R, int l = 1, int r = n)
{
if (l>r||L>R)
return 0;
int mid = (l + r) >> 1;
if (L <= l && r <= R)
return su(p);
if (R <= mid)
return ask(lc(p), L, R, l, mid);
else if (L > mid)
return ask(rc(p), L, R, mid + 1, r);
else
return ask(lc(p), L, R, l, mid) + ask(rc(p), L, R, mid + 1, r);
}
int root[N];
int get(int L, int R, int l, int r)
{
if(L>R) return 0;
if(L<=1) return ask(root[R],l,r);
if(R>n) return ask(root[n],l,r)-ask(root[L-1],l,r);
return ask(root[R], l, r) - ask(root[L-1], l, r);
}
void split(int ps)
{
auto it = mp.upper_bound(ps);
int l = prev(it)->first, r = (it->first) - 1;
int ans = prev(it)->second;
// cout<<l<<" "<<ps<<" "<<r<<"\n";
if (ps - l < r - ps)
{
int ans1, ans2;
ans1 = ans2 = 0;
for (int i = l; i < ps; i++)
{
int x = a[i];
ans1 += get(l, i, x + 1, n);
}
ans2 = ans - ans1 - get(l, ps, a[ps] + 1, n);
for (int i = l; i <= ps; i++)
{
ans2 -= get(ps + 1, r, 1, a[i] - 1);
}
st.erase(st.lower_bound(ans));
mp[l]=ans1;
mp[ps + 1]=ans2;
st.insert(ans1);
st.insert(ans2);
mp[ps]=0;
res = *prev(st.end());
}
else
{
// cout<<"Hello\n";
int ans1, ans2;
ans1 = ans2 = 0;
for (int i = ps + 1; i <= r; i++)
{
int x = a[i];
ans2 += get(ps + 1, i, x + 1, n);
}
// cout << get1(ps, r, 1, a[ps] - 1) << "\n";
ans1 = ans - ans2 - get(ps, r, 1, a[ps] - 1);
for (int i = ps; i <= r; i++)
{
ans1 -= get(l, ps-1, a[i] + 1, n);
}
// cout<<ans1<<" "<<ans2<<"\n";
st.erase(st.lower_bound(ans));
mp[l]=ans1;
st.insert(ans1);
st.insert(ans2);
mp[ps+1]=ans2;
mp[ps]=0;
res = *prev(st.end());
}
}
void solve()
{
st.clear();
mp.clear();
tot = 0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
{
int x = a[i];
root[i] = insert(root[i - 1], 1, n, x, 1);
res += ask(root[i-1],x+1,n);
}
mp[1]=res;
mp[n+1]=0;
mp[0]=0;
// cout<<res<<"\n";
st.insert(res);
for (int i = 1; i <= n; i++)
{
cout << res << " ";
int ps = res ^ p[i];
// cout<<ps<<"\n";
// cout<<"\n";
// 使ps位置的数失效
split(ps);
}
cout<<"\n";
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9500kb
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: 3234ms
memory: 64072kb
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 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed