8.3 solution to a problem

T1 [Rockwell Valley, P3938]

Obviously the topic, Fibonacci, no doubt find the law, the examination room, and if you are sensitive enough to Fibonacci, then I think you can expect positive solution, after all, 14 people examination room AC, very still water If it can not think, can play table looking at violent father, after all, you are constantly turning to his father, no matter what method it will eventually find $ fa [x] = x-fib (x birth month -1 ) $, then we'll think about it for a $ le12 $ data, he kept turning his father would turn up to how many times you'll find it 60 times, this question can be done at this time, we do not even doubled, can directly turn, but because you do not know the depth, so it is necessary to first two points are doubled again, to find the depth, then multiply $ lca $ similar approach can be a direct translation

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<set>
 4 #define maxf 62
 5 #define re register
 6 #define ll long long
 7 using namespace std;
 8 const int L=1<<20|1;
 9 char buffer[L],*S,*T;
10 #define getchar() ((S==T&&(T=(S=buffer)+fread(buffer,1,L,stdin),S==T))?EOF:*S++)
11 ll m;
12 ll f[maxf];
13 set <ll> ccc;
14 inline ll read()
15 {
16     re ll aa=0,bb=1;re char cc=getchar();
17     while(cc>'9'||cc<'0'){if(cc=='-') bb=-1;cc=getchar();}
18     while(cc<='9'&&cc>='0'){aa=aa*10+cc-'0';cc=getchar();}
19     return aa*bb;
20 }
21 inline int ef(re ll x)
22 {
23     re int l=1,r=61;
24     while(l<r)
25     {
26         if(l+1==r)
27         {
28             if(x>f[l])  return r;
29             else  return l;
30         }
31         re int mid=(l+r)>>1;
32         if(x>f[mid])  l=mid+1;
33         else  r=mid;
34     }
35     return l;
36 }
37 int main()
38 {
39     m=read();
40     f[0]=1;  f[1]=1;
41     for(re int i=2;i<maxf;++i)  f[i]=f[i-1]+f[i-2];
42     for(re int i=1;i<=m;++i)
43     {
44         ll a=read(),b=read();  ll ls1=a,ls2=b;
45         int deepa=0,deepb=0;
46         while(ls1>1)  {deepa++;  ls1-=f[ef(ls1)-1];}
47         while(ls2>1)  {deepb++;  ls2-=f[ef(ls2)-1];}
48         if(deepa<deepb)  {swap(deepa,deepb);  swap(a,b);}
49         while(deepa>deepb)  {a-=f[ef(a)-1]; deepa--;}
50         while(a!=b)  {a-=f[ef(a)-1];  b-=f[ef(b)-1];}
51         printf("%lld\n",a);
52     }
53     return 0;
54 }
View Code

T2 [Luo valley P3939]

This question and BZOJ2120 after the similarity is not an ordinary high-ah, $ BZOJ2120 $ Mo, but a team of board repair with questions, but unfortunately, I did not learn, then decadent solution to a problem, it gave me a very simple question of feeling as long as the $ STL $ 6 to be launched into this problem, no problem, this problem sorted tuple, colors do first dimension, the second dimension position to do, let's consider modifying the operation of it, you will find that he is switching $ x $ and $ x + 1 $ position, then good for you row the sequence is not affected, as long as you can get rid of the position, so just need to find lower_bound point you want to modify, the second dimension you can change it ideas, but because of the particularity, the usual questions lower_ bound structures were sorted tuple are discarded, and this time there appeared a new $ STL $: $ pair $, in fact, it is a structure, but has been defined well, and only two-dimensional, the specific implementation method can refer to the following code, modified to get, then that is asked, since it is sorted by color, it only needs a upper_bound and a lower_bound, it was used to find the first more than the $ $ R & lt position and a greater than or equal to $ $ L position, can make a difference

