1. 程式人生 > 實用技巧 >《洛谷P3901 數列找不同》

《洛谷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