Spoj 3267. D-query (Chairman tree or tree-like array offline)

Source: Internet
Author: User

A-D-queryTime limit:MS Memory Limit:0KB 64bit IO Format:%lld & %llu SubmitStatusPractice Spoj dqueryAppoint Description:System Crawler (2014-12-06)

Description

中文版 Vietnamese

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 had 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.
    • 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.

Example
Input Output

is to ask the number of different elements in the interval, or you can use a tree-like array to operate offline or use the Chairman tree

Chairman Tree Edition:

/************************************************************************* > File Name:cf.cpp > AUTHOR:ACVCL A > QQ: > Mail: [email protected] > Created time:1949äê10ôâ1èõðçæúò»0ê±0 ö0ãë************************************************************************/#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <cstring> #include <map># include<queue> #include <stack> #include <string> #include <cstdlib> #include <ctime># Include<set> #include <math.h>using namespace std;typedef long long ll;const int MAXN = 3e4 + Ten; #define REP (i,a    , b) for (int i= (a); i<= (b); i++) #define PB push_backint tot,n,m,date[maxn],root[maxn];struct chairtnode{int s,rc,lc; Chairtnode (int s=0,int lc=0,int rc=0): s (s), LC (LC), RC (RC) {}};chairtnode chairt[maxn*20];int newnode (int sum,int Lson,    int rson) {int rt=++tot;    Chairt[rt]=chairtnode (Sum,lson,rson); return RT;} void Insert (int &rt,int pre_rt,int pos,int l,int r,int val) {Chairtnode &t=chairT[pre_rt];    Rt=newnode (T.S+VAL,T.LC,T.RC);    if (l==r) return;    int mid= (L+R) >>1;    if (pos<=mid) Insert (chairt[rt].lc,t.lc,pos,l,mid,val); else Insert (chairt[rt].rc,t.rc,pos,mid+1,r,val);}    int Query (int rt,int l,int r,int pos)//query sum of [Pos,r]{if (L==pos) return chairt[rt].s;    int mid= (L+R) >>1;    if (Pos<=mid) return Query (Chairt[rt].lc,l,mid,pos) +chairt[chairt[rt].rc].s; Return Query (Chairt[rt].rc,mid+1,r,pos);}        int main () {while (~SCANF ("%d", &n)) {Rep (i,1,n) {scanf ("%d", date+i);        } tot=root[0]=0;        map<int,int>q;        int t; for (int i=1;i<=n;i++) {if (!q[date[i]) {Insert (root[i],root[i-1],i,1,n,1);//number of positions I plus 1}els            e{Insert (t,root[i-1],q[date[i]],1,n,-1);//has already appeared before, minus the previous insert (root[i],t,i,1,n,1);//Add to the present this time}        Q[date[i]]=i;      } int QL,QR;  scanf ("%d", &m);            while (m--) {scanf ("%d%d", &AMP;QL,&AMP;QR);        printf ("%d\n", Query (ROOT[QR],1,N,QL)); }} return 0;}

Tree-shaped array version:


#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm> #include <map>const int maxn1=3e4+5;const int maxn2=2e5+5;typedef Long long ll;using namespace std; int l[maxn2],r[maxn2],loc[maxn1],a[maxn1],id[maxn2],n;int c[maxn1],ans[maxn2];inline int lowbit (int x) {return x& -X;}        void Update (int x,int d) {while (x<=n) {c[x]+=d;    X+=lowbit (x);    }}int Query (int x) {int sum=0;        while (x>0) {sum+=c[x];    X-=lowbit (x); } return sum;} BOOL CMP (int I,int j) {return r[i]<r[j];}        int main () {while (~SCANF ("%d", &n)) {memset (loc,0,sizeof (Loc));        memset (c,0,sizeof (C));        map<int,int>q;        Q.clear ();            for (int i=1;i<=n;i++) {scanf ("%d", a+i);            Update (i,1);            if (!q[a[i]]) {q[a[i]]=i;        }} int m;        scanf ("%d", &m); for (int i=1;i<=m;i++)       {scanf ("%d%d", l+i,r+i);        Id[i]=i;        } sort (id+1,id+1+m,cmp);        /* The main idea is to drag the count as far as possible to the back */int right=1;            for (int i = 1; I <= m; ++i) {int x=id[i]; while (Right<=r[x]) {if (q[a[right]]!=right) {update (Q[A[RIGHT]],-1);/* in the back                Now, let the front count offset, in the back count, to ensure that the number of query interval is not omitted */q[a[right]]=right;            } ++right;            } Right=r[x];        Ans[x]=query (R[x])-query (l[x]-1);    } for (int i=1;i<=m;i++) printf ("%d\n", Ans[i]); } return 0;}






Spoj 3267. D-query (Chairman tree or tree-like array offline)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.