Saying that the problem hit $ treap $, Chairman of the tree, the tree cover tree and so on, all kinds of data structures, often vigorously card can get a good result, $ treap $ A can be directly

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #define maxn 300100
 5 using namespace std;
 6 pair <int,int> a[maxn];
 7 int n,m;
 8 int Cc[maxn];
 9 inline int read(){
10     register int ret;
11     register char r;
12     while(r=getchar(),r<'0'||r>'9');ret=r-48;
13     while(r=getchar(),r>='0'&&r<='9')ret=ret*10+r-48;
14     return ret;
15 }
16 int main()
17 {
18     n=read();  m=read();
19     for(int i=1;i<=n;++i)  {a[i].first=read();  Cc[i]=a[i].first;  a[i].second=i;}
20     sort(a+1,a+n+1);
21     for(int i=1;i<=m;++i)
22     {
23         int opt=read();
24         if(opt==1)
25         {
26             int l=read(),r=read(),Col=read();
27             pair <int,int> ls;
28             ls.first=Col;  ls.second=l;
29             int wz1=lower_bound(a+1,a+n+1,ls)-a;
30             pair <int,int> lss;
31             lss.first=Col;  lss.second=r;
32             int wz2=upper_bound(a+1,a+n+1,lss)-a;
33             int ans=wz2-wz1;
34             printf("%d\n",ans);
35         }
36         else
37         {
38             int x=read();
39             if(Cc[x]==Cc[x+1])  continue;
40             else
41             {
42                 int ls=Cc[x];
43                 pair <int,int> ls1;
44                 ls1.first=Cc[x];  ls1.second=x;
45                 int wz1=lower_bound(a+1,a+n+1,ls1)-a;
46                 a[wz1].second=x+1;  Cc[x]=Cc[x+1];
47                 pair <int,int> ls2;
48                 ls2.first=Cc[x+1];  ls2.second=x+1;
49                 int wz2=lower_bound(a+1,a+n+1,ls2)-a;
50                 a[wz2].second=x;  Cc[x+1]=ls;
51             }
52         }
53     }
54     return 0;
55 }
View Code

 

T3 [Luo valley P3940]

This problem $ k == 1 || k == 2 $ a glance friendship ah , the first lie eight points no problem

Honest $ k == 1 $ is a violence, can not because of conflict within each block, so violent sweep again, encountered conflicting packets directly on it, then lexicographically smallest multi-block is in front, behind a small packet chanting, that sweep from back to front, directly greedy can do, but if the violence sweeping the number in the current block, judge whether a conflict, a great likelihood of T, we will find that he has an interesting property, the maximum number of Why 131072 it is generally not the number should be $ le $ a few such thing, well it happens, plus two and 131072, is exactly $ 512 ^ 2 $, then we enumerate 512, definitely better than enumerate all the current block the number of cost-effective ah, so, to 512 direct enumeration to list all of the square of the number, and the current point to do is to join the difference, look at the difference in the presence or absence of the current group on it

For $ k == 2 $, and imprisoned criminals a little like, and these two questions, with disjoint-set or cross-dyeing sentenced bipartite graph can do, I use a bipartite graph, each adding a point, on the right in the current group All points run again bipartite graph, if not constitute a bipartite graph, packets directly to, and disjoint-set ideas similar to this, still from the back sweep greedy

