bzoj 3489 A simple rmq problem —— 主席树套线段树
题目: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 ...
代码如下:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define mid ((l+r)>>1)
- using namespace std;
- int const xn=1e5+,xm=xn*,xy=xm*;
- int n,m,cnt,rt[xn],ls[xm],rs[xm],cnt2,rt2[xm],ls2[xy],rs2[xy],lst[xn],mx[xy];
- struct N{int pr,nxt,pos,val;}p[xn];
- int rd()
- {
- int ret=,f=; char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
- while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
- return f?ret:-ret;
- }
- bool cmp(N x,N y){return x.pr<y.pr;}
- void add(int &x,int y,int l,int r,N t)
- {
- x=++cnt2;
- ls2[x]=ls2[y]; rs2[x]=rs2[y];
- mx[x]=max(mx[y],t.val);
- if(l==r)return;
- if(t.pos<=mid)add(ls2[x],ls2[y],l,mid,t);
- else add(rs2[x],rs2[x],mid+,r,t);
- }
- void insert(int &x,int y,int l,int r,N t)
- {
- x=++cnt;
- ls[x]=ls[y]; rs[x]=rs[y];
- add(rt2[x],rt2[y],,n+,t);//!!!
- if(l==r)return;
- if(t.nxt<=mid)insert(ls[x],ls[y],l,mid,t);
- else insert(rs[x],rs[y],mid+,r,t);
- }
- int find(int x,int l,int r,int L,int R)
- {
- if(!x)return ;//!!
- if(l>=L&&r<=R)return mx[x];
- int ret=;
- if(mid>=L)ret=max(ret,find(ls2[x],l,mid,L,R));
- if(mid<R)ret=max(ret,find(rs2[x],mid+,r,L,R));
- return ret;
- }
- int query(int x,int l,int r,int L,int R,int ql,int qr)
- {
- if(!x)return ;//!!
- if(l>=L&&r<=R)return find(rt2[x],,n+,ql,qr);
- int ret=;
- if(mid>=L)ret=max(ret,query(ls[x],l,mid,L,R,ql,qr));
- if(mid<R)ret=max(ret,query(rs[x],mid+,r,L,R,ql,qr));
- return ret;
- }
- int main()
- {
- n=rd(); m=rd();
- for(int i=,x;i<=n;i++)
- {
- x=rd();
- p[i].pos=i; p[i].val=x;
- p[i].pr=lst[x]; p[lst[x]].nxt=i;
- lst[x]=i;
- }
- for(int i=;i<=n;i++)if(!p[i].nxt)p[i].nxt=n+;
- sort(p+,p+n+,cmp);
- for(int i=,t=;i<=n+;i++)
- {
- rt[i]=rt[i-];
- while(p[t].pr==i&&t<=n)insert(rt[i],rt[i],,n+,p[t++]);
- }
- for(int i=,x,y,l,r,lt=;i<=m;i++)
- {
- x=rd(); y=rd();
- l=min((x+lt)%n+,(y+lt)%n+);
- r=max((x+lt)%n+,(y+lt)%n+);
- lt=query(rt[l-],,n+,r+,n+,l,r);//
- printf("%d\n",lt);
- }
- return ;
- }
bzoj 3489 A simple rmq problem —— 主席树套线段树的更多相关文章
- bzoj 3489 A simple rmq problem——主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
- BZOJ.3489.A simple rmq problem(主席树 Heap)
题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...
- 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][ ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- [BZOJ 3489] A simple rmq problem 【可持久化树套树】
题目链接:BZOJ - 3489 题目分析 “因为是OJ上的题,就简单点好了.”——出题人 真的..好..简单... 首先,我们求出每个数的前一个与它相同的数的位置,即 prev[i] ,如果前面没有 ...
- BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...
- BZOJ 3489: A simple rmq problem (KD-tree做法)
KD树水过这道可持久化树套树-其实就是个三维偏序 题解戳这里 CODE #include <bits/stdc++.h> using namespace std; #define ls ( ...
- BZOJ 3489 A simple rmq problem(可持久化线段树)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...
随机推荐
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便
A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【数学】codeforces A. Success Rate
http://codeforces.com/contest/773/problem/A [思路] 用 (x+a)/(y+b) = p/q 来表示其核心思想,其中a 为做对的题目,b为做的题目,则有x+ ...
- Codevs 1688 求逆序对
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我 ...
- mysql查所有列名
查询该视图 information_schema.columns 该有的都有 desc information_schema.columns; select * from information_ ...
- POJ 2749 2SAT判定+二分
题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...
- 洛谷——P1596 [USACO10OCT]湖计数Lake Counting
P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...
- 240.Search in a 2D Matrix II
/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...
- jsoup 提取 html 中的所有链接、图片和媒体
原文:http://www.open-open.com/code/view/1420729333515 package org.jsoup.examples; import org.jsoup.Jso ...
- ldd
ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 [diego@localhost ~/work/branch_dispatch_201511/rtqa_center/source ...
- Linux经常使用命令(更新中)
文件类: 1.创建目录:mkdir 例:sudo mkdir test 2.创建空文件:touch 例:sudo touch test.txt 3.删除文件:rm 删除文件不须要确认:rm -f 例: ...