You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denoted by two integers $l_i$ and $r_i$. For each query, you have to find any integer that occurs exactly once in the subarray of $a$ from index $l_i$ to index $r_i$ (a subarray is a contiguous subsegment of an array). For example, if a=[1,1,2,3,2,4]a=[1,1,2,3,2,4], then for query (li=2,ri=6)(li=2,ri=6) the subarray we are interested in is [1,2,3,2,4][1,2,3,2,4], and possible answers are 11, 33 and 44; for query (li=1,ri=2)(li=1,ri=2) the subarray we are interested in is [1,1][1,1], and there is no such element that occurs exactly once.

Can you answer all of the queries?

Input

The first line contains one integer $n$ ($1≤n≤5⋅10^5$,$1≤n≤5⋅10^5$).

The second line contains $n$ integers $a_1,a_2,…,a_n$ ($1≤a_i≤5⋅10^5$,$1≤a_i≤5⋅10^5$).

The third line contains one integer $q$ ($1≤q≤5⋅10^5$,$1≤q≤5⋅10^5$).

Then $q$ lines follow, $i$-th line containing two integers $l_i$ and $r_i$ representing $i$-th query ($1≤l_i≤r_i≤n$,$1≤l_i≤r_i≤n$).

Output

Answer the queries as follows:

If there is no integer such that it occurs in the subarray from index $l_i$ to index $r_i$exactly once, print $0$. Otherwise print any such integer.

Example

Input

6
1 1 2 3 2 4
2
2 6
1 2

Output

4
0

感想

  再也不敢熬夜了,通过熬夜换回了半夜两三个小时,后面几天我要用几十个小时去偿还,利息太高了……一道可以算做过的题,把我卡了那么多天……

解题思路

  先扔一个链接(HH的项链)//看不懂当年自己的博客什么鬼……这次借着做这题的机会,把当年的博客改得更好懂一点

  同样的思路,把询问离线,按左端点升序排序。搞一个nxt数组,nxt[i]记录下一个和a[i]相同的数字的位置,没有就赋值为inf(无穷大)。搞一棵线段树(树状数组啥的也行,只要能快速单点更新、查询区间最大值就好),记录区间内(nxt[i]最大)的i,代码里变量名是mxi,开始建树时不急着在树上存值,先把mxi全部赋值成0。

  然后,所有第一次出现的数字a[i]在线段树下标i的位置留下痕迹(用自己的nxt[i]去更新线段树,让线段树存下区间内所有留下痕迹的i中nxt[i]最大的i)。

  每次查询之前,先把询问的左端点以左的那些第一次出现的数字处理一下。如果他们的nxt[i]不是inf,就把他们的nxt[i]拿去在线段树上留下痕迹,然后清空他们自己留下的痕迹,查询时que函数会返回一个区间内nxt[i]最大的那个i,代码里变量名为temp,如果nxt[temp]都没超过询问区间的右端点,那么区间里没有只出现一次的数了,答案为0,否则答案就可以是a[temp],下一个相同的出现在区间外(inf也算区间外)。

  最后,把答案按照输入顺序排个序,输出,没了。

  和HH的项链那系列莫队板子题一样,莫队也可以(这题莫队好像要卡常?),主席树也能在线处理询问(我还不会)。

