SPOJ DQUERY

传送门:https://vjudge.net/problem/32356/origin

题意:

有n个数和m次询问

每次询问问你区间[l,r]内数的种类数有多少

题解:

莫队裸题

将询问按照左端点分块排序

然后对于每一块暴力处理

用vis数组标记数字出现的次数

代码:

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O  =  /O
//                        ____/`---'\____
//                      .'  |     |//  `.
//                     /  |||  :  |||//  
//                    /  _||||| -:- |||||-  
//                    |   | \  -  /// |   |
//                    | \_|  ''---/''  |   |
//                      .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;` _ /`;.`/ - ` : | |
//                 `-.   \_ __ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********
")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
"

const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int pos[maxn];
struct node {
    int l, r;
    int id;
} q[maxn];
bool cmp(node a, node b) {
    if(pos[a.l] == pos[b.l]) return a.r < b.r;
    return pos[a.l] < pos[b.l];
}
int vis[maxn];
int a[maxn];
int ans[maxn];
int Ans = 0;
void add(int x) {
    if(vis[a[x]] == 0) {
        Ans++;
    }
    vis[a[x]]++;
}
void del(int x) {
    vis[a[x]]--;
    if(vis[a[x]] == 0) {
        Ans--;
    }
}
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n;
    scanf("%d", &n);
    int sz = sqrt(n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        pos[i] = i / sz;
    }
    int m;
    scanf("%d", &m);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &q[i].l, &q[i].r);
        q[i].id = i;
    }
    sort(q + 1, q + m + 1, cmp);
    int L = 1, R = 0;
    for(int i = 1; i <= m; i++) {
        while(R < q[i].r) {
            R++;
            add(R);
        }
        while(L < q[i].l) {
            del(L);
            L++;
        }
        while(R > q[i].r) {
            del(R);
            R--;
        }
        while(L > q[i].l) {
            L--;
            add(L);
        }
        ans[q[i].id] = Ans;
    }
    for(int i = 1; i <= m; i++) {
        printf("%d
", ans[i]);
    }
    return 0;
}
每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
原文地址:https://www.cnblogs.com/buerdepepeqi/p/10872146.html