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

In the i-th query, you are given two integers lili and riri. Consider the subsequence ali,ali+1,ali+2,⋯,ariali,ali+1,ali+2,⋯,ari.

We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,⋯,p(i)kip1(i),p2(i),⋯,pki(i) (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)kip1(i)<p2(i)<⋯<pki(i)).

Note that kiki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉p⌈ki2⌉(i)for the i-th query.
Input
In the first line of input, there is an integer T (T≤2T≤2) denoting the number of test cases.

Each test case starts with two integers n (n≤2×105n≤2×105) and m (m≤2×105m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,⋯,an,0≤ai≤2×105a1,a2,⋯,an,0≤ai≤2×105).

There are two integers lili and riri in the following m lines.

However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n)li‘,ri‘(1≤li‘≤n,1≤ri‘≤n). As a result, the problem became more exciting.

We can denote the answers as ans1,ans2,⋯,ansmans1,ans2,⋯,ansm. Note that for each test case ans0=0ans0=0.

You can get the correct input li,rili,ri from what you read (we denote them as l‘i,r‘ili‘,ri‘)by the following formula:
li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
li=min{(li‘+ansi−1) mod n+1,(ri‘+ansi−1) mod n+1}

ri=max{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
ri=max{(li‘+ansi−1) mod n+1,(ri‘+ansi−1) mod n+1}
Output
You should output one single line for each test case.

For each test case, output one line “Case #x: p1,p2,⋯,pmp1,p2,⋯,pm”, where x is the case number (starting from 1) and p1,p2,⋯,pmp1,p2,⋯,pm is the answer.
Sample Input
2
5 2
3 3 1 5 4
2 2
4 4
5 2
2 5 2 1 2
2 3
2 4
Sample Output
Case #1: 3 3
Case #2: 3 1

题意:长度为n的序列和m次询问,每次询问是l到r之间按每个数第一次出现的位置排序,问中位数的位置是什么。
思路:因为我们要记录每个数第一次出现的位置。之前bzoj有一个类似的题,bzoj题目的网址
但是这个题需要记录第一次出现的位置,所以我们要从后往前遍历数组,如果之前出现过,就将之前的位置清空,在当前位置记录下来。这个操作在主席树里操作。对于l~r,我们可以算出来这段区间里有多少个数,然后就在转化成了求区间l ~r内第k个数的位置。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=2e5+100;
struct node{int l;int r;int sum;
}p[maxx*40];
int a[maxx];
int n,m,tot,root[maxx];inline int build(int l,int r)
{int cur=++tot;p[cur].sum=0;if(l==r) return cur;int mid=l+r>>1;p[cur].l=build(l,mid);p[cur].r=build(mid+1,r);return cur;
}
inline int update(int rot,int l,int r,int pos,int v)
{int cur=++tot;p[cur]=p[rot];p[cur].sum+=v;if(l==r) return cur;int mid=l+r>>1;if(pos<=mid) p[cur].l=update(p[rot].l,l,mid,pos,v);else p[cur].r=update(p[rot].r,mid+1,r,pos,v);return cur;
}
inline int queryI(int rot,int l,int r,int L,int R)//求区间l~r的数字种类数
{if(l<=L&&R<=r) return p[rot].sum;int mid=L+R>>1;int sum=0;if(l<=mid) sum+=queryI(p[rot].l,l,r,L,mid);if(mid<r) sum+=queryI(p[rot].r,l,r,mid+1,R);return sum;
}
inline int queryII(int rot,int L,int R,int num)//求第num个数
{if(L==R) return L;int ret=p[p[rot].l].sum;int mid=L+R>>1;if(num<=ret) return queryII(p[rot].l,L,mid,num);else return queryII(p[rot].r,mid+1,R,num-ret);
}
int main()
{int t,k=0,l,r;scanf("%d",&t);while(t--){tot=0;scanf("%d%d",&n,&m);map<int,int> mp;for(int i=1;i<=n;i++) scanf("%d",&a[i]);root[n+1]=build(1,n);for(int i=n;i>=1;i--){if(mp.find(a[i])==mp.end()) root[i]=update(root[i+1],1,n,i,1);else{int x=update(root[i+1],1,n,mp[a[i]],-1);//x是将之前标记消了的版本,第i个版本在x的基础上建立起来root[i]=update(x,1,n,i,1);}mp[a[i]]=i;}printf("Case #%d:",++k);int ans=0;while(m--){scanf("%d%d",&l,&r);l=(l+ans)%n+1;r=(r+ans)%n+1;if(l>r) swap(l,r);int kk=(queryI(root[l],l,r,1,n)+1)>>1;ans=queryII(root[l],1,n,kk);printf(" %d",ans);}printf("\n");}return 0;
}

努力加油a啊,(o)/~

Sequence II HDU - 5919(主席树)相关推荐

  1. Sequence II (HDU 5919)(主席树)

    Sequence II 题目大意是有mmm次询问,每次询问一段区间[l,r][l, r][l,r],从左到右,如果这个数是在这个区间第一次出现,则记录下其下标, 我们会得到一个新的数组,要求这个数组的 ...

  2. 2016中国大学生程序设计竞赛(长春)Sequence II HDU - 5919 主席树

    传送门 文章目录 题意: 思路: 题意: 给一个长度为nnn的序列,每次一个询问[l,r][l,r][l,r],求其中数第一次出现的位置的中位数. 思路: 先考虑一下如何求区间内不同数的个数. 因为要 ...

  3. Sequence II(HDU - 5919)

    题目 Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,an.There are m quer ...

  4. HDU 5919 Sequence II 主席树

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5919 Sequence II Time Limit: 9000/4500 MS (Java/Othe ...

  5. HDU - 5919 Sequence II——主席树+区间种类++逆序建树

    [题目描述] HDU - 5919 Sequence II [题目分析] 题目给定一个数组,每次查询一个区间,找出区间内不同数字的个数x,然后输出按出现顺序第x/2向上取整个数字的位置. 按照要求,我 ...

  6. HDU - 5919 Sequence II(主席树+思维)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a ,再给出 m 次询问,每次询问给出一个区间 [ l , r ] ,问区间 [ l , r ] 内首次出现的数字的位置的中位数 题目分析:题 ...

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

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

  8. 2016CCPC长春:Sequence II(主席树)

    问题概述:n个数q次查询,每次查询(l,r)表示查询区间[l,r]内所有不同数字第一次出现的下标,输出这些下标中位数 特殊:如果下标数sum为偶数个,则输出第sum/2个下标,且强制在线,每一组的l和 ...

  9. HDU - 5919 Sequence II

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

最新文章

  1. unity调用普通java类_Unity中C#和Java的相互调用实例代码
  2. 产品经理必备知识之网页设计系列(三)-移动端适配无障碍设计及测试
  3. SVN的安装和启动SVN的安装
  4. mybatis学习(54):鉴定器
  5. OutOfMemoryError(内存溢出)解决办法
  6. OD使用教程21(上) - 调试篇21
  7. React+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
  8. 三包围结构的字是什么样的_清桦学书之结构篇——包围结构。
  9. 大漠软件c语言教程,大漠万能脚本编辑器无需写代码,截图可以制作脚本附视频教程...
  10. matlab和cuda版本对应适配关系
  11. 企业之家完成华为鲲鹏云服务兼容性认证
  12. 缅怀2022,展望2023
  13. 微软再次荣获 Gartner 工业物联网平台魔力象限“领先者”称号
  14. python 画ks曲线_Ks密度曲线分布图绘图
  15. C语言 六大门派身份识别
  16. 指针数组,数组指针,函数指针的区别
  17. 转载:网站分析与SEO效果的评估
  18. java version 1.8下载_java jdk v1.8.0 官方免费版
  19. ui平面设计好学吗?ui设计哪些工具是需要掌握的?
  20. 【模板题】欧拉函数与线性筛求欧拉函数

热门文章

  1. Xcode12.5最新快捷键的使用(学会事半功倍)
  2. CNN卷积神经网络:权值更新公式推导
  3. STM32F4+Wi-Fi+EDP 向 OneNet 上传数据
  4. 3、AD使用技巧分享
  5. python爬取国内代理ip_Python语言爬取代理IP
  6. python neo4j嵌入_Neo4j推出基于Python的嵌入式图数据存储
  7. java mvc模式_Java MVC模式
  8. mysql 查看锁_MySQL反应慢的排查思路
  9. 图片插值数据_结合PS用这招来增强ArcGIS插值图出图效果
  10. 想问一下C++里queue要怎么遍历