Functional trie Ideology & bzoj 3261 & 3166 question

Source: Internet
Author: User

[Original question 1]

3261: maximum variance or time limit: 10 sec memory limit: 512 MB
Submit: 497 solved: 215
[Submit] [Status] Description

Given a non-negative integer sequence {A}, the initial length is N.
There are m operations, and there are two types of operations:
 
1. a x: add operation, indicating that a number X is added at the end of the sequence, and the length of the sequence is n + 1.
2. q l r x: query the operation. You need to locate P to meet L <= P <= r, so that:
 
A [p] xor a [p + 1] XOR... xor a [n] xor x is the maximum value and the maximum output value is the maximum value.

Input

The first line contains two integers, N and M. The meaning is shown in the Problem description.
The second row contains N non-negative integers, indicating the initial sequence.
 
In the next M line, each row describes an operation in the format described in the topic.

Output

Assume that there are t query operations, t rows are output, and an integer in each row indicates the answer to the query.

Sample input5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
For test points 1-2, n, m <= 5.

For test points 3-7, n, m <= 80000.
For test points 8-10, n, m <= 300000.

The test points 1, 3, 5, 7, and 9 are not modified.
For 100% of the data, 0 <= A [I] <= 10 ^ 7. Sample output4
5
6
Hint

For 100% of the data, 0 <= A [I] <= 10 ^ 7.


