HDU -- 3275 -- Line Segment tree <again>

Source: Internet
Author: User

It also enhances the understanding of the delay mark of the line tree ~~

The question is very easy to give you a string of numbers 01. each operation must be performed on the number of K to flip it that is 0-> 1 1-> 0 is equivalent to the number of a range of [l, l + K-1] 0 and 1 swap operation

First, you must consider that the minimum number of operations must start from the leftmost.

Then, I find the leftmost 0 position in each query (FIND) operation. If it does not exist, I will return-1, indicating that all are 1.

Then on this [POs, POS + k-1] Flip operation here there is a judgment is if POS + k-1> N, it is equivalent to the failure to achieve all the results of 1 through flip.

Pay attention to the usage of Pushdown and pushup functions and the content of the function body... these are the essence. It is worth understanding ~ And I cannot understand it ..

 

1 // 0 indicates that the lantern is dark. 1 indicates that the lantern is bright. 2 # include <iostream> 3 # include <algorithm> 4 using namespace STD; 5 6 const int size = 100010; 7 char STR [size]; 8 struct data 9 {10 int L; 11 int R; 12 INT flag; 13 int numone, numzero; 14} tree [size * 4]; 15 16 void pushup (INT root) 17 {18 tree [root]. numone = tree [root <1]. numone + tree [root <1 | 1]. numone; 19 tree [root]. numzero = tree [root <1]. numzero + tree [root <1 | 1]. n Umzero; 20} 21 22 void Pushdown (INT root) 23 {24 tree [root <1]. flag ^ = 1; 25 tree [root <1 | 1]. flag ^ = 1; 26 swap (tree [root <1]. numzero, tree [root <1]. numone); 27 swap (tree [root <1 | 1]. numzero, tree [root <1 | 1]. numone); 28} 29 30 void build (INT root, int L, int R) 31 {32 int mid = (L + r)> 1; 33 tree [root]. L = L; 34 tree [root]. R = r; 35 Tree [root]. flag = 0; 36 IF (L = r) 37 {38 if (STR [l] = '1') 39 {40 tree [root]. numone = 1; 41 tree [root]. numzero = 0; 42} 43 else 44 {45 tree [root]. numone = 0; 46 tree [root]. numzero = 1; 47} 48 return; 49} 50 build (root <1, L, mid); 51 build (root <1 | 1, Mid + 1, r); 52 pushup (Root); 53} 54 55 void Update (INT root, int L, int R) 56 {57 int mid = (tree [root]. L + tree [root]. r)> 1; 58 If (tree [root]. L = L & tree [root]. R = R) 59 {60 swap (tree [root]. numzero, tree [root]. numone); 61 tree [root]. flag ^ = 1; 62 Return; 63} 64 if (tree [root]. flag) 65 {66 Pushdown (Root); 67 tree [root]. flag = 0; 68} 69 If (r <= mid) 70 update (root <1, L, R); 71 else if (L> = Mid + 1) 72 Update (root <1 | 1, L, R); 73 else 74 {75 Update (root <1, L, mid ); 76 Update (root <1 | 1, Mid + 1, R); 77} 78 pushup (Root); 79} 80 8 1 int query (INT root) 82 {83 int ans =-1; 84 If (tree [root]. L = tree [root]. r) 85 {86 return tree [root]. l; 87} 88 If (tree [root]. flag) 89 {90 Pushdown (Root); 91 tree [root]. flag = 0; 92} 93 If (tree [root <1]. numzero) 94 ans = query (root <1); 95 else if (tree [root <1 | 1]. numzero) 96 ans = query (root <1 | 1); 97 return ans; 98} 99 100 int main () 101 {102 cin. sync_with_stdio (false); 103 Int N, K, CNT, Pos; 104 bool flag; 105 while (CIN> N> K & (N | K) 106 {107 CNT = 0; 108 flag = true; 109 CIN >>( STR + 1); 110 build (1, 1, n); 111 If (tree [1]. numone = N) 112 {113 cout <0 <Endl; 114} 115 else if (! K) 116 {117 cout <-1 <Endl; 118} 119 else120 {121 while (1) 122 {123 pos = query (1 ); 124 If (Pos =-1) 125 break; 126 If (Pos + k-1> N) 127 {128 flag = false; 129 break; 130} 131 update (1, POs, pos + k-1); 132 + + CNT; 133} 134 If (FLAG) 135 cout <CNT <Endl; 136 else137 cout <-1 <Endl; 138} 139} 140 return 0; 141}
View code

 

Today:

Why are you ignoring me?

Do you dislike me?

Adults are tactically isolated from each other.

HDU -- 3275 -- Line Segment tree <again>

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.