[Explanations] luogu_P3939_ number of colors (vector bipartite

With each color vector maintenance position appears, about half of the endpoint, the right point by upperbound, exchange also directly to the vector in the exchange

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=300009;
int n,m;
struct node{
    int col,w;
}a[maxn];
vector<int>c[maxn];//颜色x出现位置 
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1,x;i<=n;i++){
        scanf("%d",&x);
        c[x].pb(i);
        a[i].col=x,a[i].w=c[x].size()-1;
    }
    for(int i=1,op,x,y,z;i<=m;i++){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d%d",&x,&y,&z);
            if(c[z].size()==0){
                printf("0\n");continue;
            }
            int pos1=lower_bound(c[z].begin(),c[z].end(),x)-c[z].begin();
            int pos2=upper_bound(c[z].begin(),c[z].end(),y)-c[z].begin();//查右端点用upper 
            printf("%d\n",pos2-pos1);
        }
        else{
            scanf("%d",&x);
            if(a[x].col==a[x+1].col)continue;
            c[a[x].col][a[x].w]=x+1;
            c[a[x+1].col][a[x+1].w]=x;
            swap(a[x],a[x+1]);
        }
    }
}

 

 

 

Guess you like

Origin www.cnblogs.com/superminivan/p/11808877.html