提交记录 4440


用户 题目 状态 得分 用时 内存 语言 代码长度
psk011102 noi18c. 【NOI2018】你的名字 Accepted 100 1.476 s 315184 KB C++11 4.00 KB
提交时间 评测时间
2018-07-23 10:55:29 2020-07-31 23:02:28
// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define F(i, l, r) for(int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for(int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std;

bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}

inline int read() {
 int x = 0, fh = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar() ) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar() ) x = (x<<1) + (x<<3) + (ch ^ '0');
    return x * fh;
}
int n,m,q;
int cnt;
int ls[10000005],rs[10000005],sz[10000005];
char s[2000005],t[2000005];
long long ans;
#define mid ((l+r)>>1)
void update(int &rt,int lst,int l,int r,int pos){
      rt=++cnt;
      sz[rt]=sz[lst]+1;
      if(l==r)return ;
     if(pos<=mid){
        rs[rt]=rs[lst];
        update(ls[rt],ls[lst],l,mid,pos);
     }
     else{
        ls[rt]=ls[lst];
        update(rs[rt],rs[lst],mid+1,r,pos);
     }
}
int query(int rt,int l,int r,int L,int R){
    if(!rt)return 0;
    if(L<=l&&r<=R)return sz[rt];
    int res=0;
    if(L<=mid)res+=query(ls[rt],l,mid,L,R);
    if(R>mid) res+=query(rs[rt],mid+1,r,L,R);
    return res;
}
namespace SAM{
  int lst=1,tot=1;
  int son[1000005][26],fa[1000005],len[1000005],ne[1000005],he[1000005],dfsclk,st[1000005],ed[1000005],pos[10000005],rt[1000005];
  bool pre[1000005]; 
  void extend(int c){
        int np=++tot,p=lst;
        len[np]=len[p]+1;
        pre[np]=1;
        while(p&&!son[p][c])son[p][c]=np,p=fa[p];
        if(!p)fa[np]=1;
        else{
          int q=son[p][c];
          if(len[q]==len[p]+1){
            fa[np]=q;
          }
          else{
             int nq=++tot;
             len[nq]=len[p]+1;
             fa[nq]=fa[q];
             memcpy(son[nq],son[q],sizeof(son[nq]));
             fa[q]=fa[np]=nq;
             while(p&&son[p][c]==q)son[p][c]=nq,p=fa[p];
          }
        }
        lst=np;
   }
  void dfs(int x){
      st[x]=++dfsclk;
      pos[dfsclk]=x;
      for(int i=he[x];i;i=ne[i]){
            dfs(i);
      }
      ed[x]=dfsclk;
  }
  void built(){
       F(i,1,n)extend(s[i]-'a');
       F(i,2,tot){
         ne[i]=he[fa[i]];
         he[fa[i]]=i;
       }
       dfs(1);
       F(i,1,dfsclk){
         if(pre[pos[i]])update(rt[i],rt[i-1],1,n,len[pos[i]]);
         else rt[i]=rt[i-1];
       }
  }
  bool valid(int x,int l,int r){
     if(!x)return 0;
     if(query(rt[ed[x]],1,n,l,r)<=query(rt[st[x]-1],1,n,l,r))return 0;
     return 1;
  }
}
namespace sam{
   int lst=1,tot=1;
   int son[2000005][26],fa[2000005],len[2000005],pos[2000005],lim[2000005];
  void extend(int c,int now){
    int np=++tot,p=lst;
   len[np]=len[p]+1;
   pos[np]=now;
   while(p&&!son[p][c])son[p][c]=np,p=fa[p];
   if(!p)fa[np]=1;
   else{
     int q=son[p][c];
     if(len[q]==len[p]+1){
         fa[np]=q;
     }
     else{
       int nq=++tot;
      len[nq]=len[p]+1;
      fa[nq]=fa[q];
      pos[nq]=pos[q];
      memcpy(son[nq],son[q],sizeof(son[nq]));
      fa[q]=fa[np]=nq;
      while(p&&son[p][c]==q)son[p][c]=nq,p=fa[p];
     }
   }
   lst=np;
  }
  void clear(){
   //Set(son,0);
   F(i,1,tot)F(j,0,25)son[i][j]=0;
     tot=lst=1;
  }
  void built(int l,int r){
      int now=0,p=1;
     F(i,1,m){
         extend(t[i]-'a',i);
       while(1){
          if(SAM::valid(SAM::son[p][t[i]-'a'],l+now,r)){
              now++;
              p=SAM::son[p][t[i]-'a'];
              break;
          }
          if(now==0)break;
          now--;
          if(now==SAM::len[SAM::fa[p]])p=SAM::fa[p];
       } 
       lim[i]=now;
     }
     F(i,2,tot){
        ans+=max(0,len[i]-max(len[fa[i]],lim[pos[i]]));
     }
  }
}
int main () {
    file("name");
     scanf("%s",s+1);
     n=strlen(s+1);
     SAM::built();
     scanf("%d",&q);
     while(q--){
        int l,r;
        scanf("%s%d%d",t+1,&l,&r);
        m=strlen(t+1);
        ans=0;
          sam::built(l,r);
        printf("%lld\n",ans);
        sam::clear();
     }
     return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #14.056 ms216 KBAcceptedScore: 4

Testcase #25.687 ms488 KBAcceptedScore: 4

Testcase #35.742 ms492 KBAcceptedScore: 4

Testcase #474.527 ms932 KBAcceptedScore: 4

Testcase #571.522 ms920 KBAcceptedScore: 4

Testcase #6699.455 ms307 MB + 816 KBAcceptedScore: 4

Testcase #7699.954 ms307 MB + 732 KBAcceptedScore: 4

Testcase #889.574 ms44 MB + 156 KBAcceptedScore: 4

Testcase #9153.273 ms40 MB + 160 KBAcceptedScore: 4

Testcase #10211.676 ms87 MB + 484 KBAcceptedScore: 4

Testcase #11387.09 ms82 MB + 160 KBAcceptedScore: 4

Testcase #12368.196 ms131 MB + 384 KBAcceptedScore: 4

Testcase #13648.358 ms125 MB + 820 KBAcceptedScore: 4

Testcase #14516.223 ms176 MB + 816 KBAcceptedScore: 4

Testcase #15937.076 ms170 MB + 704 KBAcceptedScore: 4

Testcase #16677.816 ms222 MB + 256 KBAcceptedScore: 4

Testcase #171.253 s215 MB + 824 KBAcceptedScore: 4

Testcase #181.054 s95 MB + 764 KBAcceptedScore: 4

Testcase #191.188 s137 MB + 28 KBAcceptedScore: 4

Testcase #201.342 s179 MB + 368 KBAcceptedScore: 4

Testcase #211.384 s222 MB + 384 KBAcceptedScore: 4

Testcase #221.476 s222 MB + 212 KBAcceptedScore: 4

Testcase #231.476 s222 MB + 148 KBAcceptedScore: 4

Testcase #241.44 s222 MB + 344 KBAcceptedScore: 4

Testcase #251.389 s222 MB + 396 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:30:29 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