HH的项链
这题数据增强之后是真让人感觉HH
LG1972 HH的项链
改数据之后应该用西安段素或者梳妆十足,然而我也懒得改了,就发一个现在过不了的墨对算fa(然而卡一卡说不定能过)
一 墨对是什么
墨对是一个叫莫涛的dalao fa明的,是一种极其暴力的处理区间问题的数据结构(其实分块(逃。
主要利用了分块
二 思想
将询问的区间排序,然后用两个指针X**跳(没了)。
code
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn = 500010; int col[maxn],ans[maxn],sum[1000003],n,m,base,answer; int l,r; struct Q { int left; int right; int id; } ques[maxn]; int q_sum = 0; bool comp(Q a,Q b) { if(a.left/base!=b.left/base) return a.left<b.left; else return a.right<b.right; } inline void add(int x) { if(++ sum[x] == 1) answer ++; } inline void del(int x) { if(-- sum[x] == 0) answer --; } int main() { ios::sync_with_stdio(false); cin>>n; base=sqrt(n); for(int i=1; i<=n; i++) cin>>col[i]; cin>>m; for(int i=1; i<=m; i++) { cin>>ques[++q_sum].left; cin>>ques[q_sum].right; ques[q_sum].id=q_sum; } sort(ques+1,ques+q_sum+1,comp); l = ques[1].left; r = l-1; for(int i=1; i<=q_sum; i++) { while(l<ques[i].left) del(col[l++]); while(l>ques[i].left) add(col[--l]); while(r>ques[i].right) del(col[r--]); while(r<ques[i].right) add(col[++r]); ans[ques[i].id]=answer; } for(int i=1; i<=q_sum; i++) cout<<ans[i]<<endl; return 0; }