hdu5919 Sequence II

本文介绍了一种使用主席树解决区间查询问题的方法。通过构建主席树来高效地处理区间内的元素首次出现位置的查询,特别是在区间经过修改后,能够快速更新并查询中间值的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/archives/1706
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 (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:

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.

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

2016中国大学生程序设计竞赛(长春)-重现赛

Recommend

wange2014 | We have carefully selected several similar problems for you: 6242 6241 6240 6239 6238

题目要求 每次询问一个区间 要求询问
把这个区间第一次出现的数都 编号 从1到n那么
针对每个询问我需要求出(k/2)上取整
那么我从后向前走 每次针对每个数都建一棵线段树 空间开不下怎么办 用主席树 每次连接到前面的节点 然后查询的时候查询l到r区间 我要针对root[l]这个线段树去查询区间内我要询问的那个数出现的位置在哪里即可


#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 220000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S) {T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0;char ch=gc();
    while(ch<'0'||ch>'9') ch=gc();
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x;
}
struct node{
    int left,right,v;
}tree[N*40];int num;
void insert1(int &x,int l,int r,int pos,int v){
    tree[++num]=tree[x];x=num;tree[x].v+=v;;
    if (l==r) return;int mid=l+r>>1;
    if (pos<=mid) insert1(tree[x].left,l,mid,pos,v);else insert1(tree[x].right,mid+1,r,pos,v);
}
int query(int x,int l,int r,int k){
    if (l==r) return l;
    int mid=l+r>>1;
    if (k<=tree[tree[x].left].v) return query(tree[x].left,l,mid,k);else return query(tree[x].right,mid+1,r,k-tree[tree[x].left].v); 
}
int T,root[N],pos[N],n,m,a[N],ans;
int gs(int x,int l,int r,int l1,int r1){
    if(l1<=l&&r1>=r) return tree[x].v;
    int mid=l+r>>1;int tmp=0;
    if (l1<=mid) tmp+=gs(tree[x].left,l,mid,l1,r1);
    if (r1>mid) tmp+=gs(tree[x].right,mid+1,r,l1,r1);return tmp;
}
int main(){
    freopen("hdu5919.in","r",stdin);
    T=read();
    for (int ii=1;ii<=T;++ii){
        ans=0;n=read();m=read();printf("Case #%d:",ii);num=0;
        for (int i=1;i<=n;++i) a[i]=read();memset(pos,0,sizeof(pos));memset(root,0,sizeof(root));
        for (int i=n;i>=1;--i){
            root[i]=root[i+1];
            if (pos[a[i]]) insert1(root[i],1,n,pos[a[i]],-1);
            insert1(root[i],1,n,i,1),pos[a[i]]=i;
        } 
        for (int i=1;i<=m;++i){
            int l=read(),r=read();
            l=(l+ans)%n+1;r=(r+ans)%n+1;if (l>r) swap(l,r);int k=gs(root[l],1,n,l,r);
            ans=query(root[l],1,n,k+1>>1);printf(" %d",ans);
        }printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值