permu: Team Mo + segment tree

This question is water past a few days ago, is now almost no impression, and out of the water.

First, let us see it solved is the longest continuous length range, very good.

Then I think of Mountains and Seas, but I have not done yet.

Then he thought for a long time before an exam T3 hotel hotel (I use violence directly over the QAQ), also positive solutions segment tree.

But I can not think of a tree line, simply because I think the team is currently in school will only use Mo Mo team.

Later still he asked the students.

Then very simple.

We consider asking these operations range.

One approach is to solve a variety of tree fairy tree cover range problem.

Another approach is to cheat points Mo team.

Do not team. . Go left (escape

So this question ideas can come out: Mo operating team, every position into the "1" in the tree line number range for this location, delete decrease to "1", and then directly answer is [1, n] of length of the longest continuous period.

If you have done a similar tree line of the title, this kind of thing is very simple to feed.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int N=5e4+5;
 8 inline int read(){
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0'||ch>'9') f=(ch=='-')?-1:1,ch=getchar();
11     while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
12     return x*f;
13 }
14 
15 int n,m,blo;
16 int col[N],bel[N],ans[N];
17 struct node{
18     int l,r,ord;
19     inline friend bool operator<(node a,node b){
20         return (bel[a.l] ^ bel[b.l]) ? bel[a.l] < bel[b.l] : ((bel[a.l] & 1) ? a.r < b.r : a.r > b.r);
21     }
22 }q[N];
23 struct Seg_Tree{
24 #define lch k<<1
25 #define rch k<<1|1
26     struct Node{
27         int lmax,rmax,mmax;
28         int len;
29     }tr[N<<2];
30     inline void Build(int k,int l,int r){
31         tr[k].len=r-l+1;
32         if(l==r) return;
33         int mid=(l+r)>>1;
34         Build(lch,l,mid);Build(rch,mid+1,r);
35     }
36     inline void up(int k){
37         tr[k].lmax=tr[lch].lmax;
38         tr[k].rmax=tr[rch].rmax;
39         if(tr[lch].lmax==tr[lch].len) tr[k].lmax+=tr[rch].lmax;
40         if(tr[rch].rmax==tr[rch].len) tr[k].rmax+=tr[lch].rmax;
41         tr[k].mmax=max(max(tr[lch].mmax,tr[rch].mmax),tr[lch].rmax+tr[rch].lmax);
42     }
43     inline void Insert(int k,int l,int r,int x){
44         if(l==x&&r==x){
45             tr[k].lmax=1;tr[k].rmax=1;tr[k].mmax=1;
46             return;
47         }
48         int mid=(l+r)>>1;
49         if(x<=mid)Insert(lch,l,mid,x);
50         else Insert(rch,mid+1,r,x);
51         up(k);
52     }
53     inline void Erase(int k,int l,int r,int x){
54         if(l==x&&r==x){
55             tr[k].lmax=0;tr[k].rmax=0;tr[k].mmax=0;
56             return;
57         }
58         int mid=(l+r)>>1;
59         if(x<=mid)Erase(lch,l,mid,x);
60         else Erase(rch,mid+1,r,x);
61         up(k);
62     }
63 }str;
64 
65 int main(){
66     n=read();
67     m=read();
68     blo=(int)sqrt(n)+1;
69     str.Build(1,1,n);
70     for(int i=1;i<=blo;++i)
71         for(int j=1;j<=blo;++j)
72         {bel[(i-1)*blo+j]=i;if((i-1)*blo+j==n) break;}
73     for(int i=1;i<=n;++i) col[i]=read();
74     for(int i=1;i<=m;++i){
75         q[i].l=read();
76         q[i].r=read();
77         q[i].ord=i;
78     }
79     sort(q+1,q+m+1);
80     register int l=q[1].l,r=q[1].r;
81     for(int i=l;i<=r;++i) str.Insert(1,1,n,col[i]);
82     for(int i=1;i<=m;++i){
83         while(r<q[i].r) str.Insert(1,1,n,col[++r]);
84         while(l>q[i].l) str.Insert(1,1,n,col[--l]);
85         while(l<q[i].l) str.Erase(1,1,n,col[l++]);
86         while(r>q[i].r) str.Erase(1,1,n,col[r--]);
87         ans[q[i].ord]=str.tr[1].mmax;
88     }
89     for(int i=1;i<=m;++i) printf("%d\n",ans[i]);
90     return 0;
91 }
View Code

 

Guess you like

Origin www.cnblogs.com/hzoi2018-xuefeng/p/11333135.html