先把所有词扔进tire,然后dfs序弄出区间,然后就是区间最大值了

主席树各种写挂QAQ

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(int i=l;i<=r;i++)
 3 #define dec(i,l,r) for(int i=l;i>=r;i--)
 4 #define link(x) for(edge *j=h[x];j;j=j->next)
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define inf 1e9
 7 #define ll long long
 8 #define succ(x) (1<<x)
 9 #define lowbit(x) (x&(-x))
10 #define ida(x) (x-'a')
11 #define NM 100000+5
12 using namespace std;
13 int read(){
14     int x=0,f=1;char ch=getchar();
15     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
16     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
17     return x*f;
18 }
19 const int cnt=26;
20 struct seg{
21     int s;
22     seg *l,*r;
23 }T[100*NM],*_root[NM],*_o=T;
24 struct node{
25     vector<int >id;
26     node *c[cnt+1];
27 }N[4*NM],*o=N,*root;
28 int n,m,a[NM],in[NM],out[NM],tot,_t;
29 char st[NM];
30 void ins(int x){
31     node *r=root;
32     scanf("%s",st);m=strlen(st)-1;
33     dec(i,m,0){
34         if(!r->c[ida(st[i])])r->c[ida(st[i])]=++o;
35         r=r->c[ida(st[i])];
36     }
37     r->id.push_back(x);
38 }
39 void dfs(node *r){
40     if(!r->id.empty()){
41         int t=tot+1;
42         inc(i,0,r->id.size()-1)in[r->id[i]]=t,a[++tot]=r->id[i];
43     }
44     inc(i,0,cnt)if(r->c[i])
45     dfs(r->c[i]);
46     if(!r->id.empty())inc(i,0,r->id.size()-1)out[r->id[i]]=tot;
47 }
48 seg* mod(seg *p,int x,int y){
49     seg *r=++_o;int t=x+y>>1;
50     r->s=p->s+1;
51     r->l=p->l;r->r=p->r;
52     if(x==y)return r;
53     if(_t<=t)r->l=mod(p->l,x,t);
54     else r->r=mod(p->r,t+1,y);
55     return r;
56 }
57 int query(seg *_r,seg *r,int x,int y){
58     int t=x+y>>1,v=r->l->s-_r->l->s;
59     if(x==y)return _t==1?x:-1;
60     if(_t<=v)return query(_r->l,r->l,x,t);
61     else {_t-=v;return query(_r->r,r->r,t+1,y);}
62 }
63 int main(){
64 //    freopen("data.in","r",stdin);
65 //    freopen("test.out","w",stdout);
66     root=++o;
67     n=read();
68     inc(i,1,n)ins(i);
69     dfs(root);
70     _root[0]=++_o;_o->l=_o->r=_o;
71 /*    inc(i,1,n)printf("%d ",in[i]);putchar('\n');
72     inc(i,1,n)printf("%d ",out[i]);putchar('\n');
73     inc(i,1,n)printf("%d ",a[i]);putchar('\n');*/
74     inc(i,1,n){
75         _t=a[i];
76         _root[i]=mod(_root[i-1],1,n);
77     }
78 //    inc(i,1,n)printf("%d ",b[i]);putchar('\n');
79     inc(i,1,n){
80         _t=read();
81         if(_root[out[i]]->s-_root[in[i]-1]->s>=_t)
82         printf("%d\n",query(_root[in[i]-1],_root[out[i]],1,n));
83         else printf("-1\n");
84     }
85     return 0;
86 }
View Code

 

posted on 2016-02-25 18:36  onlyRP  阅读(134)  评论(0编辑  收藏  举报