Sicily 1802 atomic nucleus US investigation (line segment tree)

Source: Internet
Author: User

The question of the newbie competition reminds me of the time for soy sauce.

// Line segment tree <br/> # include <iostream> <br/> # include <cstdio> <br/> # include <cstring> <br/> # define INF 100000000 <br/> using namespace STD; <br/> int T, M; <br/> char cmd [10], n; <br/> bool R [100005], G [100005]; <br/> struct seg <br/> {<br/> int L, R, _ max, _ min, Delta; // records the maximum value, minimum value, and optimal difference value, _ max, _ Min is 0, indicating that this number does not exist <br/>} tree [100005*4]; <br/> void Update (int x) // update by backtracking <br/>{< br/> tree [X]. _ max = max (tree [2 * X]. _ max, tree [2 * x + 1]. _ max); <B R/> If (! Tree [2 * X]. _ min) tree [X]. _ min = tree [2 * x + 1]. _ min; // If the left subtree does not have a minimum value, use the minimum value of the right subtree <br/> else tree [X]. _ min = tree [2 * X]. _ min; // otherwise, the minimum value of the Left subtree is removed. <br/> tree [X]. delta = min (tree [2 * X]. delta, tree [2 * x + 1]. delta); // obtain the minimum difference value of the optimal subtree <br/> If (tree [2 * X]. _ max & tree [2 * x + 1]. _ min) // if the maximum value of the Left subtree and the minimum value of the right subtree exist <br/> tree [X]. delta = min (tree [X]. delta, tree [2 * x + 1]. _ Min-tree [2 * X]. _ max); <br/>}< br/> void buildtree (int x, int L, int R) <br/>{< br/> tree [x ]. L = L; <br/> tree [X]. R = r; <br/> tree [X]. _ max = 0; <br/> tree [X]. _ min = 0; <br/> tree [X]. delta = inf; <br/> If (L = r) return; <br/> int mid = (L + r)> 1; <br/> buildtree (2 * X, L, mid); <br/> buildtree (2 * x + 1, Mid + 1, R ); <br/>}< br/> void insert (int x, int L, int R) <br/>{< br/> int mid = (tree [X]. L + tree [X]. r)> 1; <br/> If (tree [X]. L = L & tree [X]. R = r) <br/>{< br/> tree [X]. _ max = tree [X]. _ min = L; <br/> retu Rn; <br/>}< br/> If (r <= mid) insert (2 * X, L, R); <br/> else if (L> mid) insert (2 * x + 1, L, R); <br/> tree [X]. _ max = max (tree [2 * X]. _ max, tree [2 * x + 1]. _ max); <br/> If (! Tree [2 * X]. _ min) tree [X]. _ min = tree [2 * x + 1]. _ min; <br/> else tree [X]. _ min = tree [2 * X]. _ min; <br/> Update (x); <br/>}< br/> void remove (int x, int L, int R) <br/> {<br/> int mid = (tree [X]. L + tree [X]. r)> 1; <br/> If (tree [X]. L = L & tree [X]. R = r) <br/>{< br/> tree [X]. _ max = tree [X]. _ min = 0; <br/> tree [X]. delta = inf; <br/> return; <br/>}< br/> If (r <= mid) Remove (2 * X, L, R ); <br/> else if (L> mid) Remove (2 * x + 1, L, R); <br/> Update (x); <br/>}< br/> int query (int x) // directly query the root node to obtain the result <br/>{< br/> If (tree [X]. delta = inf) <br/> return-1; <br/> else return tree [X]. delta; <br/>}</P> <p> int main () <br/>{< br/> int X; <br/> bool first = 1; <br/> // freopen ("in.txt", "r", stdin); <br/> scanf ("% d", & T ); <br/> while (t --) <br/>{< br/> If (first) First = 0; <br/> else printf ("/N "); <br/> buildtree (1,1, 100000); <br/> memset (R, 0, sizeof (R ));/ /Mark whether to remove <br/> memset (G, 0, sizeof (g); // mark whether to generate <br/> scanf ("% d ", & M); <br/> while (M --) <br/>{< br/> scanf ("% s", CMD ); <br/> If (CMD [0] = 'G') <br/> {<br/> scanf ("% d", & X ); <br/> If (! G [x]) <br/>{< br/> G [x] = 1; <br/> insert (1, x, x ); <br/> r [x] = 0; <br/>}< br/> If (CMD [0] = 'R ') <br/>{< br/> scanf ("% d", & X); <br/> If (! R [x]) <br/>{< br/> r [x] = 1; <br/> remove (1, x, x ); <br/> G [x] = 0; <br/>}< br/> If (CMD [0] = 'q ') printf ("% d/N", query (1); <br/>}< br/>} 

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.