题目链接: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. 读取Oracle表结构数据

    <html lang="zh-cn"> <head> <meta name="generator" content="O ...

  2. JTree事件

    package com.wf; import javax.swing.*; import javax.swing.event.TreeSelectionEvent; import javax.swin ...

  3. 最简单的MFC

    #include <SDKDDKVer.h> #include <afxwin.h> #include <afxext.h> #include <iostre ...

  4. 希腊字母、拉丁字母、Markdown、拼写与读音中英对照表

    大写 小写 中文名 英文 大写Markdown 小写Markdown 意义 阿尔法 Alpha A \alpha 角度.系数.角加速度.第一个.电离度.转化率 贝塔/毕塔 Beta B \beta 磁 ...

  5. 必须掌握的Linux命令

    章节简述: 本章节讲述系统内核.Bash解释器的关系与作用,教给读者如何正确的执行Linux命令以及常见排错方法. 经验丰富的运维人员可以恰当的组合命令与参数,使Linux字符命令更加的灵活且相对减少 ...

  6. %type的用法

    //%type //如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量 //锚定到这个列上.这样做有什么好处呢? //比如: //declare v_ename scot ...

  7. css3-文字旋转

    <meta charset="utf-8"/><style> * {margin: 0; padding: 0;} ul { height: 80px; b ...

  8. Openjudge-NOI题库-字符串移位包含问题

    题目描述 Description 对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串. 给定两个字符串s1和s2,要求判定其中一个字符串是否是另一字符串通过若干次 ...

  9. Process Explorer(增强任务管理器) V16.05 免费绿色版

    软件名称: Process Explorer(增强任务管理器)软件语言: 中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.2MB图片预 ...

  10. some idea for my personal page

    firstly, dump the old personal page source from Github to Dropbox.then the idea is: 1: make a fake s ...