《洛谷P3901 数列找不同》

提供两种解法。

第一种:直接对询问离线,然后莫队分块。

我们用一个数组vis来统计每个数出现的次数,然后维护区间内出现次数超过2的数量即可。

一开始加了个vis[i] = max(vis[i],0)防负数操作还wa了,这样其实有点违背统计思想了。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e5+5;
const int M = 12005;
const LL Mod = 199999;
#define rg register
#define pi acos(-1)
#define INF 1e5+1
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int n,q,a[N],ans[N],vis[N],cnt = 0;//cnt记录出现超过一次的数字的数量
struct Node{int L,r,id,bl;}p[N];
bool cmp(Node a,Node b)
{
    if(a.bl != b.bl) return a.L < b.L;
    if(a.bl&1) return a.r < b.r;
    return a.r > b.r; 
}
void del(int u)
{
    int x = a[u];
    if(vis[x] == 2) cnt--; 
    vis[x]--;
}
void add(int u)
{
    int x = a[u];
    //printf("x is %d vis is %d\n",x,vis[x]);
    if(vis[x] == 1) cnt++;
    vis[x]++;
}
int main()
{
    n = read(),q = read();
    int blsize = sqrt(n);//块的数量
    for(rg int i = 1;i <= n;++i) a[i] = read();
    for(rg int i = 1;i <= q;++i) 
    {
        p[i].L = read(),p[i].r = read();
        p[i].id = i,p[i].bl = (p[i].L-1)/blsize+1;//左端点属于哪个块
    }
    sort(p+1,p+q+1,cmp);
    int L = 0,r = 0;
    for(rg int i = 1;i <= q;++i)
    {
        while(L < p[i].L) del(L++);
        while(L > p[i].L) add(--L);
        while(r < p[i].r) add(++r);
        while(r > p[i].r) del(r--);
        ans[p[i].id] = cnt;
    }
    for(rg int i = 1;i <= q;++i) printf("%s\n",ans[i] > 0 ? "No" : "Yes");
  //  system("pause");
}
View Code

第二种:可以发现,对于每个点的值,我们可以统计它上一次出现的位置。

只要这个区间内的所有点上一次出现的位置都 < L,那么说明满足。

如果我们遍历L到r去查看每个点的上一次出现位置,显然会T。

所以我们可以记录最大的上一次出现位置,只要判断这个位置是否满足即可(因为这个位置会影响答案)

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e5+5;
const int M = 12005;
const LL Mod = 199999;
#define rg register
#define pi acos(-1)
#define INF 1e5+1
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int n,q,a[N],Max_pre[N],vis[N];
int main()
{
    n = read(),q = read();
    for(rg int i = 1;i <= n;++i) a[i] = read();
    for(rg int i = 1;i <= n;++i)
    {
        Max_pre[i] = max(Max_pre[i-1],vis[a[i]]);
        vis[a[i]] = i;
    }
    while(q--)
    {
        int L,r;L = read(),r = read();
        if(Max_pre[r] >= L) printf("No\n");
        else printf("Yes\n");
    }
  //  system("pause");
}
View Code

 

posted @ 2020-08-29 12:07  levill  阅读(153)  评论(0)    收藏  举报