Getting started with linear Foundation

Source: Internet
Author: User
Preface

Linear basisIt is really a magic algorithm.

It can be used to solve the maximum variance or sum in a set and is highly efficient. It is the time complexity of \ (O (N \ log \ maxnum.

Therefore, it is worth learning.

Linear basis Definition

What is linear basis?

For an array \ (a_1a_2... a_n \), we can use \ (num_1num_2... num _ {log \ max (a_ I)} \) to record the first one.The number with the highest bits in binary that appear in the \ (I \) bits..

Use the \ (Num \) array to find the maximum exclusive or sum in the array.

Construction Method

The construction of linear basis is actually very clever.

We first find the highest bit \ (I \) in the binary \ (x \) of the number currently inserted, and then determine whether \ (num_ I \) is \ (0 \) for classification discussion:

  • \ (Num_ I = 0 \). It indicates that there is no element in this bit, so the \ (num_ I = x \) command is used \).
  • \ (Num_ I =0 \). If there is an element on this bit, we order \ (x \) ^ \ (= num_ I \) to remove this bit, and then continue to find \ (x \) repeat the above process.

The maximum bit of \ (x \) must be decreasing, so the complexity of a single time is \ (O (log _ {a_ I, the total complexity is \ (O (N \ log \ max (a_ I.

Maximum difference or sum of queries

It is actually very easy to ask about the greatest difference or sum.

We enumerate each \ (p_ I \) from high to low, and use \ (ANS \) to record the answer.

For the current \ (p_ I \), we haveGreedyIf \ (ANS \) ^ \ (num_ I> ans \), update \ (ANS \) to \ (ANS \) ^ \ (num_ I \), otherwise, it will be skipped.

At first glance, it seems as if there are many loopholes. In fact, there is still some truth to think carefully.

Why must we be greedy?

First, we need to know the simplest nature: For \ (num_ I \), if it is not \ (0 \), the highest bit in the binary must be the \ (I \) bit.

The proof of this point can be combined with the above linear basis definition and construction method, which is not proved here.

It's easy to think,\ (ANS \) ^ \ (num_ I \) is definitely affected by the rear \ (I \) bit, And the \ (I \) Bit must have changed.

Therefore, if \ (ANS \) ^ \ (num_ I> ans \), we can get the \ (I \) the bits are \ (0 \), and The \ (I \) bits in the \ (ANS \) binary after the XOR are \ (1 \).

Since subsequent operations do not affect the size of the \ (I \) bit and the previously operated bitThe higher the \ (I \) bits, the better..

So we can solve the problem through greed.

The same is true for the reason why \ (ANS \) ^ \ (num_ I \ le ans \) is skipped and not processed.

Other operations

In addition to these two basic operations, the linear basis still has many uses, such:

  • Check whether some numbers are different or \ (k \).
  • Query the small value of \ (k.

For these operations,I just heard that,So we will not introduce it here.

Code

Take the linear basis of [Luogu 3812] [TEMPLATE] as an example and paste a piece of code:

# Include <bits/stdc ++. h> # define N 50 # define ll long longusing namespace STD; int N; Class FIO {PRIVATE: # define fsize 100000 # define Tc () (A = B & (B = (a = fin) + fread (FIN, 1, fsize, stdin), A = B )? EOF: * A ++) # define PC (CH) (void) (putchar (CH) int top; char CH, * a, * B, Fin [fsize], stack [fsize]; public: FIO () {a = B = fin;} inline void read (Int & X) {x = 0; while (! Isdigit (CH = tc (); While (x = (x <3) + (x <1) + (ch & 15 ), isdigit (CH = tc ();} inline void read (LL & X) {x = 0; while (! Isdigit (CH = tc (); While (x = (x <3) + (x <1) + (ch & 15 ), isdigit (CH = tc ();} inline void write (ll x) {If (! X) return PC ('0'); While (x) stack [++ top] = x % 10 + 48, X/= 10; while (top) PC (stack [top --]) ;}}f; Class class_linearbasis // linear base template {PRIVATE: # define logmax 50 LL num [logmax + 5]; public: inline void insert (ll x) // insert {for (register int I = logmax ;~ I; -- I) {If (! (X & (1ll <I) continue; // skip if (Num [I]) x ^ = num [I]; else return (void) (Num [I] = x); // if there is no value for this bit, so that num [I] = x; otherwise, make x ^ = num [I]} inline ll querymax (register ll ans = 0) // query the maximum variance or {for (register int I = logmax ;~ I; -- I) if (ANS ^ num [I])> ans) ans ^ = num [I]; // For each num [I] greedy decision to select or not select return ans; // return answer} linearbasis; int main () {register int I; register ll X; for (F. read (N), I = 1; I <= N; ++ I) F. read (x), linearbasis. insert (x); Return F. write (linearbasis. querymax (), 0 ;}

Getting started with linear Foundation

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.