[原创]HDU 5919 Sequence II [主席树]【数据结构】

2017-03-13 18:16:52 Tabris_ 阅读数:402


博客爬取于2020-06-14 22:41:16
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/61923707


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919
——————————————————————————————————————
Sequence II

Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1654 Accepted Submission(s): 420

Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted asa1,a2,,ana_1,a_2,⋯,a_n There are m queries.

In theithi-th query, you are given two integerslil_i andrir_i. Consider the subsequenceali,ali+1,ali+2,,aria_{l_{i} },a_{l_{i+1} },a_{l_{i+2} },⋯,a_{r_{i} }.

We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence asp(i)1,p(i)2,,p(i)kip(i)1,p(i)2,⋯,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<<p(i)ki)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(T2)(T≤2) denoting the number of test cases.

Each test case starts with two integers n (n≤2×10^5) and m (m≤2×10^5). There are n integers in the next line, which indicate the integers in the sequence(i.e.,a1,a2,,an,0ai2×105a_1,a_2,⋯,a_n,0≤a_i≤2×10^5).

There are two integerslil_i andrir_i in the followingmm 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 asans1,ans2,,ansmans_1,ans_2,⋯,ans_m. Note that for each test caseans0=0ans_0=0.

You can get the correct input li,ri from what you read (we denote them asli,ril‘_i,r‘_i)by the following formula:
li=min(li+ansi1)modn+1,(ri+ansi1)modn+1li=min{(l‘_i+ans_{i−1}) \mod n+1,(r‘_i+ans_{i−1}) \mod n+1}

ri=max(li+ansi1)modn+1,(ri+ansi1)modn+1ri=max{(l‘_i+ans_{i−1})\mod n+1,(r‘_i+ans_{i−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,,pmp_1,p_2,⋯,p_m”, wherexx is the case number (starting from 1) andp1,p2,,pmp_1,p_2,⋯,p_m 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 1

Hint

————————————————————————————————————————————

题目大意:
给定一个长度为n的序列,然后有m个查询,问你在[l,r][l,r]区间中所有不相同元素第一次出现的位置,按这个位置升序以后的中间(向上取整)的那个位置是多少(这个位置指的是原序列)?

解题思路:
因为这个题对l,rl,r的限制,这题强制在线,就不能离线树状数组做了,只能主席树做了,

然后他说在询问区间,不相同元素第一次出现的位置的中间那个,
如果是从正序挂到主席树上的话 确实不好维护,
考虑倒叙插入到主席树上,那么每次都只会记录最左边的数,更新的时候如过当前数出现过就删去之前的那个,
就不需要排序过程了,只需要在区间内找第中间的那个就行了
查询的时候我们只要对rt[l]进行查找就行了
于是就变成了找区间内不同数的个数,
然后找区间内第k大的问题,

这样的话就是主席树的基本操作了,

总体复杂度O(nlogn)O(n\log n)

附本题代码
——————————————————————————————————————————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# include <bits/stdc++.h>

using namespace std;

/**********************************/

const int N = 2e5+7;

int n,m,ans;
int a[N],vis[N];

void so(int &l,int &r){
int tem=(l+ans)%n+1;
int tmp=(r+ans)%n+1;
l=(tem<tmp)?tem:tmp;
r=(tem>tmp)?tem:tmp;
}

int rt[N],ls[N*40],rs[N*40],sum[N*40],tot;
void build(int& rt,int l,int r){
rt=++tot;
sum[rt]=0;
if(l>=r)return;
int m=((r-l)>>1)+l;
build(ls[rt],l ,m);
build(rs[rt],m+1,r);
}

void update(int& rt,int l,int r,int last,int pos,int v){
rt=++tot;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+v;
if(l>=r)return;
int m=((r-l)>>1)+l;
if(pos<=m) update(ls[rt],l ,m,ls[last],pos,v);
else update(rs[rt],m+1,r,rs[last],pos,v);
}

int query_num(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R) return sum[rt];
int m=((r-l)>>1)+l;
int ans=0;
if(L<=m) ans+=query_num(ls[rt],l ,m,L,R);
if(R> m) ans+=query_num(rs[rt],m+1,r,L,R);
return ans;
}

int query_id(int rt,int l,int r,int k){
if(l>=r)return l;
int m=((r-l)>>1)+l;
int cnt = sum[ls[rt]];
if(k<=cnt) return query_id(ls[rt],l ,m,k);
else return query_id(rs[rt],m+1,r,k-cnt);
}

int main(){
int _=1,kcase=0;
scanf("%d",&_);
while(_--){
memset(vis,0,sizeof(vis));

ans = 0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);

tot = 0;
build(rt[n+1],1,n);

for(int i=n,tem;i;i--){
tem=rt[i+1];
if(vis[a[i]])update(tem,1,n,rt[i+1],vis[a[i]],-1);
update(rt[i],1,n,tem,i,1);
vis[a[i]]=i;
}
printf("Case #%d:",++kcase);
for(int i=1,l,r,id;i<=m;i++){
scanf("%d%d",&l,&r);
so(l,r);
// printf("[%d,%d]",l,r);
id = query_num(rt[l],1,n,l,r);
// printf(" %d<=",id);
id =(id+1)>>1;
ans=query_id(rt[l],1,n,id);
printf(" %d",ans);
}
puts("");
}
return 0;
}