题目链接
经典套路
记第
i
i
个数前一个和他一样的数的位置为,后一个和他一样的数为
suf[i]
s
u
f
[
i
]
然后只有当
pre[i]+1≤l≤i and i≤r≤suf[i]−1
p
r
e
[
i
]
+
1
≤
l
≤
i
a
n
d
i
≤
r
≤
s
u
f
[
i
]
−
1
时,
i
i
才会对答案贡献
把上述不等式看成两维的,把询问 (l,r) ( l , r ) 看成点
然后就相当于预处理覆盖 n n <script type="math/tex" id="MathJax-Element-555">n</script>个矩形,查询点的最大值
二维线段树瞎搞搞
代码
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int INF = 2147483647;
const int maxn = 100010;
const int segn = 10 * 20 * maxn;
int n,m,ans,a[maxn],now[maxn],pre[maxn],suf[maxn];
int Rt,tot,rc[segn],lc[segn],maxx[segn],rt[segn];
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
inline int max(int &a,int &b)
{
return a > b ? a : b;
}
inline void modify(int &o,int l,int r,int al,int ar,int x)
{
if (!o) o = ++tot;
if (al <= l && r <= ar) {maxx[o] = max(maxx[o],x); return;}
int mid = l + r >> 1;
if (al <= mid) modify(lc[o],l,mid,al,ar,x);
if (mid < ar) modify(rc[o],mid + 1,r,al,ar,x);
}
inline void Modify(int &o,int l,int r,int xl,int xr,int yl,int yr,int x)
{
if (!o) o = ++tot;
if (xl <= l && r <= xr) {modify(rt[o],1,n,yl,yr,x); return;}
int mid = l + r >> 1;
if (xl <= mid) Modify(lc[o],l,mid,xl,xr,yl,yr,x);
if (mid < xr) Modify(rc[o],mid + 1,r,xl,xr,yl,yr,x);
}
inline int query(int o,int l,int r,int pos)
{
if (l == r) return maxx[o];
int mid = l + r >> 1;
if (pos <= mid) return max(maxx[o],query(lc[o],l,mid,pos));
else return max(maxx[o],query(rc[o],mid + 1,r,pos));
}
inline int Query(int o,int l,int r,int x,int y)
{
if (l == r) return query(rt[o],1,n,y);
int mid = l + r >> 1;
if (x <= mid) return max(query(rt[o],1,n,y),Query(lc[o],l,mid,x,y));
else return max(query(rt[o],1,n,y),Query(rc[o],mid + 1,r,x,y));
}
int main()
{
#ifdef AMC
freopen("AMC1.txt","r",stdin);
freopen("AMC2.txt","w",stdout);
#endif
n = getint(); m = getint();
for (int i = 1; i <= n; i++)
a[i] = getint();
for (int i = 1; i <= n; i++)
{
pre[i] = now[a[i]];
now[a[i]] = i;
}
for (int i = 1; i <= n; i++)
now[a[i]] = n + 1;
for (int i = n; i >= 1; i--)
{
suf[i] = now[a[i]];
now[a[i]] = i;
}
for (int i = 1; i <= n; i++)
{
// printf("%d %d %d %d\n",pre[i] + 1,i,i,suf[i] - 1);
Modify(Rt,1,n,pre[i] + 1,i,i,suf[i] - 1,a[i]);
}
for (int i = 1; i <= m; i++)
{
int x = getint(),y = getint();
int l = min((x + ans) % n + 1,(y + ans) % n + 1);
int r = max((x + ans) % n + 1,(y + ans) % n + 1);
// printf("%d %d\n",l,r);
printf("%d\n",ans = Query(Rt,1,n,l,r));
}
return 0;
}