源代码

  1. #include<stdio.h>
  2. #include<algorithm>
  3.  
  4. const int inf=0x7fffffff;
  5.  
  6. int n,m;
  7. int a[]={},mx=-,nxt[]={};
  8. int p[]={};//类似一个桶,装第i号颜色第一次出现的位置
  9.  
  10. struct Segtree{
  11. int l,r;
  12. int mxi;//区间最大的nxt[i]对应的下标i
  13. }t[];
  14. inline int lson(int x){return x<<;}
  15. inline int rson(int x){return x<<|;}
  16. void maketree(int x,int l,int r)
  17. {
  18. if(l==r)
  19. {
  20. t[x]={l,r,};
  21. return;
  22. }
  23. t[x].l=l;
  24. t[x].r=r;
  25. t[x].mxi=;
  26. int mid=(l+r)>>;
  27. maketree(lson(x),l,mid);
  28. maketree(rson(x),mid+,r);
  29. }
  30. void update(int x,int pos)
  31. {
  32. if(pos>t[x].r||pos<t[x].l) return;
  33. if(t[x].l==t[x].r)
  34. {
  35. t[x].mxi=pos;
  36. return;
  37. }
  38. update(lson(x),pos);
  39. update(rson(x),pos);
  40. if(nxt[t[lson(x)].mxi]>nxt[t[rson(x)].mxi])
  41. t[x].mxi=t[lson(x)].mxi;
  42. else
  43. t[x].mxi=t[rson(x)].mxi;
  44. }
  45. int que(int x,int l,int r)
  46. {
  47. if(l>t[x].r||r<t[x].l) return ;
  48. if(l<=t[x].l&&t[x].r<=r)
  49. return t[x].mxi;
  50. int templ=que(lson(x),l,r),tempr=que(rson(x),l,r);
  51. if(nxt[templ]>nxt[tempr]) return templ;
  52. else return tempr;
  53. }
  54.  
  55. struct Ask{
  56. int l,r,id,ans;
  57. }ask[];
  58. bool cmp1(const Ask & a,const Ask & b)
  59. {
  60. return a.l==b.l?a.r<b.r:a.l<b.l;
  61. }
  62. bool cmp2(const Ask & a,const Ask & b)
  63. {
  64. return a.id<b.id;
  65. }
  66.  
  67. int main()
  68. {
  69. //freopen("test.in","r",stdin);
  70. scanf("%d",&n);
  71. for(int i=;i<=n;i++) scanf("%d",a+i),mx=std::max(a[i],mx);
  72. for(int i=n;i>;i--)//预处理出nxt和p
  73. {
  74. if(p[a[i]]) nxt[i]=p[a[i]];
  75. else nxt[i]=inf;
  76. p[a[i]]=i;
  77. }
  78. maketree(,,n);
  79. for(int i=;i<=mx;i++)
  80. {
  81. if(p[i]) update(,p[i]);//线段树单点更新
  82. }
  83.  
  84. scanf("%d",&m);
  85. for(int i=,l,r;i<=m;i++)
  86. {
  87. scanf("%d%d",&l,&r);
  88. ask[i]={l,r,i,};
  89. }
  90. std::sort(ask+,ask++m,cmp1);
  91.  
  92. for(int i=,ll=;i<=m;i++)
  93. {
  94. while(ll<ask[i].l)
  95. {
  96. if(nxt[ll]!=0x7fffffff)
  97. {
  98. update(,nxt[ll]);
  99. nxt[ll]=;
  100. update(,ll);
  101. }
  102. ll++;
  103. }
  104. int temp=que(,ask[i].l,ask[i].r);//返回nxt最大的那个的下标
  105. if(nxt[temp]<=ask[i].r) ask[i].ans=;
  106. else ask[i].ans=a[temp];
  107. }
  108.  
  109. std::sort(ask+,ask++m,cmp2);
  110. for(int i=;i<=m;i++)
  111. printf("%d\n",ask[i].ans);
  112.  
  113. return ;
  114. }

CodeForces 1000F One Occurrence的更多相关文章

  1. codeforces 1000F One Occurrence(线段树、想法)

    codeforces 1000F One Occurrence 题意 多次询问lr之间只出现过一次的数是多少. 题解 将查询按照左端点排序,对于所有值维护它在当前位置后面第二次出现是什么时候,那么查询 ...

  2. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

  3. 【Codeforces 1000F】One Occurrence

    题意:给一个序列,每次查询某个区间内一个只出现一次的数. 思路:线段树. 首先我们看只出现一次的本质是什么. 如果一个数\(x​\)在\((l,r)​\)中只出现了一次,那么它在其中第一次出现位置为\ ...

  4. F - One Occurrence CodeForces - 1000F (线段树+离线处理)

    You are given an array aa consisting of nn integers, and qq queries to it. ii-th query is denoted by ...

  5. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...

  6. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  7. CodeForces - 427A (警察和罪犯 思维题)

    Police Recruits Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  8. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)

    A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

随机推荐

  1. Rails5 layout 和 template

    layout是布局,比如页面的头(head), 脚(foot), 内容(body) template是布局的一部分的内容   这两货实在太像了,写了这些我也是一脸懵逼. 换个说法,layout和tem ...

  2. spring 嵌套事务问题

    嵌套事物总结 事物成功总结 1.内外都无try Catch的时候,外部异常,全部回滚. 2.内外都无try Catch的时候,内部异常,全部回滚. 3.外部有try Catch时候,内部异常,全部回滚 ...

  3. Android 性能优化(12)网络优化( 8)Monitoring the Battery Level and Charging State

    Monitoring the Battery Level and Charging State PreviousNext This lesson teaches you to Determine th ...

  4. 使用wkwebview后,页面返回不刷新的问题

    onpageshow 事件在用户浏览网页时触发. onpageshow 事件类似于 onload 事件,onload 事件在页面第一次加载时触发, onpageshow 事件在每次加载页面时触发,即 ...

  5. C#基础 out传值

    public void Out(out int a, out int b) {//out相当于return返回值 //可以返回多个值 //拿过来变量名的时候,里面默认为空值 a=1; b=2; } s ...

  6. JSON基础 JS操作JSON总结

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  7. 计算机二级C语言冲刺笔记。

    2018-03-0618:32:26 风萧萧兮易水寒,壮士一去...... 四级依旧没过,计算机二级接踵而至, default语句在switch语句中可以省略,所以B错误:switch语句中并非每个c ...

  8. Python学习日记之读取中文目录

    unicode # -*- coding:utf-8 -*- import os import shutil ins="E:\\学习资料" dir=unicode(ins,'utf ...

  9. 循环插入记录,id每次加1

    sql语句写法: begin for i in 1 .. 100 loop insert into table_name values(....); end loop; commit; end; 例子 ...

  10. socket相关函数

    socket() 我们使用系统调用socket()来获得文件描述符:#include<sys/types.h>#include<sys/socket.h>int socket( ...