hdu6703主席树加set

array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2158    Accepted Submission(s): 848


 

Problem Description

You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the array is **unique**.

Moreover, there are m instructions.

Each instruction is in one of the following two formats:

1. (1,pos),indicating to change the value of apos to apos+10,000,000;
2. (2,r,k),indicating to ask the minimum value which is **not equal** to any ai ( 1≤i≤r ) and **not less ** than k.

Please print all results of the instructions in format 2.

 

 

 

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, there are two integers n(1≤n≤100,000),m(1≤m≤100,000) in the first line, denoting the size of array a and the number of instructions.

In the second line, there are n distinct integers a1,a2,...,an (∀i∈[1,n],1≤ai≤n),denoting the array.
For the following m lines, each line is of format (1,t1) or (2,t2,t3).
The parameters of each instruction are generated by such way :

For instructions in format 1 , we defined pos=t1⊕LastAns . (It is promised that 1≤pos≤n)

For instructions in format 2 , we defined r=t2⊕LastAns,k=t3⊕LastAns. (It is promised that 1≤r≤n,1≤k≤n )

(Note that ⊕ means the bitwise XOR operator. )

Before the first instruction of each test case, LastAns is equal to 0 .After each instruction in format 2, LastAns will be changed to the result of that instruction.

(∑n≤510,000,∑m≤510,000 )

 

 

Output

For each instruction in format 2, output the answer in one line.

 

 

Sample Input

 

3 5 9 4 3 1 2 5 2 1 1 2 2 2 2 6 7 2 1 3 2 6 3 2 0 4 1 5 2 3 7 2 4 3 10 6 1 2 4 6 3 5 9 10 7 8 2 7 2 1 2 2 0 5 2 11 10 1 3 2 3 2 10 10 9 7 5 3 4 10 6 2 1 8 1 10 2 8 9 1 12 2 15 15 1 12 2 1 3 1 9 1 12 2 2 2 1 9

 

 

Sample Output

 

1 5 2 2 5 6 1 6 7 3 11 10 11 4 8 11

Hint

note: After the generation procedure ,the instructions of the first test case are : 2 1 1, in format 2 and r=1 , k=1 2 3 3, in format 2 and r=3 , k=3 2 3 2, in format 2 and r=3 , k=2 2 3 1, in format 2 and r=3 , k=1 2 4 1, in format 2 and r=4 , k=1 2 5 1, in format 2 and r=5 , k=1 1 3 , in format 1 and pos=3 2 5 1, in format 2 and r=5 , k=1 2 5 2, in format 2 and r=5 , k=2 the instructions of the second test case are : 2 7 2, in format 2 and r=7 , k=2 1 5 , in format 1 and pos=5 2 7 2, in format 2 and r=7 , k=2 2 8 9, in format 2 and r=8 , k=9 1 8 , in format 1 and pos=8 2 8 9, in format 2 and r=8 , k=9 the instructions of the third test case are : 1 10 , in format 1 and pos=10 2 8 9 , in format 2 and r=8 , k=9 1 7 , in format 1 and pos=7 2 4 4 , in format 2 and r=4 , k=4 1 8 , in format 1 and pos=8 2 5 7 , in format 2 and r=5 , k=7 1 1 , in format 1 and pos=1 1 4 , in format 1 and pos=4 2 10 10, in format 2 and r=10 , k=10 1 2 , in format 1 and pos=2

 

 

Source

2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

 把操作1放入一个set中,操作2,就是查[r+1,n]中大于等于n的第一个数,再和set中的数比较

#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
//head

const int N = 100010;
int t;
int n, m, q, tot = 0;
int a[N], b[N];
int T[N], sum[N*20], L[N*20], R[N*20];
setst;
set::iterator it;
inline int build(int l, int r)
{
    int rt = ++ tot;
    if (l < r){
            int mid=(l+r)/2;
        L[rt] = build(l, mid);
        R[rt] = build(mid+1, r);
    }
    return rt;
}

inline int update(int pre, int l, int r, int x)
{
    int rt = ++ tot;
    L[rt] = L[pre];
    R[rt] = R[pre];
    sum[rt] = sum[pre] + 1;
    if (l < r){
            int mid=(l+r)/2;
        if (x <= mid) L[rt] = update(L[pre], l, mid, x);
        else R[rt] = update(R[pre], mid+1, r, x);
    }
    return rt;
}

inline int query(int u, int v, int l, int r, int k)
{if(sum[u]==sum[v])
return inf;
    if(l==r)
        return l;
        int ans=inf;
    int mid=(l+r)/2;
    if(k<=mid)
        ans=query(L[u],L[v],l,mid,k);
    if(ans==inf)
        ans=query(R[u],R[v],mid+1,r,k);
    return ans;
}

int main()
{scanf("%d",&t);
while(t--)
{ tot = 0;
        memset(T, 0, sizeof T);
        memset(sum, 0, sizeof sum);
        memset(L, 0, sizeof L);
         memset(R, 0, sizeof R);

    scanf("%d%d",&n,&m);
    T[0]=build(1,n+1);
    for(int i=1;i<=n;i++)
    {scanf("%d",&a[i]);
    T[i]=update(T[i-1],1,n+1,a[i]);
    }
    T[n+1]=update(T[n],1,n+1,n+1);
    int ans=0;
    st.clear();
st.insert(n+1);
    int pos=0;


    while(m--)
    {
        int op;
        int t1,t2;
        scanf("%d",&op);
        if(op==1)
        {scanf("%d",&t1);
        pos=t1^ans;
        st.insert(a[pos]);
    }
    else if(op==2)
    {
        scanf("%d%d",&t1,&t2);

        int r=t1^ans;
        int k=t2^ans;
        int ans1=query(T[r],T[n+1],1,n+1,k);
        int ans2=*st.lower_bound(k);
        ans=min(ans1,ans2);
        printf("%d\n",ans);
    }
}

    }
    return 0;
}

 

你可能感兴趣的:(主席树)