http://acm.split.hdu.edu.cn/showproblem.php?pid=5919

题意:
给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组成一个新的序列),输出这里面的第ceil(k/2)个数。

思路:

因为每个区间只需要统计第一个数出现的位置,那么从右往左来建树,如果该数没有出现,那么就将该位置+1,否则要将上一次出现的位置-1后再在该位置+1。

统计不同数的个数很简单,就是线段树查询。

查询出第k小的数也很简单,因为我们是从后往前建树的,那么t[l]这棵树里面的元素就是从l开始的数组,直接线段树查询第k个数即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = *1e5+; int tot,n,m;
int a[maxn],pre[maxn],rot[maxn]; struct node
{
int l,r,num;
}t[maxn*]; int build(int l, int r)
{
int root=++tot;
t[root].num=;
if(l==r) return root;
int mid=(l+r)>>;
t[root].l=build(l,mid);
t[root].r=build(mid+,r);
return root;
} int update(int root, int pos, int d)
{
int now = ++tot;
int tmp = now;
t[tot].num = t[root].num + d;
int l = , r = n;
while(l<r)
{
int mid = (l+r)>>;
if(pos<=mid)
{
t[now].l = ++tot;
t[now].r = t[root].r;
root = t[root].l;
now = tot;
r = mid;
}
else
{
t[now].l = t[root].l;
t[now].r = ++tot;
root = t[root].r;
now = tot;
l = mid + ;
}
t[now].num = t[root].num + d;
}
return tmp;
} int query(int ql ,int qr, int l, int r, int root)
{
if(ql<=l && qr>=r) return t[root].num;
int mid = (l+r)>>;
int ans = ;
if(ql<=mid) ans+=query(ql,qr,l,mid,t[root].l);
if(qr>mid) ans+=query(ql,qr,mid+,r,t[root].r);
return ans;
} int calc(int l,int r,int k,int root)
{
if(l==r) return l;
int mid = (l+r)>>;
if(t[t[root].l].num>=k) return calc(l,mid,k,t[root].l);
else return calc(mid+,r,k-t[t[root].l].num,t[root].r);
} int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
scanf("%d",&T);
while(T--)
{
tot=;
int bef=;
memset(pre,-,sizeof(pre));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
rot[n+] = build(,n);
for(int i=n;i>;i--)
{
if(pre[a[i]] == -) rot[i] = update(rot[i+],i,);
else
{
int tmp = update(rot[i+],pre[a[i]],-);
rot[i] = update(tmp,i,);
}
pre[a[i]] = i ;
}
printf("Case #%d:",++kase);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
int ll = (l + bef)%n + ;
int rr = (r + bef)%n + ;
l = min(ll, rr);
r = max(ll, rr);
int k = query(l,r,,n,rot[l]);
k = ceil(k/2.0);
int ans = calc(,n,k,rot[l]);
bef = ans;
printf(" %d",ans);
}
puts("");
}
return ;
}

HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)的更多相关文章

  1. HDU 5919 Sequence II 主席树

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

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

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

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

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

  4. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

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

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

  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 5919 Sequence II (可持久化线段树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...

  8. HDU5919 Sequence II(主席树)

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

  9. HDU - 5919 Sequence II

    题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...

随机推荐

  1. poj1185 [NOI2001炮兵阵地]

    题目链接 状压DP 本来如果考虑所有情况应该开hh[n][2^10][2^10]表示i行在i-1的状态为j,i-2的状态为k的最大个数 但是由于每行中的人互相限制所以在m=10时只有60种情况 空间就 ...

  2. PHP json_encode函数中需要注意的地方

    在php中使用 json_encode() 内置函数可以使用得php中的数据更好的与其它语言传递与使用. 这个函数的功能是将数组转换成json数据存储格式: 1 <?php 2 $arr=arr ...

  3. ajax处理文件下载

    ajax中处理文件下载,可能大数会遇到我和一样的问题,什么问题呢?就是下载程序执行了,但是浏览器没有任何下载操作,这是为什么呢? 那是因为response原因,一般请求浏览器是会处理服务器输出的res ...

  4. SQL SERVER镜像配置,无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法

    环境:非域环境 因为是自动故障转移,需要加入见证,事务安全模式是,强安全FULL模式 做到最后一步的时候,可能会遇到 执行( ALTER DATABASE [mirrortest] SET WITNE ...

  5. Python+OpenCV图像处理(七)—— 滤波与模糊操作

    过滤是信号和图像处理中基本的任务.其目的是根据应用环境的不同,选择性的提取图像中某些认为是重要的信息.过滤可以移除图像中的噪音.提取感兴趣的可视特征.允许图像重采样等等.频域分析将图像分成从低频到高频 ...

  6. Spring Advisor

    SpringAdvisor 顾问:在通知的基础之上,在细入我们的切面AOP 通知和顾问都是切面的实现方式 通知是顾问的一个属性 顾问会通过我们的设置,将不同的通知,在不同的时间点把切面织入不同的切入点 ...

  7. OLED屏幕那些次像素有趣的排列方式

    http://www.dzsc.com/data/2016-6-2/109856.html 我们今天的重点内容为倒数第二列内容的上半部分,也就是RGB排列和Pentile排列.在介绍OLED屏幕时候我 ...

  8. 【web前端】移动端控制台插件,手机端页面查看相关页面控制台信息

    一般调试手机端页面时,基本是在PC端使用手机模式进行断点或console调试.或查看有用的控制台信息的,但依旧有部分功能无法在PC端调试,经常需要使用alert进行断点,然后在手机端看效果,但是这样并 ...

  9. itchat key

    http://www.php.cn/python-tutorials-394725.html

  10. twiested 及其他轮子

    https://www.lfd.uci.edu/~gohlke/pythonlibs/