[Exam reflection] 0122 provincial election simulation 12: Delay

Blog cushions over a year I will forget my exam status in 2333.

T1 is a problem but mentally thought. . . Write a violent Paolu (lack of time, mainly invested in T2 / 3 a)

In fact, however, I thought a fool around, I feel quite able to score probability, result data odd water completely wrong stuff and 20 points, but I did not write. . .

However, T2 writing is a positive solution, although no one else like good pressure, T a detail of a WA took 80 points, makeshift bar.

Then little time T3 when writing, take a violent, think of the positive solution about how to write but did not write, too disgusting.

After the test changed inscribed write afternoon passed. . . A night would pass. . . Made with me for a long time as decadent. . .

However xx is really difficult to write ah write more autistic.

State exam is also OK. After the collapse of the test to change the title of mind (?)

 

T1:Colorado Potato Beetle

Effect: a person walking, given its path seeking encased area. Step $ 10 $ ^ 6, step number $ n \ le 1000 $

Discrete + BFS. The examination room to think too complicated. Do not give away points. . .

According to intuition to write, write on the same subject.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,d[1111],x[11111111],y[11111111],xc,yc,nt[6666][6666],qx[66666666],qy[66666666];
 4 char s[1111][4];long long ans;
 5 map<int,int>X,Y;
 6 int main(){
 7     cin>>n;int nx=0,ny=0,tx,ty,fx,fy;
 8     X[0];X[1];X[-1];Y[1];Y[0];Y[-1];
 9     for(int i=1;i<=n;++i){
10         scanf("%s%d",s[i],&d[i]);
11         if(s[i][0]=='U')nx-=d[i];
12         if(s[i][0]=='D')nx+=d[i];
13         if(s[i][0]=='L')ny-=d[i];
14         if(s[i][0]=='R')ny+=d[i];
15         X[nx];X[nx-1];X[nx+1];
16         Y[ny];Y[ny-1];Y[ny+1];
17     }
18     for(auto I:X)x[++xc]=I.first;
19     for(auto I:Y)y[++yc]=I.first;
20     for(int i=1;i<=xc;++i)X[x[i]]=i;
21     for(int i=1;i<=yc;++i)Y[y[i]]=i;
22     x[++xc]=y[++yc]=1000000007;
23     nx=0,ny=0;
24     for(int i=1;i<=n;++i){
25         fx=X[nx];fy=Y[ny];
26         if(s[i][0]=='U')nx-=d[i];
27         if(s[i][0]=='D')nx+=d[i];
28         if(s[i][0]=='L')ny-=d[i];
29         if(s[i][0]=='R')ny+=d[i];
30         tx=X[nx];ty=Y[ny];
31         if(fx>tx||fy>ty)swap(fx,tx),swap(fy,ty);
32         for(int i=fx;i<=tx;++i)for(int j=fy;j<=ty;++j)nt[i][j]=1;
33     }
34     qx[1]=qy[1]=1;ans=(0ll+x[xc]-x[1])*(y[yc]-y[1]);
35     for(int i=1;i<=xc;++i)nt[i][0]=nt[i][yc]=1;
36     for(int i=1;i<=yc;++i)nt[0][i]=nt[xc][i]=1;
37     for(int h=1,t=1;h<=t;++h){
38         int xx=qx[h],yy=qy[h];
39         if(nt[xx][yy])continue;
40         nt[xx][yy]=1;ans-=(0ll+x[xx+1]-x[xx])*(y[yy+1]-y[yy]);
41         qx[++t]=xx+1;qy[t]=yy;
42         qx[++t]=xx-1;qy[t]=yy;
43         qx[++t]=xx;qy[t]=yy+1;
44         qx[++t]=xx;qy[t]=yy-1;
45     }cout<<ans<<endl;
46 }
View Code

 

T2:Distinct Paths

Effect: $ n \ $ k $ colors fill the times m $ matrix, some lattice already filled, a length of any of claim $ n + m-1 $ a $ (1,1) to $ $ (n, m) All colors are not seeking the same number of programs on the sequence $. $ N, m \ le 1000, k \ le 10 $

