2019 CCPC network game HDU 6703 array segment tree [weights]


Title effect: a given array of n elements A, A, all elements are not repeated in [1, n].
There are two modes of operation:
1. The element position pos 1E7 +
2. queries that are not the smallest> value of k = [1, r] in.
Forced online.

Solution to a problem
because the unique value in the array, and in the range of 1 to n, and r and k are asked in the range of 1 to n. So for any operated one modified values will not be answer to the query , the query results are also bound to k in the range of n + 1 . Because not been modified values are unique, it is possible to establish the weights tree line, maintain value in the weight range where the maximum value of the subscript . The query is converted to the value of k is not smaller than the inside, more than the subscript r is the number of minimum weight . How it handles inquiries, a more violent solution is to directly ask the tree line segment weights in the range of k to n + 1, the first index exceeds the weight r is. But the complexity of the card may be, you need to branch cut . Plus an additional judgment on it, that is, after the completion of recursive query is greater than r subscript left subtree memory does not exist, and if not, then take a look at the subject in the right subtree is greater than the maximum value of r. If not greater than r, then you do not have to enter a query in the right subtree, otherwise the answer must be in the right subtree. Before entering the left subtree also use the same judgment the conditions to determine whether it is necessary to enter the left subtree This ensures that a single query complexity is O (log n) of. For an operation, this is equivalent to a weight modification index is infinite. Operational complexity is also O (log n) of the. In summary, the line algorithm to obtain a complexity of O (m * log n) can be quickly by this problem.

/*HDU 6703 array
权值线段树 在线处理
2019/08/27 12:00
*/
#pragma GCC optimize(3,"Ofast","inline")      //G++
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#include <functional>
#define TEST freopen("in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long Long ULL; // % LLU 
const  Double the PI = ACOS (- 1.0 );
 const  Double EPS = 1e6 ;
 const  int MOD = 1E9 + . 7 ;
 const  int INF = 1e6 + . 5 ;
 const  int MAXN = 1E5 + . 5 ;
 int n-, Q ;
 struct Node 
{ 
    int L, R & lt, POS;     // build weights segment tree maintenance interval index of the maximum 
} T [ . 4 * MAXN];
 int A [MAXN];
 void build ( int L,int R & lt, int O)    // a tree arises 
{ 
    T [O] .L = L; 
    T [O] .r = R & lt;
     IF (L == R & lt) 
    { 
        return ; 
    } 
    int MID = ( + R & lt L) >> . 1 ; 
    Build (L, MID, O << . 1 ); 
    Build (MID + . 1 , R & lt, O << . 1 | . 1 ); 
} 
void a pushup ( int O)           // rotatably its name a pushup 
{ 
    T [O] .POS = max (T [O << . 1 ] .POS, T [O <<. 1 | . 1 ] .POS); 
} 
void the Add ( int O, int X, int POS)    // add operation and maintenance interval index of the maximum 
{
     IF (T [O] == T .L [O] .r && T [O ] .L == X) 
    { 
        T [O] .POS = POS;
         return ; 
    } 
    int MID = (T [O] .L + T [O] .r) >> . 1 ;
     IF (X <= MID) 
        the Add (O << . 1 , X, POS);
     the else 
        the Add (O << . 1 | . 1 , X, POS); 
    a pushup (O); 
} 
int Qurey ( int O, int L, int R & lt, int ASK) 
{ 
    IF (T [O] == T .L [O] .r)       // to the leaf node holds interpretation answer 
    {
         IF (T [O]. POS> ASK)
             return T [O] .L;
        the else 
           return INF; 
    } 
    IF (T [O] == L .L && T [O] == R & lt .r)         // need based on left and right subtrees of the current period, when the node interval pos way to turn the next value interpretation (prune) 
    {
         IF (T [O << . 1 ] .POS> ASK) 
        { 
            return Qurey (O << . 1 , L, (L + R & lt) >> . 1 , ASK) ;
        } 
        The else IF (T [O << . 1 | . 1 ] .POS> ASK) 
        { 
            return Qurey (O << . 1 | . 1 , (L + R & lt) >> . 1 | . 1 , R & lt, ASK); 
        } 
       the else 
           return INF; 
    } 
    int = MID (T [O] .L + T [O] .r) >> . 1 ;          // familiar operation 
    IF (R & lt <= MID) 
    { 
        return Qurey (O << . 1 , L, R & lt, ASK);      / / lock section 
    }
     the else  IF (L> MID) 
    { 
        return Qurey (O << . 1 | . 1 , L, R & lt, ASK);    // or locked section 
    }
     the else 
    { 
        return min (Qurey (O << . 1 , L, MID, ASK), Qurey (O << . 1 | . 1 , MID + . 1 , R & lt , ASK));         // lock interval i.e. the answer may also be left subtree it takes a minimum value in the left subtree right subtree (left subtree return possible INF) 
    } 
} 
void the init ()      // input 
{ 

    Scanf ( " % D% D " , & n-, & Q); 
    Build ( . 1 , . 1 + n-, . 1 );
     for ( int I = . 1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        add(1,a[i],i);
    }
    add(1,n+1,INF);
}
void solve()        //解决
{
    int op,last=0;      //在线处理
    while(q--)
    {
        scanf("%d",&op);
        if(op==1)
        {
            int pos;
            scanf("%d",&pos);
            pos^=last;
            add(1,a[pos],INF);
        }
        else
        {
            int r,k;
            scanf("%d%d",&r,&k);
            r^=last,k^=last;
            last=qurey(1,k,n+1,r);
            printf("%d\n",last);
        }
    }
}
int main()
{
//    TEST
    int T;
    scanf("%d",&T);
    while(T--)
    {
        mem(t,0);
        init();
        solve();
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/shuaihui520/p/11419780.html