QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#719981 | #9406. Triangle | konyakest | WA | 0ms | 48740kb | C++17 | 5.2kb | 2024-11-07 10:08:05 | 2024-11-07 10:08:05 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i,j,k) for(auto i=j;i<=(decltype(i))k;i++)
#define x first
#define y second
#define exec(...) [&](){__VA_ARGS__}()
#define endl '\n'
#define os ostream
#define pb push_back
#define view(x) begin(x),end(x)
#define lambda [&]
using namespace std;
using ll=long long;
template<typename T>void ckmin(T& x,T y){x=min(x,y);}
template<typename T>void ckmax(T& x,T y){x=max(x,y);}
#ifdef DEBUG
template<typename T1,typename T2>os& operator<<(os&,pair<T1,T2>);
template<typename T,typename=decltype(T().begin()),typename=enable_if_t<!is_same_v<decay_t<T>,string>>>os& operator<<(os& out,T x){auto n=0u;out<<"{";for(auto i:x) out<<i<<(++n==x.size()?"":",");return out<<"}";}
template<typename ...T>os& operator<<(os& out,tuple<T...> x){return apply(lambda(T... x){auto n=0u;out<<"{";((out<<x<<(++n==sizeof...(T)?"":",")),...);},x),out<<"}";}
template<typename T1,typename T2>os& operator<<(os& out,pair<T1,T2> x){return out<<tuple(x);}
#define debug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<" = "<<std::make_tuple(__VA_ARGS__)<<endl
#else
#define debug(...) (void)0
#endif
const int maxn=1.4e6+5;
bool FLAG=0;
char s[maxn];
int n,h[maxn],id[maxn],sa[maxn],rk[maxn],buc[maxn],m,p,rk0[maxn*2];
int st[__lg(maxn)+5][maxn];
void assert_tle(bool op){if(!op) for(;;);}
void getsa(){
m=130,p=0;
memset(buc,0,sizeof(int)*(m+3));
F(i,1,n) buc[ rk[i]=s[i] ]++;
F(i,1,m) buc[i]+=buc[i-1];
for(int i=n;i>=1;i--) sa[ buc[rk[i]]-- ]=i;
for(int w=1;p!=n;w*=2,m=p){
p=0;
F(i,1,n) if(i>=n-w+1) assert_tle(p+1<=n),id[++p]=i;
F(i,1,n) if(sa[i]>w) assert_tle(p+1<=n),id[++p]=sa[i]-w;
memset(buc,0,sizeof(buc[0])*(m+3));
F(i,1,n) buc[ h[i]=rk[id[i]] ]++;
F(i,1,m) buc[i]+=buc[i-1];
for(int i=n;i>=1;i--) sa[ buc[h[i]]-- ]=id[i];
p=0,memcpy(rk0,rk,sizeof(int)*(n+5));
F(i,1,n) rk[sa[i]]=[w,x=sa[i-1],y=sa[i]](){return assert_tle(x+w<=2*n && y+w<=2*n),rk0[x]==rk0[y] && rk0[x+w]==rk0[y+w];}()? p: ++p;
}
F(i,1,n) assert_tle(rk[sa[i]]==i && sa[rk[i]]==i);
F(_,1,n){
int i=rk[_];
st[0][i]=max(st[0][rk[_-1]]-1,0);
while(s[sa[i]+st[0][i]]==s[sa[rk[sa[i]]-1]+st[0][i]]) assert_tle(st[0][i]<n),st[0][i]++;
}
F(i,1,__lg(n)) F(j,1,n-(1<<i)+1) st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
//F(i,1,n) debug(i,s+sa[i],st[0][i]);
}
int get(int x,int y){
assert_tle(1<=x&&y<=n);
if(x>y) return n-sa[y]+1;
int d=__lg(y-x+1);
return min(st[d][x],st[d][y-(1<<d)+1]);
}
int getfst(pair<int,int> rng){
assert_tle(rng.x<=rng.y);
int len=rng.y-rng.x+1;
{
int l=1,r=rk[rng.x]-1,z=rk[rng.x];
while(l<=r){
int mid=(l+r)>>1;
if(get(mid+1,rk[rng.x])>=len) r=mid-1,z=mid;
else l=mid+1;
}
return z;
}
}
pair<int,int> getrng(pair<int,int> rng){
assert_tle(rng.x<=rng.y);
int pos=sa[getfst(rng)];
int len=rng.y-rng.x+1;
rng={pos,pos+len-1};
{
int l=rk[rng.x],r=n,z=rk[rng.x]-1;
while(l<=r){
int mid=(l+r)>>1;
if(get(rk[rng.x]+1,mid)>=len && s[sa[mid]+len]=='$') l=mid+1,z=mid;
else r=mid-1;
}
return {rk[rng.x],z};
}
}
vector<pair<int,int>> pp;
int qzh[maxn];
int f(int l,int r){return qzh[getfst({l,r})-1];}
int c(int l,int r){
auto rng=getrng({l,r});
return qzh[rng.y]-qzh[rng.x-1];
}
ll ans;
struct Fentree{
ll t[maxn];
void add(int x,int k){
assert_tle(x<maxn);
for(;x<maxn;x+=x&-x) t[x]+=k;
}
ll query(int x){
assert_tle(x<maxn);
if(!x) return 0;
ll ans=0;
for(;x;x-=x&-x) ans+=t[x];
return ans;
}
}t;
struct DATA{
int tim,pos,val;
bool op;
friend bool operator<(const DATA& a,const DATA& b){
if(a.tim!=b.tim) return a.tim<b.tim;
return a.op<b.op;
}
};
void solve(int l,int r){
int cx=c(l,r),fx=f(l,r);
debug("solve",l,r);
debug(1ll*(cx-1)*(cx-2) + 3ll*(cx-1)*fx);
ans+=1ll*(cx-1)*(cx-2) + 3ll*(cx-1)*fx;
vector<DATA> v;
F(i,l,r-1){
int cy=c(l,i),o1=getfst({l,i}),o2=getfst({i+1,r});
ans+=6ll*cy*max(0,fx-f(i+1,r)-c(i+1,r));
if(o2<o1) ans-=3ll*cy*(cy+1);
if(o1>o2) ans+=3ll*cy*cy;
v.pb({o1,o2,cy,0});
v.pb({o2,o1,cy,1});
}
sort(view(v));
for(auto i:v){
if(i.op==0) ans-=3ll*(t.query(n)-t.query(i.pos))*i.val;
else t.add(i.pos,i.val);
}
for(auto i:v) if(i.op==1) t.add(i.pos,-i.val);
}
bool ENABLE;
void doit(){
ans=0;
memset(s,0,sizeof(char)*(n+3));
memset(h,0,sizeof(int)*(n+3));
memset(qzh,0,sizeof(int)*(n+3));
memset(id,0,sizeof(int)*(n+3));
memset(sa,0,sizeof(int)*(n+3));
memset(rk,0,sizeof(int)*(n+3));
memset(buc,0,sizeof(int)*(n+3));
memset(rk0,0,sizeof(int)*(n+3)*2);
for(auto& i:st) memset(i,0,sizeof(int)*(n+3));
n=0;
int p;
cin>>p;
pp.clear();
F(i,1,p){
static char ss[maxn];
cin>>(ss+1);
int len=strlen(ss+1);
s[++n]='$';
F(j,1,len) assert_tle(n+1<maxn),s[++n]=ss[j];
pp.pb({n-len+1,n});
}
s[++n]='$';
s[++n]='|';
//if(ENABLE) exit(0);
getsa();
//if(ENABLE) exit(0);
for(auto i:pp) qzh[rk[i.x]]++;
F(i,1,n) qzh[i]+=qzh[i-1];
for(auto i:pp) solve(i.x,i.y);
//if(!FLAG) cout<<ans/6<<endl;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int t;
cin>>t;
//if(t==1003) FLAG=1;
int tot=0;
while(t--){
doit();
//tot++;
//if(FLAG){
// if(tot==1001) ENABLE=1;
//}
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 48740kb
input:
3 6 cbaa cb cb cbaa ba ba 3 sdcpc sd cpc 1 ccpc
output:
result:
wrong answer 1st lines differ - expected: '16', found: ''