[Exam reflection] 0302 provincial election simulation 35: Nemesis

 

Rare exam and was able to read the time.

Although T1 writing is not positive solution, but as the shortest memory minimum of code shortest violence somehow stepped on all the positive solution and I said he was ignorant force.

What will not be on the test, but feel like the first question is not very easy card is full.

So readily added some small pruning, card cards often it, then I do not know why it is actually on the $ A $.

But it is a properly properly violence ah. Although positive solutions and ideas have a little bit similar, but still $ O (n ^ 2) $ in.

This time the tender can put the winter of $ RP $ consume almost the same.

$ T2 $ glance to know about a point of divide and conquer, divide and conquer this point but things did not write too long, and would like to review has been no time.

So naturally tempted to refuse the board in the examination room, he wrote a card for a long time and often violent. But this time with no eggs.

$ T3 $, then the conclusion is immortal question, but has not always wanted to prove. He took the most low-grade violence left.

One-parameter problem does not hit the table is simply a sin. I feel that even though it seems to play out can not see out of the way. . . %%% $ yzh $ grindstone

Just stumbled feel it is cost flow run bipartite graph with the right match, but how to build a bipartite graph? do not know.

$ T3 $ result to the final solution to a problem have not been proven. And $ T2 $ and cancer card cards often until midnight.

When writing $ T2 $ found previously written dotted rule have a little problem, also studied together to understand this.

$ FFT $ can live, violence convolution able to live, is $ NTT $ make life difficult. I. . .

 

T1:two

Effect: two trees, up to the specified delete an edge and then delete each round, two end points, respectively, on both sides of an edge deletion of all points on another tree. The output of this process. $ N \ le 200000 $

My son used $ dfs $ traverse order to find out the son, with $ unordered \ set $ deposit son, after traversing the Border Erase deletes reduce the amount, then no.

Who told me the difference between it and the general violence. . ? I would also like to know the reason fast (to facilitate future reuse ah $ O (n ^ 2) $ of 200,000 who do not want this kind of thing ah huh)

 1 #include<bits/stdc++.h> 
 2 using namespace std;
 3 #define S 222222
 4 unordered_set<int>son[2][S];
 5 vector<int>tbd[2];
 6 int n,f[2][S],dfn[2][S],dfr[2][S],idfn[2][S],tim,al[2][S];
 7 void dfs(int o,int p){
 8     dfn[o][p]=++tim;idfn[o][tim]=p;
 9     for(auto x:son[o][p])dfs(o,x); dfr[o][p]=tim;
10 }
11 int main(){
12     scanf("%d",&n);
13     for(int i=2;i<=n;++i)scanf("%d",&f[0][i]),son[0][f[0][i]].insert(i);
14     for(int i=2;i<=n;++i)scanf("%d",&f[1][i]),son[1][f[1][i]].insert(i);
15     dfs(0,1);tim=0;dfs(1,1); al[0][0]=al[1][0]=1;
16     int o=0;int s;scanf("%d",&s);tbd[0].push_back(s+1);al[0][s+1]=1;
17     while(1){
18         if(tbd[o].empty())return 0; puts(o?"Red":"Blue");
19         sort(tbd[o].begin(),tbd[o].end());
20         for(auto x:tbd[o]){
21             printf("%d ",x-1);al[o][x]=1;son[o][f[o][x]].erase(x);
22             for(int i=dfn[o][x],p;p=idfn[o][i],i<=dfr[o][x];++i){
23                 if(!al[o^1][p]&&(dfn[o][f[o^1][p]]<dfn[o][x]||dfn[o][f[o^1][p]]>dfr[o][x]))tbd[o^1].push_back(p),al[o^1][p]=1;
24                 for(auto u:son[o^1][p])if(!al[o^1][u]&&(dfn[o][u]<dfn[o][x]||dfn[o][u]>dfr[o][x]))tbd[o^1].push_back(u),al[o^1][u]=1;
25             }
26         }puts("");tbd[o].clear();o^=1;
27     }
28 }
The code length look like violence (1.1k) and does not look like violent run time (3300ms)

