您的位置:首页 > 产品设计 > UI/UE

1188 - Fast Queries (莫队算法)

2015-11-25 11:53 393 查看
[align=center]1188 - Fast Queries [/align]



PDF (English)
Statistics
Forum
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息