QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#171669 | #7108. Couleur | megrezbunny | AC ✓ | 1876ms | 77784kb | C++17 | 4.3kb | 2023-09-09 17:24:57 | 2023-09-09 17:24:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1e5+5;
multiset<ll, greater<ll>> mset;
int T;
int n;
int a[MAXN];
ll p[MAXN];
struct BIT
{
int t[MAXN];
inline int lowbit(int x) {return x&(-x);}
inline void clear() {memset(t, 0, sizeof(int)*(n+5));}
void add(int x)
{
while(x <= n)
{
++t[x];
x += lowbit(x);
}
}
int query(int x)
{
int ret = 0;
while(x)
{
ret += t[x];
x -= lowbit(x);
}
return ret;
}
}bit;
ll getinvcnt()
{
ll ret = 0;
for(int i = n; i >= 1; --i)
{
ret += bit.query(a[i]-1);
bit.add(a[i]);
}
return ret;
}
void myout(int id, ll ans)
{
if(id == n) printf("%lld",ans);
else printf("%lld ",ans);
}
struct node
{
int l,r; ll invcnt;
bool operator<(const node& ano) const
{
return l < ano.l;
}
};
set<node> qujian;
struct hjttree
{
struct hjtnode
{
int ls,rs,v;
}t[MAXN<<5];
#define mid ((l+r)>>1)
int root[MAXN], cnt;
void clear()
{
memset(t, 0, sizeof(hjtnode)*(cnt+2));
cnt = 0;
}
void build(int &o, int pre, int l, int r, int x)
{
o = ++cnt;
t[o] = t[pre];
++t[o].v;
if(l == r) return;
if(x <= mid) build(t[o].ls, t[pre].ls, l, mid, x);
else build(t[o].rs, t[pre].rs, mid+1, r, x);
}
int querycnt(int l, int r, int L, int R)
{
if(L > R) return 0;
return realcnt(root[r], root[l-1], 1, n, L, R);
}
//查询 ox - oy 有多少个在 [L,R] 的数
int realcnt(int ox, int oy, int l, int r, int L, int R)
{
if(L <= l && r <= R) return t[ox].v - t[oy].v;
int ret = 0;
if(L <= mid) ret += realcnt(t[ox].ls, t[oy].ls, l, mid, L, R);
if(R > mid) ret += realcnt(t[ox].rs, t[oy].rs, mid+1, r, L, R);
return ret;
}
}seg;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
bit.clear(); qujian.clear(); seg.clear();
for(int i = 1; i <= n; ++i)
{
scanf("%d",&a[i]);
seg.build(seg.root[i], seg.root[i-1], 1, n, a[i]);
}
for(int i = 1; i <= n; ++i) scanf("%lld",&p[i]);
ll invcnt = getinvcnt();
qujian.insert({1, n, invcnt}); mset.insert(invcnt);
myout(1, invcnt);
ll ans = invcnt;
for(int i = 1; i < n; ++i)
{
p[i] ^= ans;
//按p[i]位置分裂
auto it = qujian.upper_bound((node){p[i], 0, 0});
--it;
int l = it->l, r = it->r;
ll invcntleft = 0, invcntright = 0, invcntcross = 0, invcntp = 0;
//启发式处理
if(p[i]-l <= r-p[i]) //左边短
{
for(int j = l; j <= p[i]-1; ++j)
{
invcntleft += seg.querycnt(j+1, p[i]-1, 1, a[j]-1);
invcntcross += seg.querycnt(p[i]+1, r, 1, a[j]-1);
}
invcntp = seg.querycnt(l,p[i]-1,a[p[i]]+1,n) + seg.querycnt(p[i]+1,r,1,a[p[i]]-1);
invcntright = it->invcnt - invcntleft - invcntcross - invcntp;
}
else //右边短
{
for(int j = p[i]+1; j <= r; ++j)
{
invcntright += seg.querycnt(j+1, r, 1, a[j]-1);
invcntcross += seg.querycnt(l, p[i]-1, a[j]+1, n);
}
invcntp = seg.querycnt(l,p[i]-1,a[p[i]]+1,n) + seg.querycnt(p[i]+1,r,1,a[p[i]]-1);
invcntleft = it->invcnt - invcntright - invcntcross - invcntp;
}
//最后要插回去区间
mset.erase(mset.find(it->invcnt)); mset.insert(invcntleft); mset.insert(invcntright);
qujian.erase(it);
if(l <= p[i]-1) qujian.insert((node){l, p[i]-1, invcntleft});
if(p[i]+1 <= r) qujian.insert((node){p[i]+1, r, invcntright});
ans = *mset.begin();
myout(i, ans);
}
if(T) printf("\n");
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5876kb
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: 1876ms
memory: 77784kb
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