P3901 series to find a different (simple Mo Team)

Mo Getting the team title, the interval is not the same as the number of queries

You should be able to do with a tree line, but do not know what to maintenance.

A very good solution to a problem:

https://www.cnblogs.com/MisakaAzusa/p/8684319.html

Mo team algorithmic problems the main problem:

Mo algorithm is used to handle a team like offline intervals inquiry without modification issues.

Mo algorithms team thinking:

1, the sorting block and the adjacent query section as close as possible, and as little as possible from curL two pointer movement curR

2, row bucket inquiry

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+5;
 5 
 6 int n,m,blo;
 7 int Ans=0,curL,curR,cnt[maxn];
 8 //the initial values of curL and curR both are 0
 9 struct node{
10     int l,r,q;
11 }Q[maxn];
12 int a[maxn];
13 int ans[maxn];
14 
15 bool cmp(node x,node y){
16     return (x.l/blo)==(y.l/blo) ? x.r<y.r : x.l<y.l;
17 }
18 
19 void add(int pos){
20     if((++cnt[a[pos]])==1) ++Ans;
21 }
22 
23 void del(int pos){
24     if((--cnt[a[pos]])==0) --Ans;
25 }
26 
27 
28 int main(){
29     cin>>n>>m;blo=sqrt(n);
30     for(int i=1;i<=n;i++)
31         cin>>a[i];
32     for(int i=1;i<=m;i++){
33         cin>>Q[i].l>>Q[i].r;
34         Q[i].q=i;    
35     }
36     
37     sort(Q+1,Q+1+m,cmp);
38     
39      for(int i=1;i<=m;i++){
40         int L=Q[i].l;int R=Q[i].r;
41         while(curL<L) del(curL++);
42         while(curL>L) add(--curL);
43         while(curR<R) add(++curR);
44         while(curR>R) del(curR--);
45         if(Ans==(R-L+1))
46             ans[Q[i].q]=1; 
47     }
48 
49     for(int i=1;i<=m;i++)
50     {
51         if(ans[i]==1) cout <<"Yes\n";
52             else cout <<"No\n";
53     }
54     return 0;
55 }

 

Guess you like

Origin www.cnblogs.com/Msmw/p/11466456.html