Bluffing range data, but obviously if the $ n + m-1> k $ answer must be 0. So not so much the idea.

Search, search in mind, the state press, 11 hex, each bit represents the color appeared in the rightmost position is the first of several columns, you can determine the potential energy can not fill a certain color depending on the state.

Kaka added a pruning often, search and happy on the line.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 unordered_map<long long,int>M[12][12];
 4 #define mod 1000000007
 5 int mo(int x){return x>=mod?x-mod:x;}
 6 long long chg(long long st,int p,int v){p--;return st^(st&15ll<<(p<<2))^(1ll*v<<(p<<2));}
 7 int n,m,k,lp[12][12][12],ac[12][12],ok[12][12][12];
 8 int sch(int x,int y,long long st){
 9     if(x==n&&y==m+1)return 1;
10     if(y==m+1)x++,y=1;
11     if(M[x][y][st])return M[x][y][st];
12     long long rs=st;
13     for(int i=1;i<=k;++i)lp[x][y][i]=rs&15,rs>>=4;
14     if(ac[x][y])return M[x][y][st]=lp[x][y][ac[x][y]]<=y?0:sch(x,y+1,chg(st,ac[x][y],y));
15     int ans=0,cnt15=0,tp=1;
16     for(int i=1;i<=k;++i)if(!ok[x][y][i])if(lp[x][y][i]>y&&lp[x][y][i]!=15)ans=mo(ans+sch(x,y+1,chg(st,i,y)));else if(lp[x][y][i]==15)cnt15++,tp=i;
17     if(cnt15)ans=(ans+cnt15*1ll*sch(x,y+1,chg(st,tp,y)))%mod;
18     return M[x][y][st]=ans;
19 }
20 int main(){
21     cin>>n>>m>>k;
22     if(n+m-1>k)return puts("0"),0;
23     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
24         cin>>ac[i][j];
25         for(int x=1;x<=i;++x)for(int y=1;y<=j;++y)ok[x][y][ac[i][j]]=1;
26         ok[i][j][ac[i][j]]=0;
27     }
28     long long prest=(1ll<<(k<<2))-1;
29     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(ac[i][j])prest=chg(prest,ac[i][j],14);
30     cout<<sch(1,1,prest)<<endl;
31 }
View Code

 

T3: The Memory of Trees

Effect: Given trie, Always ask a string appears on a path many times. $ N, m \ le 100000, \ sum | S | \ le 300000 $

The big question is disgusting god. But the idea is not difficult to think really bad code.

First, find a direct path to heaven and ask complexity sure, but in fact not difficult to find with a bend of the inquiry can be split into several parts is better to do

First up is a chain, and is long on both sides of the inflection point as the center of each $ | S | -1 $ chain, and then another one down the chain.

Part of the original problem and appears to cross the inflection point is the same, but because the total query string grow, so do the words of the complexity of the violence is also on the (hash or kmp).

If you could find a certain period to two points on lca path within the legal complexity of the can. Then multiply violent jump father, complexity is certainly no problem.

Now we only have two chains of things. Because the inquiry is directional, so the two chains up and down seemingly not the same, but in fact, as long as the process up piece of chain when the query string flip it on and go on the same piece of chain. Now the question is to consider how to get a chain.

In fact, this is a very classic string matching, but it is a tree of multiple chains and multiple string pattern matching, how to do?

Intuitively, there are two ideas:

1) tree string, generalized SAM directly ah:

  This is what I think of the examination room, but simply abandoned.

  After use endpos set of meanings, in fact, the problem is transformed into a string match demand in the broad sense SAM, how many endpos on the specified chain.

  The idea seems simple and crude directly is better understood, but extremely thin Si fear.

  According to routine before those questions, the tree maintenance endpos, not that tree line merged yet. . ?

  Then ask this thing is still on the chain, so be quick sum, write a chant tree split chain. . .

  So much hard vegetables together, easily 4k started to write also transferred to sudden death. . .

  Then I did not write this because I will never finish decisively on the estimated examination room. Test and then looking for a little more simple way.

