洛谷 P3939 数颜色

来源:互联网 发布:淘宝二手网在哪里 编辑:程序博客网 时间:2024/06/05 22:38

洛谷 P3939 数颜色
题目分析
这道题如果想不到正解的话我们可以水分,要有梦想!针对所给出的测试点,我们去水分!两个特殊性质可以水到,还有n<=1000的暴力处理即可,最后我们就有70分!!!惊不惊喜,刺不刺激!下面给出我在考试时打的程序!

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int n,m,ans;int id[500000],a[500000],b[500000],f[500000];int flag2=0,flag1=0;//id,b是为了水分特殊性质2来设置的//f是为了水分特殊性质1来设置的 inline int read(){    int x=0,w=1;char ch=0;    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();    if(ch=='0') w=-1,ch=getchar();    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar();    return x*w;}int main(){    n=read();m=read();    for(register int i=1;i<=n;++i) a[i]=read(),id[a[i]]=i,++f[a[i]];    //水分特殊性质2     for(register int i=1;i<=n;++i) b[i]=a[i];    sort(1+b,1+n+b);    for(register int i=1;i<=n;++i)         if(b[i]==b[i-1]) flag2=1;    if(!flag2) {        for(register int i=1;i<=m;++i) {            int op=read();            if(op==1) {                int l=read(),r=read(),w=read(); ans=0;                if(l<=id[w]&&id[w]<=r) printf("%d\n",1);                else printf("%d\n",0);            }            if(op==2) { int u=read(); swap(a[u],a[u+1]); swap(id[a[u]],id[a[u+1]]); }        }        return 0;    }    //暴力瞎搞搞         for(register int i=1;i<=m;++i) {            int op=read();            if(op==1) {                int l=read(),r=read(),w=read(); ans=0;                if(r-l<n-20){                    for(register int k=l;k<=r;++k) if(a[k]==w) ans++;                    printf("%d\n",ans);                }                if(r-l>=n-20) {                    for(register int k=1;k<l;++k) if(a[k]==w) ++ans;                    for(register int k=r+1;k<=n;++k) if(a[k]==w) ++ans;                    printf("%d\n",f[w]-ans);                }            }            if(op==2) { int u=read(); swap(a[u],a[u+1]); }        }        return 0;    return 0;}

其实本来计算的是没有这么多的,可能由于某些数据不太强吧。
现在给出正解
这道题我们其实可以不用任何数据结构,什么主席树,什么线段树啊,统统不要。我们先用一个动态数组,将每种数在数组中所出现的位置记录下来,那么这个时候数组的下表的含义就变成了第几个树,而数组中的数即为在原数组中的位置,可以确定的是这个数组一定是单调的!然后我们在查询的时候再用到lower_bound和upper_bound分别去查找l和r,最后相减即可,不懂上面两个的可以点击这里
下面给出ac程序

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <vector>#include <cmath>using namespace std ;const int N=(int)3e5+10;int n,m;vector<int>a[N];int f[N];inline int read(){    int x=0,w=1;char ch=0;    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();    if(ch=='0') w=-1,ch=getchar();    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar();    return x*w;}int main() {    freopen("color.in","r",stdin);    freopen("coloe.out","w",stdout);    n=read();m=read();    for (int i=1;i<=n;++i) {f[i]=read(); a[f[i]].push_back(i);}    for (int i=1;i<=m;++i) {        int op,l,r,x;        op=read();        if (op==1) {            l=read();r=read();x=read();           int p1=lower_bound(a[x].begin(),a[x].end(),l)-a[x].begin();           int p2=upper_bound(a[x].begin(),a[x].end(),r)-a[x].begin()-1;            if (p2<p1) printf("%d\n",0);            else printf("%d\n",p2-p1+1);        }        else {            x=read();            if (f[x]==f[x+1]) continue;   int u=lower_bound(a[f[x]].begin(),a[f[x]].end(),x)-a[f[x]].begin();   int v=lower_bound(a[f[x+1]].begin(),a[f[x+1]].end(),x+1)-a[f[x+1]].begin();            a[f[x]][u]=x+1;            a[f[x+1]][v]=x;            swap(f[x],f[x+1]);        }    }    return 0;}