QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#522927 | #7108. Couleur | mhw# | ML | 0ms | 0kb | C++20 | 5.1kb | 2024-08-17 16:52:31 | 2024-08-17 16:52:33 |
answer
#include<bits/stdc++.h>
#define ll long long
#define pii make_pair
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=b;i>=a;--i)
using namespace std;
inline ll read() {
ll x=0,f=1;
char c=getchar();
while (c<'0' || c>'9') {
if (c=='-') f=-1;
c=getchar();
}
while (c>='0' && c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void print(ll x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
return ;
}
inline void pprint(ll x) {
print(x);
puts("");
}
const int N = 2e5 + 7;
int n;
int a[N];
struct seg {
int v,ls,rs;
} t[N<<8];
int rt[N<<2],cnt;
void pushup(int o) {
t[o].v=t[t[o].ls].v+t[t[o].rs].v;
}
void change(int lsto,int &o,int l,int r,int q,int v) {
if(!o) o=++cnt;
if(l==r) {
t[o].v+=v;
return ;
}
int mid=l+r>>1;
if(q<=mid) { //��������������
t[o].rs=t[lsto].rs;//������ֱ��copy
t[o].ls=++cnt;
t[t[o].ls]=t[t[lsto].ls];//������copy�ṹ�壬Ȼ�����
change(t[lsto].ls,t[o].ls,l,mid,q,v);
} else {
t[o].ls=t[lsto].ls;
t[o].rs=++cnt;
t[t[o].rs]=t[t[lsto].rs];
change(t[lsto].rs,t[o].rs,mid+1,r,q,v);
}
pushup(o);
}
int query_rnk(int o1, int o2, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) {
return t[o2].v - t[o1].v;
}
int mid = l + r >> 1, ans = 0;
if(ql <= mid) ans += query_rnk(t[o1].ls, t[o2].ls, l, mid, ql, qr);
if(qr > mid) ans += query_rnk(t[o1].rs, t[o2].rs, mid + 1, r, ql, qr);
return ans;
}
int get_rnk(int x, int y, ll q) {
if(q == 1)
return 1;
return query_rnk(rt[x-1], rt[y], 1, n, 1, q - 1) + 1;
}
ll c[N];
void add(int x, ll y){
while(x <= n){
c[x] += y;
x += x & -x;
}
}
ll ask(int x){
ll res = 0;
while(x > 0){
res += c[x];
x -= x & -x;
}
return res;
}
ll calc(int x, int y){
ll ans = 0;
for(int i = y; i >= x; --i){
ans += ask(a[i] - 1);
add(a[i], 1);
}
for(int i = y; i >= x; --i)
add(a[i], -1);
return ans;
}
ll get_min(int x, int y, ll q){
return get_rnk(x, y, q) - 1;
}
ll get_max(int x, int y, ll q){
return (y - x + 1) - (get_rnk(x, y, q + 1) - 1);
}
void slv() {
memset(rt,0,sizeof rt);
memset(t,0,sizeof t);
// memset(c,0,sizeof c);
// memset(a,0,sizeof a);
cnt=0;
cin>>n;
for(int i = 1; i <= n; i++) {
cin>>a[i];
change(rt[i-1], rt[i], 1, n, a[i], 1);
}
set<int> st;
st.insert(0);
st.insert(n + 1);
map<pair<int, int>, int> q;
set<int>num;
unordered_map<int,int>mp;
int t1=calc(1,n);
q[{1,n}]=t1;
num.insert(t1);
mp[t1]++;
cout << t1 << ' ';
ll lst = t1;
for(int i = 1; i < n; ++i) {
int pos , out;
cin>>pos;
pos ^= lst;
int l = *(--st.upper_bound(pos)) + 1;
int r = *(st.upper_bound(pos)) - 1;
// cout << pos << " " << l << " " << r << endl;
if(abs(l - pos) > abs(r - pos)) { //右边小暴力右边
ll ans1 = 0;
if(pos + 1 <= r) ans1 = calc(pos + 1, r);
ll tot_lft_max = get_max(l, pos - 1, a[pos]);
ll tot_rgt_min = 0;
if(pos + 1 <= r) tot_rgt_min = get_min(pos + 1, r, a[pos]);
ll _sum = 0;
for(int j = pos + 1; j <= r; ++j) {
_sum += get_max(l, pos - 1, a[j]);
}
ll lst_ans = q[pii(l, r)];
// cout << "lst_ans " << lst_ans << endl;
// cout << "tot_lft_max " << tot_lft_max << endl;
// cout << "tot_rgt_min " << tot_rgt_min << endl;
// cout << "_sum " << _sum << endl;
ll ans2 = lst_ans - tot_lft_max - tot_rgt_min - _sum - ans1;
// cout << ans1 << ' ' << ans2 <<endl;
q[pii(l, pos - 1)] = ans2;
q[pii(pos + 1, r)] = ans1;
mp[lst_ans]--;
if(mp[lst_ans]==0) num.erase(lst_ans);
mp[ans1]++; mp[ans2]++;
num.insert(ans1),num.insert(ans2);
out = *num.rbegin();
} else { //左边小暴力左边
ll ans1 = 0;
if(l <= pos - 1) ans1 = calc(l, pos - 1);
ll tot_lft_max = 0;
if(l <= pos - 1) tot_lft_max = get_max(l, pos - 1, a[pos]);
ll tot_rgt_min = get_min(pos + 1, r, a[pos]);
ll _sum = 0;
for(int j = l; j <= pos - 1; ++j){
_sum += get_min(pos + 1, r, a[j]);
}
ll lst_ans = q[pii(l, r)];
ll ans2 = lst_ans - tot_lft_max - tot_rgt_min - _sum - ans1;
q[pii(l, pos - 1)] = ans1;
q[pii(pos + 1, r)] = ans2;
mp[lst_ans]--;
if(mp[lst_ans]==0) num.erase(lst_ans);
mp[ans1]++; mp[ans2]++;
num.insert(ans1),num.insert(ans2);
out = *num.rbegin();
}
cout << out << ' ';
lst = out;
st.insert(pos);
}
int x;cin>>x;
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T; cin>>T;
while(T--) {
slv();
}
return 0;
}
/*
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
*/
詳細信息
Test #1:
score: 0
Memory Limit Exceeded
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