QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#522932 | #7108. Couleur | mhw# | TL | 1ms | 5936kb | C++20 | 5.1kb | 2024-08-17 16:56:48 | 2024-08-17 16:56:49 |
Judging History
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 = 1e5 + 7;
int n;
int a[N];
struct seg {
int v,ls,rs;
} t[N<<7];
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(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';
for(int i = 0; i <= n * 4; ++i) rt[i] = 0;
for(int i = 0; i <= cnt; ++i) t[i] = {0, 0, 0};
}
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: 100
Accepted
time: 1ms
memory: 5936kb
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 ...