Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 466 Accepted Submission(s): 112
Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as
a1,a2,⋯,an
There are m queries.
In the i-th query, you are given two integers li and ri . Consider the subsequence ali,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)ki (in ascending order, i.e., p(i)1<p(i)2<⋯<p(i)ki ).
Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉ for the i-th query.
In the i-th query, you are given two integers li and ri . Consider the subsequence ali,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)ki (in ascending order, i.e., p(i)1<p(i)2<⋯<p(i)ki ).
Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉ for the i-th query.
Input
In the first line of input, there is an integer T (
T≤2
) denoting the number of test cases.
Each test case starts with two integers n ( n≤2×105 ) and m ( m≤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×105 ).
There are two integers li and ri 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) . As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,⋯,ansm . Note that for each test case ans0=0 .
You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i )by the following formula:
Each test case starts with two integers n ( n≤2×105 ) and m ( m≤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×105 ).
There are two integers li and ri 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) . As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,⋯,ansm . Note that for each test case ans0=0 .
You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i )by the following formula:
li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
ri=max{(l‘i+ansi−1) mod n+1,(r‘i+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,⋯,pm ”, where x is the case number (starting from 1) and p1,p2,⋯,pm is the answer.
For each test case, output one line “Case #x: p1,p2,⋯,pm ”, where x is the case number (starting from 1) and p1,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 1Hint
Source
Recommend
题意:给你一个序列,k表示某区间一共有几个不同的数,然后求区间第(k+1)/2次出现的数的位置。
思路:现场赛的时候因为对主席树的结构不清楚,队友说有模板可以求区间不同数的个数,然后就想着套二分结果时间不够没能提交,后来重现的时候试了一发原来mlogn^2也过不了,然后研究了一下主席树的结构就发现了mlogn*2的做法,下面给代码:
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
typedef long long LL;
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 200005
typedef long long LL;
int n, m;
int a[maxn], root[maxn];
int data[maxn * 40], ls[maxn * 40], rs[maxn * 40];
int cnt;
map<int, int> mp;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
int build(int l, int r){
int t = cnt++;
data[t] = 0;
if (l == r)return t;
int mid = (l + r) >> 1;
ls[t] = build(l, mid);
rs[t] = build(mid + 1, r);
return t;
}
int update(int p, int l, int r, int v, int w){
int t = cnt++;
data[t] = data[p] + w;
if (l == r) return t;
int mid = (l + r) >> 1;
if (v <= mid){
rs[t] = rs[p];
ls[t] = update(ls[p], l, mid, v, w);
}
else{
ls[t] = ls[p];
rs[t] = update(rs[p], mid + 1, r, v, w);
}
return t;
}
int query(int t, int l, int r, int v){
if (l == r)return data[t];
int mid = (l + r) >> 1;
if (v > mid)return query(rs[t], mid+1, r, v) + data[ls[t]];
else return query(ls[t], l, mid, v);
}
int dfs(int t, int l, int r,int value){
if (l == r)
return l;
int mid = (l + r) >> 1;
if (data[ls[t]] >= value)
return dfs(ls[t], l, mid, value);
else
return dfs(rs[t], mid + 1, r, value-data[ls[t]]);
}
int main(){
int t;
scanf("%d", &t);
for (int tcase = 1; tcase <= t; tcase++){
scanf("%d%d", &n, &m);
cnt = 1;
mp.clear();
for (int i = 1; i <= n; i++){
a[i] = read();
}
root[n + 1] = build(1, n);
for (int i = n; i; i--){
if (mp[a[i]] == 0)
root[i] = update(root[i + 1], 1, n, i, 1);
else {
int tmp = update(root[i + 1], 1, n, mp[a[i]], -1);
root[i] = update(tmp, 1, n, i, 1);
}
mp[a[i]] = i;
}
int ans = 0;
printf("Case #%d:", tcase);
while(m--){
int l, r;
l = read();
r = read();
int ll = ((l + ans) % n) + 1;
int rr = ((r + ans) % n) + 1;
int l0 = min(ll, rr);
int r0 = max(ll, rr);
int k = query(root[l0], 1, n, r0);
k = (k + 1) >> 1;
ans = dfs(root[l0], 1, n, k);
printf(" %d", ans);
}
printf("\n");
}
}