2) no fancy string multiple string matching automata preferred course AC ah:

  It may be useless for too long to so little rusty. Looked back Kanban child (although in fact their own dictation right)

  It has been given for the construction of AC trie automaton and no use. Because it is not a pattern string.

  Pattern string is to ask where those ah. . . However, there's no mandatory online? That is not to say? Offline AC automatic machine down to build a chant. . .

  (However, do not forget to deal with because the chain up, so also playing into the AC automatic machine inside ..)

  Matching string tree automata run on AC, no problem, ah, directly on the line.

  But it is on the chain, zezheng? This chain has been converted into a point of the path to the root part.

  To find a good root ah, directly from the root of the prefix match and then accumulated dfs like ah.

  It is actually part of two paths to the root path to the root of the poor do chanting. . . The classic idea of ​​the difference.

  But look at what we want to ask that? A string of occurrences. I.e. the number of nodes present within the subtree of the current node AC automaton corresponding node and the ancestor chain in the original trie node corresponding to each AC automaton.

  Plus a single point. And asked subtree.

  The first idea is to LCT. Drop dead on the spot.

  Where there is so much trouble, a direct shot at the dfn order, add a single point to single-point increase, sub-tree is the sum check range.

  Fenwick tree maintenance. This is what is difficult?

  What's the difference vector memory bit and then get on trie dfs ran just fine. Plus the contribution of AC automaton corresponding point of time into this point, and then deleted when backtracking.

  However, this difference is to be noted, for example, u-lca, and not simply reduced in u lca added. Because lca Department and by the fact that part of the lower and closer to the nodes also had a contribution.

  For example: 1a2a3a4a5b6a7a8a9b10a11a12a13a14a15. 7-15 queries on aa appear.

  lca 7 and 15 is 7 ah. However, if you cut at 7, then you will 6a7a8 this to count. (8 nodes in the contribution of this stuff is not deducted at the produce 7)

  Here wrong direct blast zero. . . Shoumo a lot of small sample are good, this thing is a card down for a long time. . .

  (However, the sample took an hour to find it correct when ten minutes ... glad I found this bad when I can sample card)

  So it should be gone after that node len-1 minus the contribution to u step in lca.

  When writing violent way like this added to the list, because hashed violence that part of lca, of course, also use this node.

  Then I want to be lazy chose this method and found an array of hash tree + + doubling lca / dfs rampage + number of the code looks like it will not be good to go after careful consideration. . .

  Then write an afternoon storm + half the night to write this rare 80+ lines of code.

  Because the finish is not considered decadent also checked several water meters. . . autistic. . .

  Finally in the middle of the night AC this question. Write a blog when the solution to a problem solving report. A relatively successful end of the day.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define S 666666
 4 int n,m,c[26][S],f[S],fir[S],l[S],to[S],v[S],ec,ch[S],pc,rt;
 5 int R[S],q[S],ans[S],F[20][S],dep[S],fv[S];char s[S];
 6 vector<int>ap[S],Rt[S];
 7 void link(int a,int b,int va){l[++ec]=fir[a];fir[a]=ec;to[ec]=b;v[ec]=va;}
 8 void DFS(int p){
 9     dep[p]=dep[F[0][p]]+1;for(int i=1;i<19;++i)F[i][p]=F[i-1][F[i-1][p]];
10     for(int i=fir[p];i;i=l[i])if(to[i]!=F[0][p])F[0][to[i]]=p,DFS(to[i]),fv[to[i]]=v[i];
11 }
12 int lca(int a,int b){
13     if(dep[a]<dep[b])swap(a,b);
14     int sub=dep[a]-dep[b];
15     for(int i=18;~i;--i)if(sub&1<<i)a=F[i][a];
16     if(a==b)return a;
17     for(int i=18;~i;--i)if(F[i][a]!=F[i][b])a=F[i][a],b=F[i][b];
18     return F[0][a];
19 }
20 void insert(int al,int&p,int o){
21     if(!p)p=++pc;
22     if(!s[al]){R[o]=p;return;}
23     insert(al+1,c[s[al]-'a'][p],o);
24 }
25 void buildAC(){
26     q[1]=1;for(int i=0;i<26;++i)c[i][0]=1;f[0]=1;
27     for(int h=1,t=1;h<=t;++h)for(int i=0;i<26;++i)
28         if(c[i][q[h]])f[q[++t]=c[i][q[h]]]=c[i][f[q[h]]];
29         else c[i][q[h]]=c[i][f[q[h]]];
30 }
31 int Fir[S],Ec,L[S],To[S],dfn[S],dfr[S],tim;
32 void Link(int a,int b){L[++Ec]=Fir[a];Fir[a]=Ec;To[Ec]=b;}
33 void dfs(int p){
34     dfn[p]=++tim;
35     for(int i=Fir[p];i;i=L[i])dfs(To[i]);
36     dfr[p]=tim;
37 }
38 int t[S];
39 void add(int p,int w){for(;p<S;p+=p&-p)t[p]+=w;}
40 int Ask(int p,int a=0){for(;p;p^=p&-p)a+=t[p];return a;}
41 int ask(int p){return Ask(dfr[p])-Ask(dfn[p]-1);}
42 void dfsans(int p,int acp){
43     add(dfn[acp],1);
44     //for(int i=1;i<=tim;++i)cout<<Ask(i)-Ask(i-1)<<' ';cout<<endl;
45     for(int i=0;i<ap[p].size();++i)ans[ap[p][i]>>1]+=Rt[p][i]*ask(R[ap[p][i]]);
46     for(int i=fir[p];i;i=l[i])if(to[i]!=F[0][p])dfsans(to[i],c[v[i]][acp]);
47     add(dfn[acp],-1);
48 }
49 void ins(int p,int o,int r){ap[p].push_back(o);Rt[p].push_back(r);}
50 unsigned long long hsh[S],pw[S],rh;
51 int spj(int a,int b,int c,int o,int x=0){
52     int len=strlen(s),tp=rh=0,sub=max(dep[a]-dep[c]-len+1,0);
53     for(int i=0;i<len;++i)rh=rh*31+s[i]-'a';
54     for(int i=18;~i;--i)if(sub&1<<i)a=F[i][a];
55     ins(a,o<<1,-1);
56     while(a!=c)++tp,hsh[tp]=fv[a],a=F[0][a];
57     sub=max(dep[b]-dep[c]-len+1,0);
58     for(int i=18;~i;--i)if(sub&1<<i)b=F[i][b];
59     ins(b,o<<1|1,-1);
60     a=b;tp+=dep[b]-dep[c];
61     while(b!=c)hsh[tp]=fv[b],tp--,b=F[0][b];
62     tp+=dep[a]-dep[c];
63     for(int i=1;i<=tp;++i)hsh[i]+=hsh[i-1]*31,pw[i]=pw[i-1]*31;
64     for(int i=len;i<=tp;++i)x+=rh==hsh[i]-hsh[i-len]*pw[len];
65     return x;
66 }
67 int main(){//freopen("1.in","r",stdin);
68     cin>>n>>m;pw[0]=1;
69     for(int i=1,a,b;i<n;++i)scanf("%d%d%s",&a,&b,s),link(a,b,s[0]-'a'),link(b,a,s[0]-'a');
70     DFS(1);
71     for(int i=1,x,y,c;i<=m;++i){
72         scanf("%d%d%s",&x,&y,s);c=lca(x,y);
73         ins(x,i<<1,1);ins(y,i<<1|1,1);ans[i]=spj(x,y,c,i);
74         insert(0,rt,i<<1|1);
75         reverse(s,s+strlen(s));
76         insert(0,rt,i<<1);
77     }buildAC();
78     for(int i=1;i<=pc;++i)Link(f[i],i);
79     dfs(0);    dfsans(1,rt);
80     for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
81 }
View Code

 

Encourage each other: 10 days training at home Do not decadent.

2020/01/29 23:50

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/12241876.html