[Analysis] What is the functional trie? It is probably similar to the functional Line Segment tree (the Chairman tree. Take the above example. We require a certain number of P in the l -- r range, which is actually to use max (sum [N] ^ sum [P-1] ^ X ). Sum indicates 1 ~ I prefix XOR. (Because XOR satisfies the interval reduction property) Because sum [N] ^ X is a fixed value for each query, we may set it to y.

First, sum [1] ~ Sum [N] is inserted into function trie in sequence. (Note: Only log (n) nodes are updated each time, and all the remaining vertices are copied the last time.) Then we crawl 1.1 points in the trie of the L-R section. (The problem of + 1 or-1 can be solved by yourself. In the code, I added a node No. 1 that is 0, which facilitates computation. As a result, a + 1 node is missing.) during insertion, we can record a second node, the time when S is inserted. Assume that the XOR value is the largest, and we should first go to ^ 1. How to determine L ~ In R, is there ^ 1 in a prefix binary? Use s to subtract. If it is greater than 0, it indicates that it has been inserted in the middle.

[Code] (extremely slow)

#include<cstdio>#define N 600005using namespace std;int a[N*25][2],s[N*25],root[N],b[N];int n,m,i,T,L,R,tot;char opt[3];void insert(int x,int &y,int Num,int d){  int p=(Num>>d)&1;s[y=++tot]=s[x]+1;  if (d<0) return;  a[y][p^1]=a[x][p^1];  insert(a[x][p],a[y][p],Num,d-1);}int Query(int x,int y,int Num,int d){  if (d<0) return 0;int p=(Num>>d)&1;  if (s[a[y][p^1]]-s[a[x][p^1]]) return (1<<d)+Query(a[x][p^1],a[y][p^1],Num,d-1);  return Query(a[x][p],a[y][p],Num,d-1);}inline int Read(){  char ch=getchar();for (;ch<'0'||ch>'9';ch=getchar());  int x=0;for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';  return x;}int main(){  n=Read();m=Read();  insert(root[0],root[1],b[i=1]=0,24);n++;  for (i=2;i<=n;i++)    T=Read(),b[i]=b[i-1]^T,insert(root[i-1],root[i],b[i],24);  while (m--)  {    scanf("%s",opt);    if (opt[0]=='A')       T=Read(),n++,insert(root[n-1],root[n],b[n]=b[n-1]^T,24);    else      L=Read(),R=Read(),T=Read(),printf("%d\n",Query(root[L-1],root[R],b[n]^T,24));  }  return 0;}

[Original question 2]

3166: [heoi2013] alotime limit: 20 sec memory limit: 256 MB
Submit: 368 solved: 189
[Submit] [Status] Description

Welcome to AlO (arithmetic and logistic online ). This is a vr mmorpg,
As you can see by name, there are full of mathematical puzzles.
Now you have n gems, each of which has an energy density, which is recorded as AI. the energy of these gems
The density is different. Now you can choose to combine several continuous gems (more than one) and set them to AI, AI + 1 ,..., A j, then the energy density of the integrated gem is the secondary density of the energy density in these gems.
The energy density of any other gemstone is an exclusive or value, that is, set the energy density of this gemstone.
If it is K, the energy density of the generated gemstone is max {k xor ap | AP =k, I ≤ p ≤ j }.
Now you need to know how to choose the gems that need to be integrated to maximize the energy density of the generated gems.

Input

The first line is an integer N, which indicates the number of gems.
In the second row, N integers represent A1 to an, respectively, indicating the energy density of each gemstone, ensuring that there is Ai =aj for I? J.
 

Output

Output a line of an integer, indicating the maximum energy density of the gemdale.

Sample input5
9 2 1 4 7


Sample output14

Hint



[Example]

Select the interval [1, 5]. The maximum value is 7 XOR 9.

 

 

For 100% of data, 1 ≤ n ≤ 50000, 0 ≤ AI ≤ 10 ^ 9

Source

Enhanced Data by HTA


[Analysis] At the beginning, I felt that I could not start with it: Generally, I have a Q-related question. Here I am looking for the maximum value! We can do the following conversion: enumerate each vertex I, use it as the secondary Shard, and then expand to the left and right sides. Assume that L [I] indicates the first subscript of a vertex larger than him on the left of vertex I, and ll [I] indicates the subscript OF THE SECOND vertex larger than him. The same applies to R. You can divide the situation into two types: ll [I] + 1 ~, R [I]-1, L [I] + 1 ~ Rr [I]. (It can be divided into only one type, please YY on your own) Suppose we have obtained it within a considerable efficiency, and then the principle is not bad with the previous question ~ R crawls in trie.

What is the problem? L [I] and R [I] can be obtained using a monotonic queue. Ll [I] I used to use L [L [I], but this is obviously a problem. (Sorry, I found out when I was playing the video ...) What should we do? I also thought about the efficiency of using a similar query set. However, if the data is 100,101, 99, 98. When you look for the ll value of 101, it will be very slow and almost scanned once !! So it's hard. Later, I thought lower_bound was acceptable, but it was troublesome.

After consulting with Red Bull, he also used that method of God. He quickly refuted my data. In the calculation of, 99, 98..., transfer is O (1) level, so the average efficiency is O (n ). Fierce! After writing his method, I lost it smartly and ran like...

[Code] (temporary rank 1)

#include<cstdio>#include<algorithm>#define N 50005using namespace std;int A[N],B[N],L[N],R[N],LL[N],RR[N],q[N],root[N],s[N*200],a[N*200][2];int n,h,t,i,ans,tot,Max,temp;void insert(int x,int &y,int Num,int d){  s[y=++tot]=s[x]+1;  if (d<0) return;  int p=(Num>>d)&1,temp=y;  a[y][p^1]=a[x][p^1];  insert(a[x][p],a[y][p],Num,d-1);}int Query(int x,int y,int Num,int d){  if (d<0) return 0;int p=(Num>>d)&1;  if (s[a[y][p^1]]-s[a[x][p^1]]) return (1<<d)+Query(a[x][p^1],a[y][p^1],Num,d-1);  return Query(a[x][p],a[y][p],Num,d-1);}inline int Read(){  char ch=getchar();for (;ch<'0'||ch>'9';ch=getchar());  int x=0;for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';  return x;}inline int wentL(int k){  if (A[k]>A[i]||!k) return k;  return wentL(L[k]);}inline int wentR(int k){  if (A[k]>A[i]||k==n+1) return k;  return wentR(R[k]);}int main(){  n=Read();  for (i=1;i<=n;i++) A[i]=Read(),Max=max(Max,A[i]);  h=t=0;  for (i=1;i<=n;i++)  {    while (h<t&&A[i]>=A[q[t]]) t--;    L[i]=(h>=t)?0:q[t];    //LL[i]=(h+1>=t)?0:q[t-1];    q[++t]=i;  }  h=t=0;  for (i=n;i;i--)  {    while (h<t&&A[i]>=A[q[t]]) t--;    R[i]=(h>=t)?n+1:q[t];    //RR[i]=(h+1>=t)?0:q[t-1];    q[++t]=i;  }  L[0]=0;R[n+1]=n+1;  for (i=2;i<=n;i++) LL[i]=L[i]?wentL(L[i]-1):0;  RR[n]=n+1;for (i=n-1;i;i--)   RR[i]=R[i]<=n?wentR(R[i]+1):n+1;  insert(root[n+1],root[0],0,30);  for (i=1;i<=n;i++)     insert(root[i-1],root[i],A[i],30);  for (i=1;i<=n;i++)  {    if (A[i]==Max) continue;    if (L[i]) ans=max(ans,temp=Query(root[LL[i]],root[R[i]-1],A[i],30));    if (R[i]<=n) ans=max(ans,temp=Query(root[L[i]],root[RR[i]-1],A[i],30));       }  printf("%d",ans);  return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.