QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#167842 | #7108. Couleur | Delay_for_five_minutes | TL | 2ms | 5508kb | C++14 | 4.4kb | 2023-09-07 17:48:11 | 2023-09-07 17:48:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[100005];
int B ;
ll sum[325][325] ;
int be[100005] , L[325] , R[325];
vector<int> va[325];
struct BIT {
int c[100005];
void reset() {
for(int i = 1;i <= n;i++) c[i] = 0;
}
void add(int u,int v) {
while(u <= n) {
c[u] += v;u += (u & -u) ;
}
}
int qry(int u) {
int ans = 0;
while(u) {
ans += c[u] ; u -= (u & -u);
}
return ans;
}
}fen ;
int cal_inv(int l,int r)
{
int ans = 0;
for(int i = r;i >= l;i--) {
ans += fen.qry(a[i] - 1) ;
fen.add(a[i] , 1) ;
}
for(int i = l;i <= r;i++) fen.add(a[i], -1);
return ans;
}
vector<int> get(int l,int r)
{
vector<int> ans;
for(int i = l;i <= r;i++) ans.push_back(a[i]) ;
return ans;
}
ll gb(vector<int>& a,vector<int> &b) ///a[i] > b[j];
{
int np = 0;
ll ans = 0;
for(int i = 0;i < a.size();i++) {
while(np < b.size() && b[np] < a[i]) np++;
ans += np;
}
return ans;
}
ll sums[325][100005];
void init()
{
B = sqrt(n) ;
for(int i = 1;i <= n;i++) {
be[i] = be[i - 1];
if(i % B == 1 || B == 1) {be[i]++; L[be[i]] = i ; R[be[i - 1]] = i - 1;}
}
R[be[n]] = n;
for(int i = 1;i <= be[n];i++) {va[i] = get(L[i] , R[i]) ; sort(va[i].begin() , va[i].end()) ;}
for(int i = 1;i <= be[n];i++) {
for(int j = i;j <= be[n];j++) {
if(i == j) sum[i][j] = cal_inv(L[i] , R[i]) ;
else {
sum[i][j] = gb(va[i] , va[j]) ;
}
}
}
for(int i = 1;i <= be[n];i++) {
for(int j = i + 1;j <= be[n];j++) sum[i][j] += sum[i][j - 1];
}
for(int j = be[n] ; j >= 1;j--) {
for(int i = j - 1;i >= 1;i--) sum[i][j] += sum[i + 1][j] ;
}
for(int i = 1;i <= be[n];i++) {
for(int j = 1;j <= n;j++) {
if(L[i] <= j && j <= R[i]) sums[i][j] = sums[i][j - 1];
else if(j < L[i]) {
sums[i][j] = sums[i][j - 1] + (lower_bound(va[i].begin() , va[i].end() , a[j]) - va[i].begin()) ;
}
else {
sums[i][j] = sums[i][j - 1] + (va[i].size() - (upper_bound(va[i].begin() , va[i].end() , a[j]) - va[i].begin())) ;
}
}
}
}
ll qry(int l,int r)
{
if(be[l] == be[r]) {
return cal_inv(l , r) ;
}
int Lp = be[l] , Rp = be[r] ;
ll ans = sum[Lp + 1][Rp - 1];
// printf("B %lld\n" , ans);
for(int i = Lp +1; i <= Rp-1;i++) {
ans += sums[i][R[Lp]] - sums[i][l - 1];
ans += sums[i][r] - sums[i][L[Rp] - 1] ;
}
vector<int> x = get(l , R[Lp]) , y = get(L[Rp] , r) ;
sort(x.begin(),x.end()) ; sort(y.begin() , y.end()) ;
ans += gb(x , y);
// printf("After %lld\n" , ans);
ans += cal_inv(l , R[Lp]) + cal_inv(L[Rp] , r) ;
return ans;
}
struct val {
int l , r;
ll v;
};
bool operator < (val x,val y)
{
return x.r < y.r;
}
void solv()
{
set<val> st;
multiset<ll> vals;
cin>>n;
for(int i=1;i<=n;++i)cin>>a[i];
init();
ll z = qry(1 , n) ; cout << z <<' ' ;
st.insert((val){1 , n , z}) ; vals.insert(z) ;
// puts("ojbk") ; printf("Z %lld\n",z) ;
// exit(0) ;
// printf("ASK %lld\n",qry(2 , n)) ;
for(int i = 1;i <= n;i++) {
long long x ; cin >> x;
x ^= z;
auto it = st.lower_bound((val){x , x , 0}) ;
int ul = it->l , ur = it->r;
ll uv = it->v ;
vals.erase(vals.find(uv)) ;
st.erase(it) ;
//printf("ul %d , ur %d\n",ul,ur) ;
if(x - 1 >= ul) {
ll nv = qry(ul , x - 1) ; //printf("Lnv %lld\n",nv);
st.insert((val){ul , x - 1 , nv}) ; vals.insert(nv) ;
}
if(x + 1 <= ur) {
ll nv = qry(x + 1 , ur); //printf("Rnv %lld\n",nv);
st.insert((val){x + 1 , ur , nv}) ; vals.insert(nv) ;
}
z = 0;
if(vals.size()) {
auto it = vals.end() ; it-- ; z = (*it) ;
}
if(i != n) cout << z <<' ' ;
//printf("Z %lld\n",z) ;
}
cout << '\n' ;
}
int main(){
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
// freopen("in.txt","r",stdin) ;
int t;cin >> t;
while(t--) solv() ;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5508kb
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 ...