QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#315583 | #7901. Basic Substring Structure | lmeowdn | WA | 14ms | 13440kb | C++14 | 3.1kb | 2024-01-27 14:26:07 | 2024-01-27 14:26:08 |
Judging History
answer
//vanitas vanitatum et omnia vanitas
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=2e5+5,base=19260817,mod=1e9+7;
int n,a[N],b[N],h[N],d[N],ans;
map<int,int> f[N];
int hsh(int l,int r) {
return (h[r]-h[l-1]*b[r-l+1]%mod+mod)%mod;
}
int lcp(int x,int y) {
int k=0,l=1,r=n-y+1;
while(l<=r) {
int mid=l+r>>1;
if(hsh(x,x+mid-1)==hsh(y,y+mid-1)) k=mid, l=mid+1;
else r=mid-1;
}
return k;
}
void solve() {
n=read(); rep(i,1,n) a[i]=read();
b[0]=1; rep(i,1,n) b[i]=b[i-1]*base%mod;
rep(i,1,n) h[i]=(h[i-1]*base+a[i])%mod;
rep(i,0,n+1) d[i]=0, f[i].clear(); ans=0;
rep(i,1,n) {
//cout<<"WwwwwwwwwwwwwwwwwwwwwwwwwwwwwK "<<i<<endl;
int k=lcp(1,i);
int j=i+k-1; ans+=k;
if(i!=1&&i<=j) {
d[i]+=j-i+1, d[i+1]+=i-j-2, d[j+2]+=1;
//cout<<"RED "<<i<<" "<<j<<" "<<j-i+1<<endl;;
}
if(j<n) {
int p=lcp(k+2,j+2)+1; chmin(p,j-k);
//cout<<" "<<i<<" "<<j<<" "<<k<<" "<<p<<" "<<a[j+1]<<" "<<a[i+p]<<endl;
if(p<j-k||a[k+1]!=a[i+p]) {
f[j+1][a[k+1]]+=p;
} else {
int q=lcp(j+2,i+p+1)+1;
//cout<<" "<<q<<endl;
f[j+1][a[k+1]]+=p+q;
}
}
int t=min(i-1,k);
//cout<<i<<" "<<t<<" "<<k<<endl;
if(t) {
d[1]+=k, d[2]-=k+1, d[t+1]-=k-t, d[t+2]+=k-t+1;
//cout<<"DER "<<1<<" "<<t<<" "<<k<<endl;
}
if(i>k+1&&j<n) {
int p=lcp(k+2,j+2)+1;
//cout<<" "<<i<<" "<<k+1<<" "<<a[j+1]<<" "<<p<<endl;
f[k+1][a[j+1]]+=p;
}
}
rep(i,1,n) d[i]+=d[i-1];
rep(i,1,n) d[i]+=d[i-1]; int pans=0;
rep(i,1,n) {
int res=ans-d[i], ex=0;
//cout<<d[i]<<endl;
for(auto [x,y]:f[i]) chmax(ex,y);
res+=ex; pans+=(res^i);
//cout<<i<<" "<<res<<endl;
}
printf("%lld\n",pans);
}
signed main() {
int T=read(); while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 13440kb
input:
2 4 2 1 1 2 12 1 1 4 5 1 4 1 9 1 9 8 10
output:
15 217
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 14ms
memory: 13088kb
input:
10000 8 2 1 2 1 1 1 2 2 9 2 2 1 2 1 2 1 2 1 15 2 1 2 1 1 1 1 2 2 1 2 1 2 2 1 2 1 1 10 2 1 1 1 2 2 1 1 2 2 3 2 1 2 11 1 2 2 1 1 2 1 2 2 1 1 14 2 1 1 1 1 2 1 1 1 2 2 1 2 1 12 2 2 2 1 2 2 2 1 1 2 1 2 4 2 1 1 2 8 1 2 2 2 1 2 1 1 8 1 1 2 1 2 1 1 1 6 2 1 1 1 2 2 14 2 2 1 1 1 1 2 2 2 1 2 2 1 1 10 1 2 2 1 1...
output:
91 129 347 3 211 9 252 363 277 15 95 107 58 348 225 3 335 352 388 303 3 19 121 66 15 83 36 261 11 64 28 90 87 101 252 186 21 48 322 63 102 20 24 61 319 363 266 315 347 281 324 282 230 310 3 331 54 328 8 76 32 148 320 39 338 89 242 3 166 346 245 20 155 3 405 393 392 81 271 361 20 54 21 279 3 17 351 3...
result:
wrong answer 1st lines differ - expected: '94', found: '91'