HDU5919-Sequence II

Sequence II

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


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.
 

Input
In the first line of input, there is an integer T ( T2 ) denoting the number of test cases.

Each test case starts with two integers n ( n2×105 ) and m ( m2×105 ). There are n integers in the next line, which indicate the integers in the sequence(i.e.,  a1,a2,,an,0ai2×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  li,ri(1lin,1rin) . 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  li,ri )by the following formula:
li=min{(li+ansi1) mod n+1,(ri+ansi1) mod n+1}

ri=max{(li+ansi1) mod n+1,(ri+ansi1) 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.
 

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
 

Source
 


题意:给你n个数,有m次询问。对于每个询问输入l,r,表示 Al...Ar 这个区间我们得到每个数第一次出现的位置下标的排列,假设这个区间有k个不同的数,我们得到的排列是 p1<p2<p3<...<pk,求第(k+1)/2个数

解题思路:本题是强制在线,可以用主席树求出区间不同数的个数,可以倒着插入主席树,再寻找位置的中位数上就可以了


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <cmath>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;

int n, m, q, ans[200009];
int a[200009], visit[200009];
int sum[200009 * 40], s[200009], L[200009 * 40], R[200009 * 40], tot;

void build(int pre,int &now, int l, int r, int k, int val)
{
	sum[now=++tot] = sum[pre] + val;
	if (!sum[now]) { now = 0; return; }
	if (l == r) { L[now] = 0, R[now] = 0; return; }
	int mid = (l + r) >> 1;
	L[now] = L[pre], R[now] = R[pre];
	if (k <= mid) build(L[pre],L[now], l, mid, k, val);
	else build(R[pre],R[now], mid + 1, r, k, val);
}

int query(int k, int l, int r, int ll, int rr)
{
	if (l >= ll&&r <= rr) return sum[k];
	int mid = (l + r) >> 1;
	int cnt = 0;
	if (mid >= ll) cnt += query(L[k], l, mid, ll, rr);
	if (rr>mid) cnt += query(R[k], mid + 1, r, ll, rr);
	return cnt;
}

int get(int k, int l, int r, int ll, int rr, int p)
{
	if (l == r) return l;
	int mid = (l + r) >> 1;
	if (p>sum[L[k]]) return get(R[k], mid + 1, r, ll, rr, p - sum[L[k]]);
	else return get(L[k], l, mid, ll, rr, p);
}

int main()
{
	int t, cas = 0;
	scanf("%d", &t);
	while (t--)
	{
		printf("Case #%d:", ++cas);
		scanf("%d%d", &n, &q);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		memset(visit, 0, sizeof visit);
		L[n+1]=R[n+1]=ans[0]=sum[0]=s[n+1]=tot=0;
		for (int i = n; i >= 1; i--)
		{
			if (visit[a[i]])
			{
				build(s[i + 1], s[i], 1, n, visit[a[i]], -1);
				build(s[i], s[i], 1, n, i, 1);
			}
			else build(s[i + 1], s[i], 1, n, i, 1);
			visit[a[i]] = i;
		}
		for (int i = 1; i <= q; i++)
		{
			int l, r;
			scanf("%d%d", &l, &r);
			l = ((l + ans[i - 1]) % n) + 1;
			r = ((r + ans[i - 1]) % n) + 1;
			if (l>r) swap(l, r);
			int cnt = (query(s[l], 1, n, l, r)+1) / 2;
			ans[i] = get(s[l], 1, n, l, r, cnt);
			printf(" %d", ans[i]);
		}
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值