bzoj 1878

这题的做法有点神奇。。

离线处理。按r排序,记录上个相同的数出现的位置,边加入一个数的同时删去上一个相同的数就好了。。用BIY维护和

BIT写起来挺快的。。

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<iostream>
 7 #define inc(i,l,r) for(int i=l;i<=r;i++)
 8 #define dec(i,l,r) for(int i=l;i>=r;i--)
 9 #define link(x) for(edge *j=h[x];j;j=j->next)
10 #define mem(a) memset(a,0,sizeof(a))
11 #define ll long long
12 #define succ(x) (1<<x)
13 #define lowbit(x) (x&(-x))
14 #define NM 1000000+5
15 using namespace std;
16 int read(){
17     int x=0,f=1;char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
19     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
20     return x*f;
21 }
22 int t=1,n,m,tmp[NM],h[NM],next[NM],ans[NM],l[NM],r[NM],_x,a[NM];
23 bool cmp(int x,int y){
24     return r[x]<r[y];
25 }
26 void upd(int x,int t){
27     for(;x<=n;x+=lowbit(x))a[x]+=t;
28 }
29 int sum(int x){
30     int s=0;
31     for(;x;x-=lowbit(x))s+=a[x];
32     return s;
33 }
34 int main(){
35     freopen("data.in","r",stdin);
36     n=read();
37     inc(i,1,n){
38         _x=read();next[i]=h[_x];h[_x]=i;
39     }
40     m=read();
41     inc(i,1,m){
42         l[i]=read();r[i]=read();tmp[i]=i;
43     }
44     sort(tmp+1,tmp+m+1,cmp);
45     inc(i,1,m){
46         for(;t<=r[tmp[i]];t++){
47             if(next[t])upd(next[t],-1);upd(t,1);
48         }
49         ans[tmp[i]]=sum(r[tmp[i]])-sum(l[tmp[i]]-1);
50     }
51     inc(i,1,m)printf("%d\n",ans[i]);
52     return 0;
53 }
View Code

 

转载于:https://www.cnblogs.com/onlyRP/p/5187278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值