1188 - Fast Queries (莫队算法)

1188 - Fast Queries
Time Limit: 3 second(s)Memory Limit: 64 MB

Given an array of N integers indexed from 1 toN, and q queries, each in the form i j, you have to findthe number of distinct integers from index i to j (inclusive).

Input

Input starts with an integer T (≤ 5),denoting the number of test cases.

The first line of a case is a blank line. The next linecontains two integers N (1 ≤ N ≤ 105), q (1≤ q ≤ 50000). The next line contains N space separatedintegers forming the array. There integers range in [0, 105].

Each of the next q lines will contain a query whichis in the form i j (1 ≤ i ≤ j ≤ N).

Output

For each test case, print the case number in a single line.Then for each query you have to print a line containing number of distinctintegers from index i to j.

Sample Input

Output for Sample Input

1

 

8 5

1 1 1 2 3 5 1 2

1 8

2 3

3 6

4 5

4 8

Case 1:

4

1

4

2

4

Note

Dataset is huge. Use faster I/O methods.


题解: 裸的莫队分块,时间复杂度近似为q*(n^1.5).

AC代码:

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月22日 星期日 11时41分51秒
File Name     :lightoj/1188/Fast_Queries.cpp
************************************************ */
 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
 
using namespace std;
 
 
#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)
 
 
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = MAXN;
const int mod = 1e9 + 7;
int a[MAXN],num[MAXN];
int n,q,unit,ans[MAXN];
 
struct Node{
    int l,r,id;
    bool operator < (const Node &a) const{
        if(l / unit != a.l / unit) return l / unit < a.l / unit;
        return r < a.r;
    }
    void read(int i){
        scanf("%d %d",&l,&r);
        id = i;
    }
}p[MAXN];
 
void work(){
    int temp = 0;
    MEM(num);
    int L = 1,R = 0;
    for(int i = 0; i < q; i++){
        while(R > p[i].r){
            num[a[R]]--;
            if(num[a[R]] == 0) temp--;
            R--;
        }
        while(R < p[i].r){
            R++;
            num[a[R]]++;
            if(num[a[R]] == 1) temp++;
        }
        while(L > p[i].l){
            L--;
            num[a[L]]++;
            if(num[a[L]] == 1) temp++;
        }
        while(L < p[i].l){
            num[a[L]]--;
            if(num[a[L]] == 0) temp--;
            L++;
        }
        ans[p[i].id] = temp;
    }
}
 
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T,cnt = 0;
    cin>>T;
    while(T--){
        scanf("%d %d",&n,&q);
        REP_1(i,n) scanf("%d",&a[i]);
        REP(i,q) p[i].read(i);
        unit = (int)sqrt(n + 0.5);
        sort(p,p + q);
        work();
        printf("Case %d:\n",++cnt);
        for(int i = 0; i < q; i++) printf("%d\n",ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值