日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队)

發(fā)布時(shí)間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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 5Output 3 2 3

解題報(bào)告:

?

AC代碼:(樹狀數(shù)組,mp存位置,其實(shí)也可以用一個(gè)pre數(shù)組去存位置,(需要先都初始化成-1),用時(shí)分別是170ms和150ms)

#include<bits/stdc++.h>using namespace std; map<int ,int> mp; struct Node {int l,r;int id; } node[200000 + 5]; int n,m,c[30000 + 5],ans[200000 + 5]; int a[30000 + 5]; int lowbit(int x) {return x&-x; } void update(int x,int val) {while(x<=n) {c[x] += val;x+=lowbit(x);} } int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res; } bool cmp(const Node & a,const Node & b) {return a.r < b.r; } int main() {while(~scanf("%d",&n) ) {memset(c,0,sizeof c);mp.clear();for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);}scanf("%d",&m);for(int i = 1; i<=m; i++) {scanf("%d%d",&node[i].l,&node[i].r);node[i].id = i;}int cur = 1;sort(node+1,node+m+1,cmp);for(int i = 1; i<=m; i++) {while(cur <= node[i].r) {if(mp[a[cur] ] == 0) update(cur,1);else {update(mp[a[cur] ] ,-1);update(cur,1);}mp[a[cur] ] = cur;cur++;}ans[node[i].id] = query(node[i].r) - query(node[i].l - 1);}for(int i = 1; i<=m; i++) {printf("%d\n",ans[i]);}}return 0 ; }

AC代碼2:

#include<bits/stdc++.h>using namespace std; const int MAX = 30000 + 5; struct TREE {int l,r;int val; } tree[MAX * 40]; int n,tot; int pre[1000000 + 5],root[MAX],a[MAX]; int build(int l,int r) {int ne = ++tot;tree[ne].val = 0;tree[ne].l=tree[ne].r = 0;if(l == r) return ne;int m = (l+r)/2;tree[ne].l = build(l,m);tree[ne].r = build(m+1,r);return ne; } int update(int pos,int c,int val,int l,int r) {int ne = ++tot;tree[ne] = tree[c];tree[ne].val += val;if(l == r) return ne;int m = (l+r)/2;if(m >= pos) tree[ne].l = update(pos,tree[c].l,val,l,m);else tree[ne].r = update(pos,tree[c].r,val,m+1,r);return ne; } int query(int pos,int c,int l,int r) {if(l == r) return tree[c].val;int m = (l+r)/2;if(m>=pos) return tree[tree[c].r].val + query(pos,tree[c].l,l,m);else return query(pos,tree[c].r,m+1,r); } int main() {cin>>n;memset(pre,-1,sizeof pre);for(int i = 1; i<=n; i++) scanf("%d",a+i);root[0] = build(1,n);//構(gòu)造主席樹 for(int i = 1; i<=n; i++) {if(pre[a[i]] == -1) {root[i] = update(i,root[i-1],1,1,n);}else {int tmp = update(pre[a[i]],root[i-1],-1,1,n);root[i] = update(i,tmp,1,1,n); }pre[a[i]]=i;}int q,x,y;cin>>q;while(q--) {scanf("%d%d",&x,&y);printf("%d\n",query(x,root[y],1,n));}return 0 ; }

莫隊(duì)算法:(還未做、。、)

總結(jié)

以上是生活随笔為你收集整理的【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。