hdu 6703 array weights segment tree

I read the title for a long time did not understand ......

You give a length of n are arranged in a. Two operations, each time increasing the inside of a number of 10 , 000 , 000, or ask not equal a1-ar, not less than the minimum number k is. k does not exceed n. So the answer must be no more than n + 1.

We consider the sequence of the original tree line do weights, index for the value of the original sequence, the subscript value of the original sequence.

Consider not modified, then not only is less than k from the segment tree superscript [k + 1, n + 1] range of answers. Is not equal to the value of a1-ar segment tree (prosequence subscript) is greater than r. To minimize the number is that we can find on the left subtree left subtree find.

Consider amending, adds a number ai 10 , 000 , 000, it means that the original ai this number, no longer is limited to a1-ar this condition, because a is a permutation, ai alone this one, now It was changed to a big number. So just a segment tree marked change the value ai corresponding to n + 1, this way, it no longer affected by a1-ar This condition limits.

Complexity is mlogn, an intuitive feeling is no longer part of the range found to be out of bounds immediately stopped. Section in the range of only a recursively in the end, a total of two, that is logn.

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 const int MAXN = 101000;
 7 int n,m,T,vec[MAXN],a[MAXN];
 8 struct segtree
 9 {
10     int maxn[4 * MAXN]; 
11     void build(int k,int l,int r,int *vec)
12     {
13         if (l == r)
14         {
15             maxn[k] = vec[l];
16             return;
17         }
18         int mid = l + r >> 1;
19         build(k << 1,l,mid,vec);
20         build(k << 1 | 1,mid + 1,r,vec);
21         maxn[k] = max(maxn[k << 1],maxn[k << 1 | 1]);
22     }
23     void change(int k,int l,int r,int x)
24     {
25         if (l == r)
26         {
27             maxn[k] = n + 1;
28             return;
29         }
30         int mid = l + r >> 1;
31         if (x <= mid)
32             change(k << 1,l,mid,x);
33         else
34             change(k << 1 | 1,mid + 1,r,x);
35         maxn[k] = max(maxn[k << 1],maxn[k << 1 | 1]);
36     }
37     int query(int k,int l,int r,int x,int y)
38     {
39         if (r < y)
40             return -1;
41         if (l == r)
42             return l;
43         int mid = l + r >> 1,res = -1;
44         if (maxn[k << 1] > x)
45             res = query(k << 1,l,mid,x,y);
46         if (res == -1)
47             if (maxn[k << 1 | 1] > x)
48                 res = query(k << 1 | 1,mid + 1,r,x,y);
49         return res;
50     }
51 } st;
52 int main()
53 {
54     for (scanf("%d",&T);T != 0;T--)
55     {
56         
57         int lst = 0;
58         scanf("%d%d",&n,&m);
59         for (int i = 1;i <= n;i++)
60         {
61             scanf("%d",&a[i]);
62             vec[a[i]] = i;
63         }
64         vec[n + 1] = n + 1;
65         st.build(1,1,n + 1,vec);
66         int tx,ty,opt;
67         for (int i = 1;i <= m;i++)
68         {
69             scanf("%d",&opt);
70             if (opt == 1)
71             {
72                 scanf("%d",&tx);
73                 tx ^= lst;
74                 st.change(1,1,n + 1,a[tx]);
75             }else
76             {
77                 scanf("%d%d",&tx,&ty);
78                 tx ^= lst;
79                 ty ^= lst;
80                 lst = st.query(1,1,n + 1,tx,ty);
81                 printf("%d\n",lst); 
82             }
83         }
84     }
85 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11404869.html