SPOJ: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
题意:给定N个数,以及Q个询问,对于每个询问,回答这个区间去重后有多少个数。
思路:询问多,时间紧,用莫队和分块是过不去的。 这里需要用主席树,而且我知道要用主席树后也没有想到这么写。。。写法太神奇了。(我太弱了
这里的主席树记录的是前缀每个位置是否用贡献。对于每个区间[1,i],它建立在[1,i-1]的基础上。对于最后一个数,即第i个数,考虑它的贡献,如果它之前没有出现相同的数,则贡献为1。否则,把pre的贡献减去1,i的贡献加1。
(这路说它非常规,是因为它记录的是位置,而不是位置上的数
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node{
int val,l,r;
node() { val=l=r=; }
node(int L,int R,int V):l(L),r(R),val(V){}
}s[maxn*];
int rt[maxn],cnt;
map<int,int>mp;
void insert(int &now,int pre,int L,int R,int pos,int val)
{
s[now=++cnt]=node(s[pre].l,s[pre].r,s[pre].val+val); //先假设都等
if(L==R) return ;
int Mid=(L+R)>>;
if(pos<=Mid) insert(s[now].l,s[pre].l,L,Mid,pos,val); //这里再把不等的改掉
else insert(s[now].r,s[pre].r,Mid+,R,pos,val);
}
int query(int now,int pos,int L,int R)
{
if(L==R) return s[now].val;
int Mid=(L+R)>>;
if(pos<=Mid) return query(s[now].l,pos,L,Mid)+s[s[now].r].val;
return query(s[now].r,pos,Mid+,R);
}
int main()
{
int N,Q,x,y,i;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%d",&x);
if(!mp[x]) insert(rt[i],rt[i-],,N,i,);
else {
insert(y,rt[i-],,N,mp[x],-);
insert(rt[i],y,,N,i,);
}
mp[x]=i;
}
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&x,&y);
printf("%d\n",query(rt[y],x,,N));
}
return ;
}
SPOJ:D-query(非常规主席树求区间不同数的个数)的更多相关文章
- SPOJ - DQUERY (主席树求区间不同数的个数)
题目链接:https://vjudge.net/problem/SPOJ-DQUERY 题目大意:给定一个含有n个数的序列,有q个询问,每次询问区间[l,r]中不同数的个数. 解题思路:从左向右一个一 ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- SPOJ 3267 求区间不同数的个数
题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ - 3267. D-query 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- 主席树——求区间第k个不同的数字(向右密集hdu5919)
和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- 定时任务crontab如何实现每秒执行?
linux crontab 命令,最小的执行时间是一分钟.如需要在小于一分钟内重复执行,可以有两个方法实现. 方法一:crontab -l内容如下,则每10秒执行一次/home/fdipzone/ph ...
- jquery_ajax 入门实例
序:本文通过几个小样例,简单介绍怎样使用jqueryAjax异步载入. 1. $(selector).load(url,[data],[callback]) :加载远程HTML文件代码并插入DOM中. ...
- 百科知识 .tar.xz文件如何打开
7-ZIP可以打开,右击提取到当前目录即可 发现这个压缩比例还是相当不一般的,都快十倍了.
- 一起talk C栗子吧(第八十四回:C语言实例--使用信号进行进程间通信一)
各位看官们,大家好,上一回中咱们说的是进程间通信的样例.这一回咱们说的样例是:使用信号进行进程间通信.闲话休提,言归正转. 让我们一起talk C栗子吧! 我们在上一回中提到过进程之间通信须要解决的三 ...
- react 获取 input 的值
1.通过 onChange -- e.target.value class App extends Component { state = { username: '张三' }; // 用户名 get ...
- 在命令行下运行Matlab
2014-04-20 22:08:11 在命令行下执行: matlab -help 可以得到帮助文件: Usage: matlab [-h|-help] | [-n | -e] [-arch | v= ...
- Eclipse-----Eclipse断点调试
- ios 常见错误整理 持续更新
本文转载至 http://blog.csdn.net/yesjava/article/details/8086185 1. mutating method sent to immutable obj ...
- # Playables API(翻译)
The Playables API provides a way to create tools, effects or other gameplay mechanisms by organi ...
- Erlang function guards NOTE
Note: I've compared , and ; in guards to the operators andalso and orelse. They're not exactly the s ...