区间内有多少个不同的数字(离线树状数组)
来源:互联网 发布:医疗大数据解决方案 编辑:程序博客网 时间:2024/05/16 06:56
知道了区间的两个端点s和e,那么这个区间内有多少个不同的数字
先把询问存一起 按右端点排个序 每次用map看出没出现过 一直保留最后出现的那个 再算出所有查询。。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<map>using namespace std;#define clr(x) memset(x,0,sizeof(x))#define INF 0x1f1f1f1fstruct node { int st,en,id,ans;}q[100010];int cmp(struct node a,struct node b){ return a.en<b.en;}int cmp1 (struct node a,struct node b){ return a.id<b.id;}int cal[100010];int lowbit(int x) {return x&(-x);}int getsum(int x){ int s=0; for(;x>0;x-=lowbit(x)) s+=cal[x]; return s;}void update(int x,int value){ for(;x<=100010;x+=lowbit(x)) cal[x]+=value;}int n,Q;int a[100010];int main(){ map<int,int>mp; int i; while(scanf("%d%d",&n,&Q)!=EOF){ mp.clear(); clr(cal); for(i=1;i<=n;i++){ scanf("%d",&a[i]); } for(i=1;i<=Q;i++){ scanf("%d%d",&q[i].st,&q[i].en); q[i].id=i; } sort(q+1,q+Q+1,cmp); int temp=1; for(i=1;i<=Q;i++){ while(temp<=q[i].en){ if(mp[a[temp]]!=0){ update(mp[a[temp]],-1); } mp[a[temp]]=temp; update(temp,1); temp++; } q[i].ans=getsum(q[i].en)-getsum(q[i].st-1); } sort(q+1,q+1+Q,cmp1); for(i=1;i<=Q;i++){ printf("%d\n",q[i].ans); } } return 0;}
- 区间内有多少个不同的数字(离线树状数组)
- SPOJ3267 D-Query 树状数组离线操作 或 主席树 查询某一区间内有多少不同的数
- hdu 5869 区间内不同的GCD数(离线+树状数组)
- 线段树离线处理(区间内出现k次的数有多少个)Codeforces Round #136 (Div. 2)
- hdu 4358 欧拉树形变线性+树状数组+离散化+离线+区间内出现k次的不同的数有几个+手动扩展栈
- BZOJ 题目1878: [SDOI2009]HH的项链(树状数组离线求区间不同种类数)
- 离线记录+树状数组(hdu 5869 统计任意区间的不同gcd值)
- hdu4777 求区间内与其它数均互质的数的个数(树状数组+离线处理)
- 主席树-查询区间有多少个不同的数
- 【HDU5654 BestCoder Round 77 (div1) D】【前驱位置思想 排序 树状数组】xiaoxin and his watermelon candy 区间内多少个不同连续单升三元
- hdu4777 Rabbit Kingdom 离线树状数组 求询问区间内的区间数
- hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)
- HH的项链(树状数组)区间内不同的数量
- HDU 5869区间CGD不同种类数---树状数组+map统计区间不同种类数(离线)
- SPOJ D-query 区间不同数的个数 [在线主席树 or 离线树状数组]
- SPOJ DQUERY(树状数组离线处理 or 主席树 区间不同数个数)
- 有多少个不同的子串?-- 后缀数组
- Codeforces 703D Mishka and Interesting sum (树状数组求区间内不同的数的异或和)
- uva 755 487--3279(检索+排序)
- GridView消除点击状态,设置点击换背景
- visio studio提高工作效率的十个小技巧
- 一段简单c程序的汇编语言学习(ubuntu+x86)
- android开发环境搭建
- 区间内有多少个不同的数字(离线树状数组)
- hdu4267——树状数组
- Unique Binary Search Trees
- C++指针数组和指向指针的指针
- hdu 3948 The Number of Palindromes
- SSH调用lucene的错误
- android之按钮单击事件及监听器的实现方式
- 《学习Qt之路2》笔记:Qt之文本对话框
- 中断处理学习笔记