洛谷 #4113. 採花
阿新 • • 發佈:2018-11-26
題意
問l~r區間中數量大於1的花的種類
題解
莫隊
除錯記錄
cmp優化寫炸
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 2000005
using namespace std;
struct node{
int l, r, id;
}q[maxn];
int a[maxn], n, k, m, pos[maxn], cnt[maxn], ans[maxn];
inline bool cmp(node const &a, node const &b){ return (pos[a.l] == pos[b.l]) ? (pos[a.l] & 1) ? a.r < b.r : a.r > b.r : a.l < b.l; }
inline bool idcmp(node const &a, node const &b){ return a.id < b.id; }
inline int read(){
int x = 0; char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar ();
while (ch >= '0' && ch <= '9') x = x * 10 + (ch & 15), ch = getchar();
return x;
}
inline void solve(){
int l = 1, r = 0, sum = 0;
for (int i = 1; i <= m; i++){
for (; r < q[i].r; r++) cnt[a[r + 1]]++, (cnt[a[r + 1]] == 2) ? sum++ : 0;
for (; r > q[i].r; r--) (cnt[ a[r]] == 2) ? sum-- : 0, cnt[a[r]]--;
for (; l < q[i].l; l++) (cnt[a[l]] == 2) ? sum-- : 0, cnt[a[l]]--;
for (; l > q[i].l; l--) cnt[a[l - 1]]++, (cnt[a[l - 1]] == 2) ? sum++ : 0;
ans[q[i].id] = sum;
}
}
inline void write(int x){
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int main(){
n = read(), k = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= m; i++) q[i].l = read(), q[i].r = read(), q[i].id = i;
int block = sqrt(n);
for (int i = 1; i <= m; i++) pos[i] = (i - 1) / block + 1;
sort(q + 1, q + m + 1, cmp);
solve();
// sort(q + 1, q + m + 1, idcmp);
for (int i = 1; i <= m; i++) write(ans[i]), putchar('\n');
return 0;
}