• bzoj 2743&&spoj DQUERY


    大概  这是一类题 离线处理+树状数组 当然可以暴力主席树  要明白主席树的精髓是 没一个rt[i] 对应着一颗线段树  于rt[i-1]相比只有一条链发生变化即可 然后就可以把每次操作将对应的前驱节点(该题特指前一个出现相同元素的节点 进行修改操作  主席树是在线查询 树状数组是离线处理

    2743: [HEOI2012]采花

    Time Limit: 15 Sec  Memory Limit: 128 MB
    Submit: 2709  Solved: 1386
    [Submit][Status][Discuss]

    Description

    萧芸斓是Z国的公主,平时的一大爱好是采花。
    今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花。花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公主采花。公主每次采花后会统计采到的花的颜色数,颜色数越多她会越高兴!同时,她有一癖好,她不允许最后自己采到的花中,某一颜色的花只有一朵。为此,公主每采一朵花,要么此前已采到此颜色的花,要么有相当正确的直觉告诉她,她必能再次采到此颜色的花。由于时间关系,公主只能走过花园连续的一段进行采花,便让女仆福涵洁安排行程。福涵洁综合各种因素拟定了m个行程,然后一一向你询问公主能采到多少朵花(她知道你是编程高手,定能快速给出答案!),最后会选择令公主最高兴的行程(为了拿到更多奖金!)。

    Input

     第一行四个空格隔开的整数n、c以及m。接下来一行n个空格隔开的整数,每个数在[1, c]间,第i个数表示第i朵花的颜色。接下来m行每行两个空格隔开的整数l和r(l ≤ r),表示女仆安排的行程为公主经过第l到第r朵花进行采花。

    Output

     
    共m行,每行一个整数,第i个数表示公主在女仆的第i个行程中能采到的花的颜色数。

    Sample Input

    5 3 5
    1 2 2 3 1
    1 5
    1 2
    2 2
    2 3
    3 5

    Sample Output

    2
    0 0 1 0
    【样例说明】
    询问[1, 5]:公主采颜色为1和2的花,由于颜色3的花只有一朵,公主不采;询问[1, 2]:颜色1和颜色2的花均只有一朵,公主不采;
    询问[2, 2]:颜色2的花只有一朵,公主不采;
    询问[2, 3]:由于颜色2的花有两朵,公主采颜色2的花;
    询问[3, 5]:颜色1、2、3的花各一朵,公主不采。

    HINT

    【数据范围】

    对于100%的数据,1 ≤ n ≤    10^6,c ≤ n,m ≤10^6。

    #include <bits/stdc++.h>
    #define N 1000005 
    using namespace std;
    int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return f*x;
    }
    int n,m,c;
    int get_id(int x){
    	return x&(-x);
    }
    int d[N];
    void update(int x,int t){
    	for(int i=x;i<=n;i+=get_id(i)){
    		d[i]+=t;
    	}
    }
    int Sum(int x){
    	int sum=0;
    	for(int i=x;i>0;i-=get_id(i)){
    		sum+=d[i];
    	}
    	return sum;
    }
    int a[N];int pre[N],now[N];
    typedef struct node{
    	int l,r,biao;
    	friend bool operator <(node aa,node bb){
    		return aa.r<bb.r;
    	}
    }node;
    node b[N];
    int ans[N];
    int main(){
    	ios::sync_with_stdio(false);
    	n=read();c=read();m=read();
    	memset(pre,0,sizeof(pre));
    	memset(now,0,sizeof(now));
    	for(int i=1;i<=n;i++){
    		a[i]=read();pre[i]=now[a[i]];now[a[i]]=i;}
    	for(int i=1;i<=m;i++){
    		b[i].l=read();b[i].r=read();b[i].biao=i;
    	}
    	sort(b+1,b+m+1);int u=1;
    	for(int i=1;i<=m;i++){
    		while(u<=b[i].r){
    			if(pre[u]) update(pre[u],1);
    			if(pre[u]&&pre[pre[u]]) update(pre[pre[u]],-1);
    			u++;
    		}
    		ans[b[i].biao]=Sum(b[i].r)-Sum(b[i].l-1);
    	}
    	for(int i=1;i<=m;i++) printf("%d
    ",ans[i]);
    	return 0;
    }
    

      

    DQUERY - D-query

    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 5
    
    Output
    3
    2
    3 
    主席树
    #include <bits/stdc++.h>
    #define N 30005
    using namespace std;
    int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return f*x;
    }
    int a[N];
    int rt[N],cnt;
    typedef struct node{
    	int l,r,sum;
    }node;
    node d[N*30];
    void update(int &x,int y,int l,int r,int t,int vul){
    	++cnt;x=cnt;d[x]=d[y];d[x].sum+=vul;
    	if(l==r) return ;
    	int mid=(l+r)>>1;
    	if(t<=mid) update(d[x].l,d[y].l,l,mid,t,vul);
    	else update(d[x].r,d[y].r,mid+1,r,t,vul);
    }
    int ans;
    void querty(int x,int l,int r,int l1,int r1){
    	if(l1<=l&&r<=r1){
    		ans+=d[x].sum;
    		return ;
    	}
    	int mid=(l+r)>>1;
    	if(l1<=mid) querty(d[x].l,l,mid,l1,r1);
    	if(r1>mid) querty(d[x].r,mid+1,r,l1,r1);
    }
    map<int,int>ma;
    int main(){
    	ios::sync_with_stdio(false);
    	int n,m;
    	while(scanf("%d",&n)!=EOF){
    	cnt=0;
    	memset(rt,0,sizeof(rt));
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    	for(int i=1;i<=n;i++){
    		if(ma[a[i]]==0) update(rt[i],rt[i-1],1,n,i,1);
    		else{
    			update(rt[i],rt[i-1],1,n,ma[a[i]],-1);
    			update(rt[i],rt[i],1,n,i,1);
    		}
    		ma[a[i]]=i;
    	}
    	scanf("%d",&m);int l,r;
    	while(m--){
    		scanf("%d%d",&l,&r);
    		ans=0;querty(rt[r],1,n,l,r);
    		printf("%d
    ",ans);
    	}
    	ma.clear();
    }
    	return 0;
    }
    

      树状数组

    #include <bits/stdc++.h>
    #define N 30005
    using namespace std;
    int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return f*x;
    }
    int pre[N],now[N],d[N],a[N];
    typedef struct node{
    	int l,r,biao;
    	friend bool operator <(node aa,node bb){
    		return aa.r<bb.r;
    	}
    }node;int n,m;
    node dd[200005];
    int get_id(int x){
    	return x&(-x);
    }
    void update(int x,int t){
    	for(int i=x;i<=n;i+=get_id(i)){
    		d[i]+=t;
    	}
    }
    int Sum(int x){
    	int sum=0;
    	for(int i=x;i>0;i-=get_id(i)){
    		sum+=d[i];
    	}
    	return sum;
    }
    int ans[200005];
    int main(){
    	ios::sync_with_stdio(false);
    	map<int,int>ma;
    	while(scanf("%d",&n)==1){
    		memset(pre,0,sizeof(pre));
    		memset(d,0,sizeof(d)); 
    		for(int i=1;i<=n;i++){
    			a[i]=read();pre[i]=ma[a[i]];ma[a[i]]=i;
    		}
    		scanf("%d",&m);
    		for(int i=1;i<=m;i++){
    			dd[i].l=read();dd[i].r=read();dd[i].biao=i;
    		}
    		sort(dd+1,dd+m+1);int u=1;
    		for(int i=1;i<=m;i++){
    			while(u<=dd[i].r){
    				update(u,1);
    				if(pre[u]) update(pre[u],-1);
    				u++;
    			}
    		ans[dd[i].biao]=Sum(dd[i].r)-Sum(dd[i].l-1);			
    		}
    		for(int i=1;i<=m;i++) printf("%d
    ",ans[i]);
    		ma.clear();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    MongoDB ODM
    MongoDb python连接
    json格式化
    IDEA使用
    centos7安装完mariadb设置初始密码
    linux虚机联网
    问题解决记录【612-714】
    资料积累
    技术名词理解
    eclipse
  • 原文地址:https://www.cnblogs.com/wang9897/p/8406397.html
Copyright © 2020-2023  润新知