$ Tips $: Do not empty when $ memset $, put in a few points to clear a few points, otherwise it will not pay attention to a T, but be sure to empty ah! ! There is a repeat of the point, do not forget to build map

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<vector>
  5 #include<cstring>
  6 #define maxn 270000
  7 using namespace std;
  8 /*const int L=1<<20|1;
  9 char buffer[L],*S,*T;
 10 #define getchar() ((S==T&&(T=(S=buffer)+fread(buffer,1,L,stdin),S==T))?EOF:*S++)*/
 11 int n,k,js=1;
 12 int a[maxn],ans[maxn],pd[maxn];
 13 vector <int> b[maxn],bb[maxn],c[maxn];
 14 int jss,bjjj;
 15 int head[maxn],to[maxn*2],xia[maxn*2],visit[maxn];
 16 inline int read()
 17 {
 18     int ret;
 19     char r;
 20     while(r=getchar(),r<'0'||r>'9');ret=r-48;
 21     while(r=getchar(),r>='0'&&r<='9')ret=ret*10+r-48;
 22     return ret;
 23 }
 24 void add(int x,int y)
 25 {
 26     to[++jss]=y;  xia[jss]=head[x];  head[x]=jss;
 27 }
 28 void dfs(int x,int colo)
 29 {
 30     visit[x]=colo;
 31     for(int i=head[x];i;i=xia[i])
 32     {
 33         int ls=to[i];
 34         if(ls<=bb[js][0])
 35         {
 36             if(visit[ls]==0)  dfs(ls,3-colo);
 37             else if(visit[ls]==colo)  {bjjj=1;  return ;}
 38         }
 39     }
 40 }
 41 int main()
 42 {
 43 //    freopen("division18.in","r",stdin);
 44     n=read();  k=read();
 45     for(int i=1;i<=n;++i)  a[i]=read();
 46     if(k==1)
 47     {
 48         int zz=n;
 49         while(zz>0)
 50         {
 51             if(b[js].size()!=0)
 52                 for(int i=2;i<=512;++i)
 53                 {
 54                     int ls1=i*i-a[zz];
 55                     if(pd[ls1]==1)
 56                     {
 57                         for(int i=0;i<b[js].size();++i)  pd[b[js][i]]=0;
 58                         ans[++js]=zz;  break;
 59                     }
 60                 }
 61             b[js].push_back(a[zz]);  pd[a[zz]]=1;  zz--;
 62         }
 63         printf("%d\n",js);
 64         for(int i=js;i>=2;--i)  printf("%d ",ans[i]);
 65         puts("");
 66     }
 67     else
 68     {
 69         for(int i=n;i>=1;--i)
 70         {
 71             if(b[js].size()==0)
 72             {
 73                 b[js].push_back(a[i]);  bb[js].push_back(i);
 74                 pd[a[i]]=1;  c[a[i]].push_back(i);
 75                 continue;
 76             }
 77             for(int j=2;j<=512;++j)
 78             {
 79                 int ls1=j*j-a[i];  bjjj=0;
 80                 if(pd[ls1]!=0)
 81                 {
 82                     for(int k=0;k<c[ls1].size();++k)
 83                         if(js==1||c[ls1][k]<=bb[js][0])
 84                             {add(c[ls1][k],i);  add(i,c[ls1][k]);}
 85                     for(int k=0;k<bb[js].size();++k)  visit[bb[js][k]]=0;
 86                     for(int k=0;k<bb[js].size();++k)
 87                         if(visit[bb[js][k]]==0)  dfs(bb[js][k],1);
 88                     for(int k=0;k<bb[js].size();++k)  visit[bb[js][k]]=0;
 89                     visit[i]=0;
 90                 }
 91                 if(bjjj==1)
 92                 {
 93                     for(int k=1;k<=jss;++k)  {to[k]=0;  xia[k]=0;}
 94                     for(int k=0;k<b[js].size();++k)
 95                                             {head[bb[js][k]]=0;  pd[b[js][k]]=0;}
 96                     head[i]=0;  jss=0;  js++;
 97                 }
 98             }
 99             b[js].push_back(a[i]);  bb[js].push_back(i);
100             c[a[i]].push_back(i);  pd[a[i]]=1;
101         }
102         printf("%d\n",js);
103         for(int i=js;i>=2;--i)  printf("%d ",bb[i][0]);
104         puts("");
105     }
106     return 0;
107 }
View Code

 

Guess you like

Origin www.cnblogs.com/hzjuruo/p/11299372.html