Thinking about positive solutions segment tree, each edge corresponds to another tree, the equivalent of $ $ DFS sequence of points within an interval to be broken even the outer edges of all sections.

So probably two tree line, maintaining a greater than less than one maintenance, bordered total added $ O (n \ log \ n) $ strips.

Then for the node tree line opened a $ vector $, to maintain internal order, in fact, each deletion of a prefix or suffix (and lazy deletion), violence can be.

It is probably the point. Time complexity $ O (n \ log \ n) $. Constant large.

 

T2:bracket

Effect: the tree node parentheses, each directed path request is legitimate bracket sequence, if it can be split into up to $ A $ substrings meet each sub-string of the string are legitimate right parenthesis is $ a $.

Seeking a weight of $ 1, ..., n $ string is the number of brackets. $ N \ le 50000 $.

Brackets into $ + 1 / -1 $ This is common sense. Then all the trees of this path must be included in the answer probably is dotted ruled.

Your goal is to find two after that point, and the path weights and path $ 0 $ merge together.

Statistics legitimate weight is this: If the current path prefix and is the starting point to the maximum / minimum value encountered this point, then it is legitimate and the right value is the number of occurrences of this value.

The combined weight of the two, then it is the classic convolution. But because this question was how small the number of convolution length, so there is no law there is no pre-treatment membrane is not running efficiency violence float is also good.

If the path to be sentenced to special weights and not $ 0 $ then both ends of the combined weights will be $ 1 + $.

And to note that the two paths a left half of a right half, while containing only a starting point or even heavier.

Therefore, the overall time complexity is large constant $ O (nlog ^ 2n) $ a. If the convolution is violence, then that is a small constant $ O (n ^ 2logn) $ a.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define S 266666
 4 #define mod 998244353
 5 #define For(i) for(int i=fir[p];i;i=l[i])if(!al[to[i]]&&to[i]!=fa) 
 6 int fir[S],l[S],sz[S],to[S],v[S],ec,n,tsz,C,Csz,ans[S],MX,MN,fa,R[S];
 7 char s[3];vector<int>pos[S],neg[S];int r1[S],r2[S],rev[S],al[S],m;
 8 void link(int a,int b){l[++ec]=fir[a];fir[a]=ec;to[ec]=b;}
 9 void cen(int p,int fa=0){
10     int mx=0;sz[p]=1;
11     For(i)cen(to[i],p),sz[p]+=sz[to[i]],mx=max(mx,sz[to[i]]);
12     mx=max(mx,tsz-sz[p]); if(mx<Csz)Csz=mx,C=p;
13 }
14 void dfs(int p,int fa,int v1,int v2,int mx,int mn,int mxc,int mnc){
15     v1+=v[p];v2+=v[p];
16     if(v1>mx)mx=v1,mxc=0;if(v1==mx)mxc++,pos[mx].push_back(mxc);
17     if(v2<mn)mn=v2,mnc=0;if(v2==mn)mnc++,neg[-mn].push_back(mnc);
18     MX=max(MX,mx);MN=min(MN,mn);
19     For(i)dfs(to[i],p,v1,v2,mx,mn,mxc,mnc);
20 }
21 void cal(int rt){
22     for(int x=0,cnt;cnt=0,x<=MX&&x<=-MN;++x){
23         for(auto y:pos[x])cnt=max(cnt,y),r1[y]++; pos[x].clear();
24         for(auto y:neg[x])cnt=max(cnt,y),r2[y]++; neg[x].clear();
25         for(int i=0;i<=cnt;++i)for(int j=0;j<=cnt;++j)R[i+j]+=1ll*r1[i]*r2[j];
26         for(int i=0;i<=cnt*2;++i)ans[i+(x?-1:0)]+=R[i]*rt;
27         for(int i=0;i<=cnt*2;++i)r1[i]=r2[i]=R[i]=0;
28     }for(int x=MX+1;x<=-MN;++x)neg[x].clear();
29     for(int x=-MN+1;x<=MX;++x)pos[x].clear();
30 }
31 void DaC(int p){
32     al[p]=1;int Mx=0,CNT=0,Mn=0,I=0;
33     For(i)MX=MN=0,dfs(to[i],0,v[p],0,v[p]>0,0,v[p]>0,0),cal(-1);
34     if(v[p]>0)pos[1].push_back(1);MX=v[p]>0;MN=0;r2[0]++;For(i)dfs(to[i],0,v[p],0,v[p]>0,0,v[p]>0,0);cal(1);
35     For(i)tsz=Csz=sz[to[i]],cen(to[i]),DaC(C);
36 }
37 int main(){
38     scanf("%d",&n);
39     for(int i=1,a,b;i<n;++i)scanf("%d%d",&a,&b),link(a,b),link(b,a);
40     for(int i=1;i<=n;++i)scanf("%s",s),v[i]=s[0]=='('?1:-1;
41     tsz=Csz=n;cen(1);DaC(C);
42     scanf("%d",&m);for(int i=0,x;i<m;++i)scanf("%d",&x),printf("%d\n",ans[x]);
43 }
Looks like the code length violence (1.7k) and looks like a running time of violence (6600ms)

 

 T3: sum

