题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489

题解:http://www.itdaan.com/blog/2017/11/24/9bc46b690756fe252e17fc3ca90aa01.html

在我挣扎一下午时 Narh 早就A了...

于是看看有何不同,发现 add  和 insert 中必须把 ls[x] = ls[y] , rs[x] = rs[y] 写在前面,而不能是修改 rs 则在那里单写一个 ls[x] = ls[y] 什么的,否则过不了样例...

然后 find 和 query 中一定要有一个 if(!x) return 0; ,否则秒 WA ...

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define mid ((l+r)>>1)
  6. using namespace std;
  7. int const xn=1e5+,xm=xn*,xy=xm*;
  8. int n,m,cnt,rt[xn],ls[xm],rs[xm],cnt2,rt2[xm],ls2[xy],rs2[xy],lst[xn],mx[xy];
  9. struct N{int pr,nxt,pos,val;}p[xn];
  10. int rd()
  11. {
  12. int ret=,f=; char ch=getchar();
  13. while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
  14. while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
  15. return f?ret:-ret;
  16. }
  17. bool cmp(N x,N y){return x.pr<y.pr;}
  18. void add(int &x,int y,int l,int r,N t)
  19. {
  20. x=++cnt2;
  21. ls2[x]=ls2[y]; rs2[x]=rs2[y];
  22. mx[x]=max(mx[y],t.val);
  23. if(l==r)return;
  24. if(t.pos<=mid)add(ls2[x],ls2[y],l,mid,t);
  25. else add(rs2[x],rs2[x],mid+,r,t);
  26. }
  27. void insert(int &x,int y,int l,int r,N t)
  28. {
  29. x=++cnt;
  30. ls[x]=ls[y]; rs[x]=rs[y];
  31. add(rt2[x],rt2[y],,n+,t);//!!!
  32. if(l==r)return;
  33. if(t.nxt<=mid)insert(ls[x],ls[y],l,mid,t);
  34. else insert(rs[x],rs[y],mid+,r,t);
  35. }
  36. int find(int x,int l,int r,int L,int R)
  37. {
  38. if(!x)return ;//!!
  39. if(l>=L&&r<=R)return mx[x];
  40. int ret=;
  41. if(mid>=L)ret=max(ret,find(ls2[x],l,mid,L,R));
  42. if(mid<R)ret=max(ret,find(rs2[x],mid+,r,L,R));
  43. return ret;
  44. }
  45. int query(int x,int l,int r,int L,int R,int ql,int qr)
  46. {
  47. if(!x)return ;//!!
  48. if(l>=L&&r<=R)return find(rt2[x],,n+,ql,qr);
  49. int ret=;
  50. if(mid>=L)ret=max(ret,query(ls[x],l,mid,L,R,ql,qr));
  51. if(mid<R)ret=max(ret,query(rs[x],mid+,r,L,R,ql,qr));
  52. return ret;
  53. }
  54. int main()
  55. {
  56. n=rd(); m=rd();
  57. for(int i=,x;i<=n;i++)
  58. {
  59. x=rd();
  60. p[i].pos=i; p[i].val=x;
  61. p[i].pr=lst[x]; p[lst[x]].nxt=i;
  62. lst[x]=i;
  63. }
  64. for(int i=;i<=n;i++)if(!p[i].nxt)p[i].nxt=n+;
  65. sort(p+,p+n+,cmp);
  66.  
  67. for(int i=,t=;i<=n+;i++)
  68. {
  69. rt[i]=rt[i-];
  70. while(p[t].pr==i&&t<=n)insert(rt[i],rt[i],,n+,p[t++]);
  71. }
  72. for(int i=,x,y,l,r,lt=;i<=m;i++)
  73. {
  74. x=rd(); y=rd();
  75. l=min((x+lt)%n+,(y+lt)%n+);
  76. r=max((x+lt)%n+,(y+lt)%n+);
  77. lt=query(rt[l-],,n+,r+,n+,l,r);//
  78. printf("%d\n",lt);
  79. }
  80. return ;
  81. }

bzoj 3489 A simple rmq problem —— 主席树套线段树的更多相关文章

  1. bzoj 3489 A simple rmq problem——主席树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...

  2. BZOJ.3489.A simple rmq problem(主席树 Heap)

    题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...

  3. bzoj 3489: A simple rmq problem k-d树思想大暴力

    3489: A simple rmq problem Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 551  Solved: 170[Submit][ ...

  4. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  5. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  6. [BZOJ 3489] A simple rmq problem 【可持久化树套树】

    题目链接:BZOJ - 3489 题目分析 “因为是OJ上的题,就简单点好了.”——出题人 真的..好..简单... 首先,我们求出每个数的前一个与它相同的数的位置,即 prev[i] ,如果前面没有 ...

  7. BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...

  8. BZOJ 3489: A simple rmq problem (KD-tree做法)

    KD树水过这道可持久化树套树-其实就是个三维偏序 题解戳这里 CODE #include <bits/stdc++.h> using namespace std; #define ls ( ...

  9. BZOJ 3489 A simple rmq problem(可持久化线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...

随机推荐

  1. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便

    A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 【数学】codeforces A. Success Rate

    http://codeforces.com/contest/773/problem/A [思路] 用 (x+a)/(y+b) = p/q 来表示其核心思想,其中a 为做对的题目,b为做的题目,则有x+ ...

  3. Codevs 1688 求逆序对

     时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我 ...

  4. mysql查所有列名

    查询该视图  information_schema.columns  该有的都有 desc information_schema.columns; select * from information_ ...

  5. POJ 2749 2SAT判定+二分

    题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...

  6. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  7. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  8. jsoup 提取 html 中的所有链接、图片和媒体

    原文:http://www.open-open.com/code/view/1420729333515 package org.jsoup.examples; import org.jsoup.Jso ...

  9. ldd

    ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 [diego@localhost ~/work/branch_dispatch_201511/rtqa_center/source ...

  10. Linux经常使用命令(更新中)

    文件类: 1.创建目录:mkdir 例:sudo mkdir test 2.创建空文件:touch 例:sudo touch test.txt 3.删除文件:rm 删除文件不须要确认:rm -f 例: ...