题目链接:hdu_5919_Sequence II

题意:

给你n个数,m个询问,每次问你一个区间中每一种数在区间中第一次出现的位置的中位数,强制在线。

题解:

一看就是主席树搞,不过这里要询问第一次出现的位置,有个技巧就是倒着将数插进去,如果有相同的数,就把位置靠后的数的贡献取消掉,这样查询的时候就只需要查询root[l]了,因为root[l]包括了l到n的信息,前面已经只保留了相同数的最前面的位置,所以查询一共有多少种数时直接查询root[l]的l到r就行了。

 #include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int N=2e5+;
struct node{int l,r,sum;}T[*N]; int t,n,m,a[N],pre[N],root[N],tot,ic=; void update(int &x,int y,int pos,int val,int l=,int r=n)
{
T[++tot]=x?T[x]:T[y],T[tot].sum+=val,x=tot;
if(l==r)return;
int m=l+r>>;
if(pos<=m)update(T[x].l,T[y].l,pos,val,l,m);
else update(T[x].r,T[y].r,pos,val,m+,r);
} int query(int x,int k,int l=,int r=n)
{
if(l==r)return l;
int m=l+r>>;
if(k<=T[T[x].l].sum)return query(T[x].l,k,l,m);
return query(T[x].r,k-T[T[x].l].sum,m+,r);
} int getsum(int x,int L,int R,int l=,int r=n)
{
if(L<=l&&r<=R)return T[x].sum;
int m=l+r>>,ans=;
if(L<=m)ans+=getsum(T[x].l,L,R,l,m);
if(R>m)ans+=getsum(T[x].r,L,R,m+,r);
return ans;
} int main()
{
scanf("%d",&t);
while(t--)
{
printf("Case #%d: ",ic++);
scanf("%d%d",&n,&m);
tot=;
F(i,,n)scanf("%d",a+i);
for(int i=n;i>;i--)
{
if(pre[a[i]])update(root[i],root[i+],pre[a[i]],-);
update(root[i],root[i+],i,);
pre[a[i]]=i;
}
int ans=,l,r,sum;
F(i,,m)
{
scanf("%d%d",&l,&r);
l=(l+ans)%n+,r=(r+ans)%n+;
if(l>r)l^=r,r^=l,l^=r;
sum=getsum(root[l],l,r);
printf("%d%c",ans=query(root[l],sum+>>)," \n"[i==m]);
}
while(tot)T[tot].l=T[tot].r=T[tot].sum=,tot--;
F(i,,n)root[i]=,pre[a[i]]=;
}
return ;
}

hdu_5919_Sequence II(主席树)的更多相关文章

  1. HDU 5919 Sequence II 主席树

    Sequence II Problem Description   Mr. Frog has an integer sequence of length n, which can be denoted ...

  2. HDU5919 Sequence II(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  3. HDU 5919 -- Sequence II (主席树)

    题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...

  4. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

  5. Sequence II HDU - 5919(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  6. HDU 5919 Sequence II(主席树)题解

    题意:有A1 ~ An组成的数组,给你l r,L = min((l + ans[i - 1]) % n + 1, (r + ans[i - 1]) % n + 1),R = max((l + ans[ ...

  7. HDU--5519 Sequence II (主席树)

    题目链接 2016年长春ccpc I 题 题目大意 : 给你n(n≤2∗105n≤2∗105)个数,每个数的大小 0<Ai≤2∗10^5   0<Ai≤2∗10^5. 再给你m(m≤2∗1 ...

  8. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  9. SPOJ3267 D-query 离线+树状数组 在线主席树

    分析:这个题,离线的话就是水题,如果强制在线,其实和离线一个思路,然后硬上主席树就行了 离线的代码 #include <iostream> #include <stdio.h> ...

随机推荐

  1. asp.net小技巧:保留password模式文本框textbox内的数据不丢失。

    在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号.圆点),就没有了! 一个可行的做法是 : prot ...

  2. android ViewStub延时渲染的应用

    android开发当中,我们经常会遇到根据某个条件去控制一个控件的显示/隐藏的情况.虽然setVisibility(int visibility)的确可以达到这样的目的,但是在渲染时,其实隐藏的布局也 ...

  3. install plugin elasticsearch-analysis-ik

    1.github下载分词器插件(请各位下载自己elasticsearch相对应的版本,否则会有兼容性问题) https://github.com/medcl/elasticsearch-analysi ...

  4. jquery ColorPicker 颜色选择器

    $(function() { $('#colorpickerField').ColorPicker({ onSubmit: function(hsb, hex, rgb, el) { $(el).va ...

  5. 让innerHTML获取的内容包含input和select(option)的最新值

    function refreshData(){ var allInputObject=document.body.getElementsByTagName("input"); fo ...

  6. Fedora24 升级到25

    1 安装dnf-plugin-system-upgrade dnf install dnf-plugin-system-upgrade 2 更新软件包 dnf system-upgrade downl ...

  7. html5 拖拽文件到页面实现上传

    思路:监听拖拽区域的 drop 事件,阻止浏览器上的默认拖拽事件 参考:http://www.helloweba.com/view-blog-192.html 例子: <!DOCTYPE htm ...

  8. SQL如何实现远程数据库链接

    利用sp_addlinkeserver 进行远程数据库连接可以把多个数据库的数据,放置到一个数据库中, 或者有设置操作权限的情况下,我们可以通过这种方式进行查询,备份数据等操作. 首先,我们要创建连接 ...

  9. DFS - leetcode [深度优先遍历]

    最短路径=>BFS    所有路径=>DFS 126. Word Ladder II BFS+DFS: BFS找出下一个有效的word进队 并记录step 更新两个变量:unordered ...

  10. 使用inno setup制作安装包