QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135453 | #4425. Cake | Sommohito# | TL | 0ms | 0kb | C++20 | 4.0kb | 2023-08-05 15:29:50 | 2023-08-05 15:29:54 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
#define ALL(x) x.begin(),x.end()
typedef pair<int,int> pii;
const int N = 5e5+5;
const int oo = 1e9+7;
const int K = sqrt(N);
vector<pii> vec[K][2][2];
bool par[N];
bool parb[N];
bool del[N];
int inc[N];
int lpar[K];
int linc[K];
int aa[N];
int bb[N];
int n;
void no()
{
cout<<"-1\n";
}
void build(const int k,int todel,int ii)
{
for(auto i: {0,1})
for(auto j: {0,1})
vec[k][i][j].clear();
for(int i=k*K;i<n and i< (k*K+K);i++)
{
if(todel == i)
del[i]=1;
if(del[i]) continue;
par[i]^=lpar[k];
inc[i]+=linc[k];
if(i<=ii)
{
par[i]^=1;
inc[i]+=1;
}
int x = par[i];
int y = (i+inc[i])%2;
vec[k][x][y].push_back({aa[i],i});
}
for(auto i: {0,1})
for(auto j: {0,1})
sort(ALL(vec[k][i][j]));
lpar[k]=0;
linc[k]=0;
}
int gt(int k,int val,int x,int y)
{
debug(lpar[k],linc[k]);
auto z = lower_bound(ALL(vec[k][x^lpar[k]][(y+linc[k])%2]), make_pair(val,-1));
if(z!=vec[k][x^lpar[k]][(y+linc[k])%2].end() and z->first == val)
return z->second;
else return oo;
}
void TEST_CASES()
{
cin>>n;
vector<pii> a(n),b(n);
for(int i=0;i<n;i++) cin>>a[i].first;
for(int i=0;i<n;i++) cin>>a[i].second;
for(int i=0;i<n;i++) cin>>b[i].first;
for(int i=0;i<n;i++) cin>>b[i].second;
debug(a,b);
vector<pii> go = a;
for(auto &[x,y]: go) if(x>y) swap(x,y);
sort(ALL(go));
go.erase(unique(ALL(go)), go.end());
int lim = (n-1)/K+1;
for(int i=0;i<n;i++)
{
auto [x,y] = a[i];
if(x>y) swap(x,y);
int ii=lower_bound(ALL(go),make_pair(x,y))-go.begin();
assert(ii<go.size());
aa[i]=ii;
par[i]=a[i].first!=x;
del[i]=0;
}
for(int i=0;i<n;i++)
{
auto [x,y] = b[i];
if(x>y) swap(x,y);
int ii=lower_bound(ALL(go),make_pair(x,y))-go.begin();
if(ii>=go.size())
return no();
if(go[ii] != make_pair(x,y)) return no();
bb[i] = ii;
parb[i] = b[i].first!=x;
}
// for(int i=0;i<n;i++) cout<<aa[i]<<" "; cout<<endl;
// for(int i=0;i<n;i++) cout<<par[i]<<" "; cout<<endl;
// for(int i=0;i<n;i++) cout<<bb[i]<<" "; cout<<endl;
// for(int i=0;i<n;i++) cout<<parb[i]<<" "; cout<<endl;
// debug(lim);
for(int i=0;i<lim;i++)
{
lpar[i]=0;
linc[i]=0;
build(i,-1,-1);
}
ll ans=0;
for(int i=0;i<n;i++)
{
int pos = oo;
for(int j=0;j<lim;j++)
{
pos=min(pos,gt(j,bb[i],parb[i],i));
pos=min(pos,gt(j,bb[i],!parb[i],i+1));
if(b[i].first==b[i].second)
{
pos=min(pos,gt(j,bb[i],parb[i],i+1));
pos=min(pos,gt(j,bb[i],!parb[i],i));
}
if(pos!=oo) break;
}
debug(i,pos);
if(pos==oo)
return no();
for(int j=0;j<pos/K;j++) lpar[j]^=1, linc[j]+=1;
int posi = pos+inc[pos]+linc[pos/K];
build(pos/K,pos,pos);
assert(posi>=i);
assert(posi<n);
ans+= posi-i;
}
cout<<ans<<"\n";
}
/*
1
2
1 2
2 3
2 1
3 2
3 2
2 1
*/
int32_t main()
{
#ifndef APURBA
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
//freopen("input.txt","r",stdin);
//freopen("out1.txt","w",stdout);
int t=1;
cin>>t;
while(t--)
{
TEST_CASES();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 500000 471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...