D-query SPOJ - DQUERY (莫队算法裸题)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.
Input
Line 1: n (1 ≤ n ≤ 30000).
Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
Output
For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.
Example
Input
5
1 1 2 1 3
3
1 5
2 4
3 5
Output
3
2
3
題意:
給你一個(gè)長(zhǎng)度為n的數(shù)組,和m個(gè)詢問,對(duì)于每一個(gè)詢問,請(qǐng)你輸出數(shù)組在 l 到 r 區(qū)間中,有多少個(gè)不同的數(shù)字。
思路:
用一個(gè)flag[i] 數(shù)組 表示當(dāng)前區(qū)間中出現(xiàn)了多少次i數(shù)字。
利用flag數(shù)組進(jìn)行常規(guī)的add、del 轉(zhuǎn)移即可。
細(xì)節(jié)見代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int *p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ pll ans[maxn]; ll Ans = 0ll; int l = 0; int r = 0; struct node {int l, r, id; } a[maxn]; int pos[maxn]; int n, m; int len; bool cmp(node aa, node bb) {if (pos[aa.l] == pos[bb.l]) {return aa.r < bb.r;} else {return pos[aa.l] < pos[bb.l];} } int col[maxn]; int flag[maxn]; void add(int x) {if (flag[col[x]]++ == 0) {Ans++;} } void del(int x) {if (--flag[col[x]] == 0) {Ans--;} } int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gg(n); len = (int)(sqrt(n));repd(i, 1, n) {gg(col[i]);}gg(m);repd(i, 1, m) {gg(a[i].l);gg(a[i].r);a[i].id = i;pos[i] = i / len;}sort(a + 1, a + 1 + m, cmp);repd(i, 1, m) {while (l > a[i].l) {l--;add(l);}while (r < a[i].r) {r++;add(r);}while (l < a[i].l) {del(l);l++;}while (r > a[i].r) {del(r);r--;}ans[a[i].id].fi = Ans;}repd(i, 1, m) {printf("%lld\n", ans[i].fi);}return 0; }inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11365133.html
總結(jié)
以上是生活随笔為你收集整理的D-query SPOJ - DQUERY (莫队算法裸题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于更改当前公司(一)--ChangeC
- 下一篇: Powerful array CodeF