QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#457029#7108. CouleurmanizareAC ✓1688ms63352kbC++144.0kb2024-06-28 22:12:062024-06-28 22:12:07

Judging History

你现在查看的是最新测评结果

  • [2024-06-28 22:12:07]
  • 评测
  • 测评结果:AC
  • 用时:1688ms
  • 内存:63352kb
  • [2024-06-28 22:12:06]
  • 提交

answer

#include <bits/stdc++.h>
 

#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define ld long double
#define int long long 
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= b;i++)
#define per(i, a , b) for(int i=a;i >= b;i--)
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10  , maxq = 1e5 ,  N = 1e5 +1 , lg = 20 , sq =1700   , inf = 1e18, maxk = 2022 , mod = 1e9 + 7;
set <pii> s , s2; 
int ri[maxn] , fen[maxn] , n , a[maxn]  , z  = 0; 
vector <int> vec[maxn] ;

void fupd(int x , int w){
  while(x <= n){
    fen[x] += w;
    x += x&-x ;
  }
}
int fque(int x){
  int ans =0 ;
  while(x){
    ans += fen[x] ; 
    x -= x&-x ;
  }
  return ans;  
}

int inv(int l , int r){
  int ans = 0;
  per(i , r , l){
    ans += fque(a[i]-1) ;
    fupd(a[i] , 1) ;
  }
  rep(i , l , r)fupd(a[i], -1) ; 
  return ans ; 
}

void add(int l , int r , int t){
  if(t==-1)t = inv(l , r) ;
  ri[l] = r ;
  s.insert({l,t}) ;s2.insert({t,l}); 
}
void rem(int l){
  auto x = s.lower_bound((pii){l+1,-1}) ;
  x--;
  s2.erase({(*x).S , (*x).F});
  s.erase(*x) ;
}

int cp(){
  if(sz(s2) == 0){
    cout << "wtf\n" ;return 0 ; 
  }
  return (*s2.rbegin()).F  ; 
}

int ro[maxn] ;
struct node{
  int l ,r , t = 0 ;
  node(){
    t =0 ;
  }
};int c =1; 
node seg[maxn*20] ;
int bui(int l = 1 , int r =n ){
  int mid = (l+r)/2;
  if(l == r){
    seg[c].t =0 ;
    c++;
    return c-1 ;
  }
  int i1 = bui(l,mid) , i2 = bui(mid+1,r) ;
  seg[c].t = seg[i1].t + seg[i2].t ;
  seg[c].l = i1 ;
  seg[c].r = i2 ;
  c++ ;
  return c-1 ;
}
int upd(int x ,int p  ,int l= 1 ,int r = n){
  int mid = (l+r)/2 , pl =p<<1 ,pr =p<<1|1;
  if(l == r){
    seg[c].t = 1;c++;
    return c-1;
  }
  if(mid >= x){
    int i1 = upd(x , seg[p].l , l , mid); 
    seg[c].l = i1 ;
    seg[c].r = seg[p].r ;
  }else{
    int i1 = upd(x, seg[p].r , mid+1,r) ;
    seg[c].l = seg[p].l ;
    seg[c].r = i1 ;
  }
  seg[c].t = seg[seg[c].l].t + seg[seg[c].r].t ; 
  c++; 
  return c-1 ;
}
int que(int p , int le ,int ri ,int l =1 ,int r = n){
  int mid = (l+r)/2 ; 
  if(le > r || l > ri)return 0 ;
  if(le <= l && r <= ri)return seg[p].t ;
  return que(seg[p].l , le,ri,l,mid)  + que(seg[p].r , le , ri , mid+1 ,r) ;
}
void ch(int x){
  auto d= s.lower_bound((pii){x+1,-1}) ; 
  d-- ;
  int l = (*d).F , w = (*d).S ,r ;
  r = ri[l] ;
  if(l == r){
    rem(x) ;
    return ;
  }
  if(r == x){
    rem(x) ;
    add(l, r-1 , w-que(ro[a[x]+1] , l , r-1)) ;  
    return ; 
  }
  if(l == x){
    rem(x) ;
    add(l+1 , r , w-(r-l - que(ro[a[x]] , l+1 , r))) ;
    return ; 
  }
  if(r-x > x-l){
    int w1 = inv(l , x-1) ;
    w -= w1 ;
    rep(i , l , x){
      w -= (r-x+1 - que(ro[a[i]] , x ,r)) ;
    }
    rem(x) ;
    add(l , x-1 , w1) ;
    add(x+1 ,r , w ) ;
    return ; 
  }else{
    int w1 = inv(x+1 , r) ;
    w-=w1 ;
    rep(i , x ,r){
      w -= que(ro[a[i]+1] , l , x) ;
    }
    rem(x);
    add(l , x-1 , w) ;
    add(x+1 , r, w1) ;
    return ; 
  }
}


signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);   
  int T ;cin >> T; 
  while(T--){
    c = 1 ;
    cin >> n ;
    s.clear() ; s2.clear() ;
    rep(i ,1 , n)vec[i].clear() ; 
    rep(i ,1 ,n){
      cin>> a[i] ; 
      vec[a[i]].pb(i) ;
    }
    ro[n+1] = bui() ;
    per(i , n , 1){
      ro[i] = ro[i+1] ;
      for(int x : vec[i])ro[i] = upd(x , ro[i]) ;
    }
    add(1 , n , -1) ;  
    z = cp() ;
    cout << z << " " ; 
    rep(i , 1, n){
      int p ;
      cin >> p ;
      if(i == n)continue ;
      p ^= z ;
      ch(p) ;
      z = cp() ;
      cout << z << " " ;
    }
    cout << "\n" ;
  }
}
/*
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
*/

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 54708kb

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: 1688ms
memory: 63352kb

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