題目連結
分析
我隻能說太妙了…
離線 + 樹狀數組
參考題解:
這個題要和《HH的項鍊》做對比;
關鍵就是為什麼要離線處理,以及 怎麼對區間進行排序和維護;
[SDOI2009]HH的項鍊:
這個題求的是區間中不同數的個數,假設我們從前往後周遊;
周遊到a[i]時,對于前面的x(x=a[i]),我們要選一個做代表;
不妨選a[i]作為代表,則我們應該消除前面所有x的影響;
我們隻用記錄每個數出現的上個位置,然後每次更新就行;
然後我們發現這個過程是不可逆的,如果先處理r大的再處理r小的那麼就會有問題;
是以我們需要對r從小到大排序;
[HEOI2012]采花:
這個題不同,這個題隻有區間中的數出現>=2時才會算一次貢獻;
這個問題就很奇怪…我們可以維護一下相鄰相同數的關系;
即記錄一下a[i]這個數下一次出現的下标ne[a[i]];
從前往後考慮:(也是用一個指針now疊代)
對于一個數a[now],如果目前詢問的區間是[l,r],now<l;
即a[now]在詢問區間外,那麼a[now]的下一個數ne[a[now]]在這個區間不産生貢獻;
我們可以update(ne[a[now]],-1);
但是對于ne[ne[a[now]]],是有可能産生貢獻的,是以我們update(ne[ne[a[now]]],1);
由此可以知道我們這個now指針和區間l是有關的,now從前往後周遊l,我們可以按l從小到大排區間;
此時我們query®,這樣區間内出現過一次的數都沒有貢獻,出現過多次的數我們也隻算了一次(在第二次出現的時候打了一次标記)
這裡為啥不用query(l)?
其實我們在周遊now的時候,a[now]第一次出現是沒有算貢獻的,當l變大;
now在周遊時,同時會更新,消除區間外的數的影響,是以query(l)是0;
代碼
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
#include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second
#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double>> PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;
const int maxn = 3e6 + 7;
const int N = 2010 + 7;
const int M = 1e6 + 7;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}
int n,c,m;
int a[maxn];
int ne[maxn];
int tr[maxn];
int vis[maxn];
int ans[maxn];
int pos[maxn];
struct node
{
int l,r;
int id;
}no[maxn];
bool cmp(node no1,node no2)
{
return no1.l < no2.l;
}
void update(int x,int v)
{
while(x <= n)
{
tr[x] += v;
x += lowbit(x);
}
}
int query(int x)
{
int ans = 0;
while(x)
{
ans += tr[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
scanf("%d %d %d",&n,&c,&m);
for(int i=1;i<=n;i++) ire(a[i]);
for(int i=1;i<=m;i++)
{
int l,r;
iire(l,r);
no[i] = {l,r,i};
}
sort(no+1,no+1+m,cmp);
//從後往前處理每個數的下個數的位置
for(int i=n;i>=1;i--)
{
ne[i] = pos[a[i]];
pos[a[i]] = i;
}
//初始化,每個數第二次出現的位置打标記
for(int i=1;i<=n;i++)
if(!vis[a[i]] && ne[i]) update(ne[i],1), vis[a[i]] = 1;
int now = 0;
for(int i=1;i<=m;i++)
{
while(now < no[i].l) //在區間外
{
if(ne[now])
{
update(ne[now],-1);
if(ne[ne[now]]) update(ne[ne[now]],1);
}
now++;
}
ans[no[i].id] = query(no[i].r);
}
for(int i=1;i<=m;i++) cout<<ans[i]<<'\n';
return 0;
}