QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135429 | #4425. Cake | Sommohito# | RE | 0ms | 0kb | C++20 | 3.7kb | 2023-08-05 15:12:43 | 2023-08-05 15:12:46 |
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);
unordered_map<int,int> mp[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})
mp[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;
if(mp[k][x][y].count(aa[i])) continue;
mp[k][x][y][aa[i]] = i;
}
lpar[k]=0;
linc[k]=0;
}
int gt(int k,int val,int x,int y)
{
debug(lpar[k],linc[k]);
debug(val,x,y,mp[k][x][y%2]);
if(mp[k][x^lpar[k]][(y+linc[k])%2].count(val))
return mp[k][x^lpar[k]][(y+linc[k])%2][val];
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);
map<pii,int> go;
int lim = (n-1)/K+1;
for(int i=0;i<n;i++)
{
auto [x,y] = a[i];
if(x>y) swap(x,y);
if(go.count({x,y}) == 0)
{
go[{x,y}] = go.size()+1;
}
aa[i]=go[{x,y}];
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);
// debug(go.count({x,y}));
if(go.count({x,y}) == 0)
{
return no();
}
bb[i]=go[{x,y}];
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
Runtime Error
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...