标签:har 扫描线 str 解决 oid 答案 描述 iba for
#include<bits/stdc++.h> using namespace std; const int N=1e6+5; int n,q,size,ans[N],ge,cnt1,cnt2,sh[N],dq[N],dui[N],cj,cc[N]; struct ztef { int x1,x2,y1,y2,k,id; }a[N<<1],huan2[N]; struct cha { int qi,ho; }b[N],huan[N]; struct line { int he,ze1,ze2,pu,du; }li[N<<1]; string s,ss; struct pigu { int er[N][27],dfn[N],cnt,siz[N]; inline int insert(string x) { int len=x.size(),now=0; for(int i=0;i<len;i++) { if(!er[now][x[i]-‘a‘]) er[now][x[i]-‘a‘]=++cnt; now=er[now][x[i]-‘a‘]; } return now; } inline void dfs(int now) { dfn[now]=++size;siz[now]=1; for(int i=0;i<=25;i++) { if(!er[now][i]) continue; dfs(er[now][i]); siz[now]+=siz[er[now][i]]; } } inline int cha(string x) { int now=0,len=x.size(); for(int i=0;i<len;i++) { if(!er[now][x[i]-‘a‘]) return 0; now=er[now][x[i]-‘a‘]; } return now; } }zh,fa; inline int read() { char c=getchar(); int x=0,f=1; while(!isdigit(c)) {if(c==‘-‘) f=-1;c=getchar();} while(isdigit(c)) {x=(x<<3)+(x<<1)+c-‘0‘;c=getchar();} return x*f; } inline int lowbit(int x) { return x&(-x); } inline void update(int now,int v) { while(now<=cj) { sh[now]+=v; now+=lowbit(now); } } inline int query(int now) { int daan=0; if(now<=0) return 0; while(now) { daan+=sh[now]; now-=lowbit(now); } return daan; } inline bool cmp2(line x,line y) { return x.he<y.he; } inline bool cmp1(cha x,cha y) { return x.qi<y.qi; } inline void solve(int l,int r,int L,int R) { if(L>R) return; if(l==r) { if(r==n+1) for(int i=L;i<=R;i++) ans[dui[i]]=-1; else for(int i=L;i<=R;i++) ans[dui[i]]=l; return; } int cn3=1,mid=(l+r)>>1,cn1=0,cn2=0; for(int i=l;i<=mid;i++) huan[++cn1]=b[i]; sort(huan+1,huan+cn1+1,cmp1); for(int i=L;i<=R;i++) { if(ans[dui[i]]==-1) continue; li[++cn2]=(line){a[dui[i]].x1-1,a[dui[i]].y1,a[dui[i]].y2,-1,i}; li[++cn2]=(line){a[dui[i]].x2,a[dui[i]].y1,a[dui[i]].y2,1,i}; } sort(li+1,li+cn2+1,cmp2); for(int i=1;i<=cn2;i++) { while(cn3<=cn1&&huan[cn3].qi<=li[i].he) { update(huan[cn3].ho,1); cn3++; } dq[li[i].du]+=(query(li[i].ze2)-query(li[i].ze1-1))*li[i].pu; } for(int i=1;i<cn3;i++) update(huan[i].ho,-1); int hu1=L,hu2=R; for(int i=L;i<=R;i++) { if(dq[i]>=a[dui[i]].k) cc[hu1++]=dui[i]; else { cc[hu2--]=dui[i]; a[dui[i]].k-=dq[i]; } dq[i]=0; } for(int i=L;i<=R;i++) dui[i]=cc[i]; solve(l,mid,L,hu1-1); solve(mid+1,r,hu2+1,R); } int main() { n=read();q=read(); for(int i=1;i<=n;i++) { cin>>s; b[i].qi=zh.insert(s); reverse(s.begin(),s.end()); b[i].ho=fa.insert(s); } zh.dfs(0);size=0;fa.dfs(0); for(int i=1;i<=n;i++) { b[i].qi=zh.dfn[b[i].qi]; b[i].ho=fa.dfn[b[i].ho]; } cj=fa.cnt+1; for(int i=1,x,y,z;i<=q;i++) { cin>>s>>ss>>z; dui[i]=i; reverse(ss.begin(),ss.end()); x=zh.cha(s); y=fa.cha(ss);a[i].k=z; if(x==0||y==0) {ans[i]=-1;continue;} a[i].id=i; a[i].x1=zh.dfn[x];a[i].x2=zh.dfn[x]+zh.siz[x]-1; a[i].y1=fa.dfn[y];a[i].y2=fa.dfn[y]+fa.siz[y]-1; } solve(1,n+1,1,q); for(int i=1;i<=q;i++) cout<<ans[i]<<"\n"; return 0; }
标签:har 扫描线 str 解决 oid 答案 描述 iba for
原文地址:https://www.cnblogs.com/betablewaloot/p/12130294.html