Segment tree Interval Update (hdu1698) interval Merge (poj3667)

Source: Internet
Author: User
Tags bitset cmath

The latency tag used for Interval Update. It is only updated to the maximum parent interval during the update. The updated status is recorded. When the access to the subinterval is reached, the corresponding records are updated in a timely manner, pushodown ().

Code:

 

# Include <cstdlib>
# Include <cctype>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <sstream>
# Include <set>
# Include <queue>
# Include <stack>
# Include <fstream>
# Include <iomanip>
# Include <bitset>
# Include <list>
# Include <ctime>
Using namespace STD;

# Define set (ARR, what) memset (ARR, what, sizeof (ARR ))
# Define ff (I, A) for (I = 0; I <A; I ++)
# Define SD (a) scanf ("% d", &)
# Define SSD (a, B) scanf ("% d", & A, & B)
# Define SF (a) scanf ("% lf", &)
# Define SS (a) scanf ("% s",)
# Define SLD (a) scanf ("% LLD", &)
# Define PF (a) printf ("% d \ n",)
# Define PPF (a, B) printf ("% d \ n", a, B)
# Define SZ (ARR) (INT) A. Size ()
# Define swap (A, B) A = a xor B; B = a xor B; A = a xor B;
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "W", stdout)
# Define max 1 <30
# Define ESP 1e-5
# Define lson L, M, RT <1
# Define rson m + 1, R, RT <1 | 1
Template <class T> inline t sqr (t a) {return a * ;}
Template <class T> inline void Amin (T & A, t B) {if (a =-1 | A> B) A = B ;}
Template <class T> inline void amax (T & A, t B) {if (a <B) A = B ;}
Template <class T> inline T min (t a, t B) {return A> B? B: ;}
Template <class T> inline T max (t a, t B) {return A> B? A: B ;}
Const int maxn = 100001;
Int sum [maxn <2];
Int Val [maxn <2];
Void pushup (int rt ){
Sum [RT] = sum [RT <1] + sum [RT <1 | 1];
}
Void Pushdown (int rt, int CNT ){
If (Val [RT]) {
Val [RT <1] = Val [RT <1 | 1] = Val [RT];
Sum [RT <1] = (CNT-(CNT> 1) * val [RT];
Sum [RT <1 | 1] = (CNT> 1) * val [RT];
Val [RT] = 0; // modify the cache value to 0 after update to avoid repeated updates

}
}
Void build (int l, int R, int RT ){
Val [RT] = 0;
Sum [RT] = 1;
If (r = L) return;
Int M = (L + r)> 1;
Build (lson );
Build (rson );
Pushup (RT );
}
Void Update (int l, int R, int C, int L, int R, int RT ){
If (L <= L & R <= r ){
Val [RT] = C;
Sum [RT] = (R-l + 1) * C;
Return;
}
Pushdown (RT, R-l + 1); // The subinterval is used and updated based on the Val of the parent node.
Int M = (L + r)> 1;
If (L <= m) Update (L, R, C, lson );
If (r> m) Update (L, R, C, rson );
Pushup (RT );
}
Int main (){
Int t, n, I, j, M, A, B, C;
SD (t );
Ff (I, T ){
SSD (n, m );
Build (1, n, 1 );
While (M --){
SSD (a, B); SD (C );
Update (a, B, c, 1, n, 1 );
}
Printf ("case % d: the total value of the hook is % d. \ n", I + 1, sum [1]);
}
Return 0;
}

 

 

The range merging is quite tangled, mainly because the accuracy of lsum and rsum is not understood at the beginning. The latency technology is also used during the update.

Code:

# Include <cstdlib>
# Include <cctype>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <sstream>
# Include <set>
# Include <queue>
# Include <stack>
# Include <fstream>
# Include <iomanip>
# Include <bitset>
# Include <list>
# Include <ctime>
Using namespace STD;

# Define set (ARR, what) memset (ARR, what, sizeof (ARR ))
# Define ff (I, A) for (I = 0; I <A; I ++)
# Define SD (a) scanf ("% d", &)
# Define SSD (a, B) scanf ("% d", & A, & B)
# Define SF (a) scanf ("% lf", &)
# Define SS (a) scanf ("% s",)
# Define SLD (a) scanf ("% LLD", &)
# Define PF (a) printf ("% d \ n",)
# Define PPF (a, B) printf ("% d \ n", a, B)
# Define SZ (ARR) (INT) A. Size ()
# Define swap (A, B) A = a xor B; B = a xor B; A = a xor B;
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "W", stdout)
# Define max 1 <30
# Define ESP 1e-5
# Define lson L, M, RT <1
# Define rson m + 1, R, RT <1 | 1
Template <class T> inline t sqr (t a) {return a * ;}
Template <class T> inline void Amin (T & A, t B) {if (a =-1 | A> B) A = B ;}
Template <class T> inline void amax (T & A, t B) {if (a <B) A = B ;}
Template <class T> inline T min (t a, t B) {return A> B? B: ;}
Template <class T> inline T max (t a, t B) {return A> B? A: B ;}
Const int maxn = 50010;
Int msum [maxn <2], lsum [maxn <2], rsum [maxn <2];
Int cover [maxn <2];
Void pushup (int rt, int CNT ){
Lsum [RT] = lsum [RT <1];
Rsum [RT] = rsum [RT <1 | 1];
If (lsum [RT] = CNT-(CNT> 1) lsum [RT] + = lsum [RT <1 | 1];
If (rsum [RT] = (CNT> 1) rsum [RT] + = rsum [RT <1];
Msum [RT] = max (lsum [RT <1 | 1] + rsum [RT <1], max (msum [RT <1], msum [RT <1 | 1]);
}
Void Pushdown (int rt, int CNT ){
If (cover [RT]! =-1 ){
Cover [RT <1] = cover [RT <1 | 1] = cover [RT];
Msum [RT <1] = lsum [RT <1] = rsum [RT <1] = cover [RT]? 0: CNT-(CNT> 1 );
Msum [RT <1 | 1] = lsum [RT <1 | 1] = rsum [RT <1 | 1] = cover [RT]? 0: (CNT> 1 );
Cover [RT] =-1;
}
}
Void build (int l, int R, int RT ){
Msum [RT] = lsum [RT] = rsum [RT] = r-L + 1;
Cover [RT] =-1;
If (L = r) return;
Int M = (L + r)> 1;
Build (lson );
Build (rson );
}
Void Update (int l, int R, int C, int L, int R, int RT ){
If (L <= L & R <= r ){
Msum [RT] = lsum [RT] = rsum [RT] = C? 0: (R-l + 1 );
Cover [RT] = C;
Return;
}
Pushdown (RT, R-l + 1 );
Int M = (R + l)> 1;
If (L <= m) Update (L, R, C, lson );
If (M <r) Update (L, R, C, rson );
Pushup (RT, R-l + 1 );
}
Int query (int w, int L, int R, int RT ){
If (L = r) return l;
Pushdown (RT, R-l + 1 );
Int M = (L + r)> 1;
If (msum [RT <1]> = W) return query (W, lson); // The left interval must be
Else if (rsum [RT <1] + lsum [RT <1 | 1]> = W) return M-rsum [RT <1] + 1; // The interval merging meets the requirements
Return query (W, rson); // only the right range can be satisfied
}
Int main (){
Int n, m, I, j, t, a, B, Q;
While (~ SSD (n, m )){
Build (1, n, 1 );
While (M --){
SD (t );
If (t = 1 ){
SD ();
If (msum [1] <A) PF (0 );
Else {
Q = query (A, 1, n, 1 );
PF (Q );
Update (Q, q + A-1, 1, 1, n, 1 );
}
} Else {
SSD (A, B );
Update (A, A + b-1, 0, 1, n, 1 );
}
}
}
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.