QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#630261#9425. Generated String11d10xyWA 4ms12300kbC++145.6kb2024-10-11 17:22:422024-10-11 17:22:43

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 17:49:05]
  • hack成功,自动添加数据
  • (/hack/995)
  • [2024-10-11 17:22:43]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:12300kb
  • [2024-10-11 17:22:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,m;
char str[100010];
namespace SA{
int sa[100010],rk[100010],tmp[100010],cnt[100010],h[100010],mi[21][100010];
void cntsort(int key[],int s[],int t[],int n,int V){
   for(int i=0;i<=V;i++)cnt[i]=0;
   for(int i=1;i<=n;i++)cnt[key[s[i]]]++;
   for(int i=1;i<=V;i++)cnt[i]+=cnt[i-1];
   for(int i=n;i>=1;i--)t[cnt[key[s[i]]]--]=s[i];
}
void init(){
   for(int i=1;i<=n;i++)tmp[i]=i,rk[i]=str[i];
   cntsort(rk,tmp,sa,n,512);
   for(int p=0,m=1;p<n;m<<=1){
      p=0;
      for(int i=n;i>n-m;i--)tmp[++p]=i;
      for(int i=1;i<=n;i++)if(sa[i]>m)tmp[++p]=sa[i]-m;
      cntsort(rk,tmp,sa,n,max(n,512));
      for(int i=1;i<=n;i++)tmp[i]=rk[i];
      p=0;
      for(int i=1;i<=n;i++){
         if(tmp[sa[i]]==tmp[sa[i-1]]&&tmp[sa[i]+m]==tmp[sa[i-1]+m])rk[sa[i]]=p;
         else rk[sa[i]]=++p;
      }
   }
   for(int i=1;i<=n;i++){
      h[i]=max(h[i-1]-1,0);
      for(;str[sa[rk[i]-1]+h[i]]==str[i+h[i]];h[i]++);
   }
   for(int i=1;i<=n;i++)mi[0][i]=h[sa[i]];
   for(int i=1;i<=20;i++)for(int k=1;k+(1<<i)-1<=n;k++)
   mi[i][k]=min(mi[i-1][k],mi[i-1][k+(1<<i-1)]);
}
int lcp(int l,int r){
   if(l==r)return n-l+1;
   l=rk[l],r=rk[r];
   if(l>r)swap(l,r);
   l++;int g=__lg(r-l+1);
   return min(mi[g][l],mi[g][r-(1<<g)+1]);
}
}
namespace ds{
int res1[100010],resl[100010],resr[100010];
struct Q_{int i,sid;};
vector<Q_>a,b;
struct N_{vector<pair<int,int>>seg;};
vector<N_>S;
struct R_{int i,p,j,q;char x,y;};
inline R_ calc(int X,int Y){
   auto&a=S[X].seg,&b=S[Y].seg;
   auto reva=rbegin(a),revb=rbegin(b);
   int i=0,p=0,j=0,q=0;
   while(i<a.size()&&j<b.size()){
      int l1=reva[i].second-reva[i].first+1,l2=revb[j].second-revb[j].first+1;
      int w=min(l1-p,l2-q),v=SA::lcp(reva[i].first+p,revb[i].first+q);
      if(v<w){p+=v,q+=v;break;}
      p+=w,q+=w;
      if(p==l1)i++,p=0;
      if(q==l2)j++,q=0;
   }
   int c1=i==a.size()?0:str[reva[i].first+p],c2=j==b.size()?0:str[revb[j].first+q];
   return{i,p,j,q,c1,c2};
}
inline bool cmp(int X,int Y){
   if(X==Y)return false;
   R_ w=calc(X,Y);
   if(w.i<w.j)swap(X,Y),swap(w.i,w.j),swap(w.p,w.q);
   for(int t=0;t<w.i;t++)S[X].seg.pop_back();
   if(w.p)S[X].seg.back().first+=w.p;
   auto ry=rbegin(S[Y].seg);
   if(w.q){
      auto y=ry[w.j];
      S[X].seg.emplace_back(y.first,y.first+w.q-1);
   }
   for(int t=w.j-1;t>=0;t--)S[X].seg.push_back(ry[t]);
   return w.x<w.y;
}
void calc(){
   SA::init();
   for(auto&X:S)reverse(begin(X.seg),end(X.seg));
   stable_sort(begin(a),end(a),[](Q_ x,Q_ y){return cmp(x.sid,y.sid);});
   for(int i=0;i<a.size();i++)res1[a[i].i]=i+1;
   for(int i=0;i<b.size();i++){
      resl[b[i].i]=lower_bound(begin(a),end(a),b[i],[](Q_ x,Q_ y){return cmp(x.sid,y.sid);})-begin(a)+1;
      resr[b[i].i]=partition_point(begin(a)+resl[b[i].i],end(a),[&](Q_ x){
         return calc(b[i].sid,x.sid).i==S[b[i].sid].seg.size();
      })-begin(a);
   }
   for(auto&X:S)reverse(begin(X.seg),end(X.seg));
}
}
namespace cdq{
struct N_{int i,w,x,yl,yr;};
vector<N_>a;
int cnt[100010],ans[100010];
inline void add(int p,int v){
   for(;p<=n;p+=p&-p)cnt[p]+=v;
}
inline int ask(int p){
   int res=0;for(;p;p-=p&-p)res+=cnt[p];
   return res;
}
void solve(int l,int r){
   if(l==r)return;
   int mid=l+r>>1;
   solve(l,mid),solve(mid+1,r);
   int j=l;
   for(int i=mid+1;i<=r;i++){
      for(;j<=mid&&a[j].x<=a[i].x;j++)if(!a[j].i)add(a[j].yl,a[j].w);
      if(a[i].i)ans[a[i].i]+=a[i].w*(ask(a[i].yr)-ask(a[i].yl-1));
   }
   for(;--j>=l;)if(!a[j].i)add(a[j].yl,-a[j].w);
   inplace_merge(begin(a)+l,begin(a)+mid+1,begin(a)+r+1,[](N_ x,N_ y){
      return x.x<y.x;
   });
}
}
struct T1_{int x,y;}q1[100010];
struct T2_{int xl,xr,yl,yr;}q2[100010];
struct Q_{char op;int i,j;}q[100010];
int main(){
   scanf("%d%d%s",&n,&m,str+1);
   auto reads=[]{
      int k;scanf("%d",&k);
      vector<pair<int,int>>a(k);
      for(auto&X:a)scanf("%d%d",&X.first,&X.second);
      return a;
   };
   for(int i=1;i<=m;i++){
      char op;
      scanf(" %c",&op);
      q[i].op=op;
      if(op=='+'){
         q[i].i=ds::S.size(),ds::S.push_back({reads()});
      }
      if(op=='-')scanf("%d",&q[i].i);
      if(op=='?'){
         q[i].i=ds::S.size(),ds::S.push_back({reads()});
         q[i].j=ds::S.size(),ds::S.push_back({reads()});
      }
   }
   for(int i=1;i<=m;i++){
      if(q[i].op=='+')ds::a.push_back({i,q[i].i});
      if(q[i].op=='?')ds::b.push_back({i,q[i].i});
   }
   ds::calc();
   for(int i=1;i<=m;i++){
      if(q[i].op=='+')q1[i].x=ds::res1[i];
      if(q[i].op=='?')q2[i].xl=ds::resl[i],q2[i].xr=ds::resr[i];
   }
   reverse(str+1,str+n+1);
   for(auto&x:ds::S){
      reverse(begin(x.seg),end(x.seg));
      for(auto&y:x.seg){
         swap(y.first,y.second);
         y.first=n-y.first+1,y.second=n-y.second+1;
      }
   }
   ds::b.clear();
   for(int i=1;i<=m;i++){
      if(q[i].op=='+')ds::a.push_back({i,q[i].i});
      if(q[i].op=='?')ds::b.push_back({i,q[i].j});
   }
   ds::calc();
   for(int i=1;i<=m;i++){
      if(q[i].op=='+')q1[i].y=ds::res1[i];
      if(q[i].op=='?')q2[i].yl=ds::resl[i],q2[i].yr=ds::resr[i];
   }
   for(int i=1;i<=m;i++){
      if(q[i].op=='+'){
         cdq::a.push_back({0,1,q1[i].x,q1[i].y,0});
      }
      if(q[i].op=='-'){
         cdq::a.push_back({0,-1,q1[q[i].i].x,q1[q[i].i].y,0});
      }
      if(q[i].op=='?'){
         cdq::a.push_back({i,-1,q2[i].xl-1,q2[i].yl,q2[i].yr});
         cdq::a.push_back({i,1,q2[i].xr,q2[i].yl,q2[i].yr});
      }
   }
   cdq::solve(0,cdq::a.size()-1);
   for(int i=1;i<=m;i++)if(q[i].op=='?')printf("%d\n",cdq::ans[i]);
   return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 12300kb

input:

8 7
abcaabbc
+ 3 1 3 2 4 3 8
+ 2 1 4 1 8
+ 1 2 4
? 1 5 6 1 7 8
- 3
+ 1 2 5
? 1 2 3 1 5 5

output:

2
1

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 12148kb

input:

5 2000
bbaab
+ 1 3 5
+ 2 5 5 3 5
- 2
? 1 1 3 3 3 3 4 5 2 5
- 1
? 3 1 1 2 4 1 5 1 3 4
? 1 1 2 2 2 4 4 4
? 1 1 5 1 5 5
+ 3 1 2 1 5 1 4
? 2 1 5 1 3 2 1 2 5 5
? 1 3 4 1 4 5
- 9
? 1 1 4 1 2 3
+ 2 1 5 1 2
+ 1 1 4
- 15
- 14
? 2 2 5 2 5 1 3 4
+ 1 2 3
- 19
+ 3 1 4 1 5 4 5
- 21
+ 1 2 5
? 3 1 5 5 5 1 5 1 3 5
?...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 10th lines differ - expected: '1', found: '0'