Effect: $ \ {1 ... n \} $ is selected from a subset of GCD $ $ $ 1 to $. And maximize the number of elected. $ N \ le 2 \ times 10 ^ 5 $

First, the title means is that every germplasm factor only in a digital Lane.

Then throw Conclusion: a quality factor either alone appears in a digital, or is a $ \ le \ sqrt {n} $ and $ a> \ sqrt {n} $ occurs in a few years.

Network flow running weighted bipartite graph matching. This problem no more. time complexity? Network Flow $ P $ do you say?

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define S 6222222
 4 #define ll long long
 5 int n,np[S],ans,f[S],p[S],pc,v[S],w[S],fir[S],to[S],d[S],q[S],ec=1,al[S],l[S];
 6 int F(int a,ll b){while(b<=n)b*=a;return b/a;}
 7 bool bfs(){
 8     for(int i=1;i<=pc;++i)d[i]=-n,al[i]=0;al[0]=0;
 9     for(int h=1,t=1;h<=t;++h)for(int i=fir[q[h]];i;i=l[i])if(d[to[i]]<d[q[h]]+w[i]&&v[i])
10         d[q[++t]=to[i]]=d[q[h]]+w[i];
11     return d[pc]>0;
12 }
13 void link(int a,int b,int V,int W){l[++ec]=fir[a];fir[a]=ec;to[ec]=b;v[ec]=V;w[ec]=W;}
14 void con(int a,int b,int W){link(a,b,1,W);link(b,a,0,-W);}
15 int dfs(int p,int f){
16     if(p==pc)return f;int r=f;al[p]=1; 
17     for(int i=fir[p];i;i=l[i])if(!al[to[i]]&&v[i]&&d[to[i]]==d[p]+w[i]){
18         int x=dfs(to[i],1);
19         if(!x)d[to[i]]=-n;
20         else v[i]--,v[i^1]++,r--,ans+=w[i];
21     }return f-r;
22 }
23 int main(){
24     cin>>n;int sq=sqrt(n);
25     for(int i=2;i<=n;++i)if(!np[i]){
26         ll r=i;while(1ll*r*i<=n)r*=i;
27         ans+=r;p[++pc]=i;f[pc]=r;
28         for(int j=i+i;j<=n;j+=i)np[j]=1;
29     }
30     for(int i=1;p[i]<=sq;++i)for(int j=pc;p[j]>sq;--j)if(F(p[i],p[j])>f[i]+f[j])
31         con(i,j,F(p[i],p[j])-f[i]-f[j]);
32     for(int i=1;p[i]<=sq;++i)con(0,i,0);++pc;
33     for(int i=pc-1;p[i]>sq;--i)con(i,pc,0);
34     while(bfs())dfs(0,n);
35     cout<<ans+1<<endl;
36 }
Boring conclusion title. But actually does not permit

  

Guess you like

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