HDU4614 [Line Segment tree.]

Source: Internet
Author: User
Tags acos

I read and understood the code of Daniel, and then I typed the results. There were a lot of errors.

The line segment tree is regarded as a data structure. It is best to use a style that is bare and then use it as a template ..

Which of the following statements is disgusting?

In addition, if the question is a little more complex, it is doomed to repeat various formulas. Not only does DP make itself sick when paying attention to the boundary, but these formulas + 1 make the-1 result a thousand miles away, or it cannot be debugged by itself.

The idea is very important, and the template is also very important. If the line segment tree is knocked bare, it is prone to errors, which is more likely than tree array errors.
 

# Include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <ctime> # include <iostream> # include <algorithm> # include <string> # include <queue> # include <set> # include <map> # include <vector> # include <assert. h> using namespace std; # define lowbit (I) (I &-I) # define sqr (x) * (x )) # define enter printf ("\ n") # define is_sqr (x) (x & (x-1) # define pi acos (-1.0) # Define clr (x) memset (x, 0, sizeof (x) # define fp1 freopen ("in.txt", "r", stdin) # define fp2 freopen ("out.txt", "w", stdout) # define pb push_back typedef long LL; const double eps = 1e-7; const double DINF = 1e100; const int INF = 1000000006; const ll linf = 000000000000000005ll; const int MOD = (int) 1e9 + 7; const int maxn = 300005; template <class T> inline T Min (T a, T B) {return a <B? A: B;} template <class T> inline T Max (T a, T B) {return a> B? A: B;} template <class T> inline T Min (T a, T B, T c) {return min (a, B), c );} template <class T> inline T Max (T a, T B, T c) {return max (a, B), c);} struct Node {int l, r, sum, add;} node [maxn]; // this area also shows how much data the line tree needs to store. For example, if the height of the tree is H void build (int root, int l, int r) {node [root]. l = l; node [root]. r = r; node [root]. add =-1; node [root]. sum = 0; if (node [root]. l = node [root]. r) return; else {int mid = (l + r)/2; build (root * 2, l, m Id); build (root * 2 + 1, mid + 1, r) ;}} int query (int root, int l, int r) {if (node [root]. l = l & node [root]. r = r) return node [root]. sum; // another error occurs if (node [root]. add> = 0) // update a subnode? {Int ls = root * 2, rs = root * 2 + 1; // node [root]. sum = (node [root]. r-node [root]. l + 1) * node [root]. add; // this step is not required. You have modified node [ls]. sum = (node [ls]. r-node [ls]. l + 1) * node [root]. add; // two node errors [rs]. sum = (node [rs]. r-node [rs]. l + 1) * node [root]. add; // does the ls rs need to be updated? Node [ls]. add = node [rs]. add = node [root]. add; node [root]. add =-1;} int ans = 0; int mid = (node [root]. l + node [root]. r)/2; if (r <= mid) {return query (root * 2, l, r);} else if (l> mid) {return query (root * 2 + 1, l, r);} else {ans = query (root * 2, l, mid ); ans + = query (root * 2 + 1, mid + 1, r);} return ans;} void update (int root, int l, int r, int add) {if (node [root]. l = l & node [root]. r = r) // What is the relationship between the updates and the above {node [root]? add = Add; node [root]. sum = (node [root]. r-node [root]. l + 1) * node [root]. add; return;} int mid = (node [root]. l + node [root]. r)/2; if (r <= mid) {update (root * 2, l, r, add);} else if (l> mid) {update (root * 2 + 1, l, r, add);} else {update (root * 2, l, mid, add); update (root * 2 + 1, mid + 1, r, add);} node [root]. sum = node [root * 2]. sum + node [root * 2 + 1]. sum;} int bin_sea (int l, int r, int a) {int ll = l; while (l <r) {int mid = (l + r)/2; int temp _ Num = query (1, ll, mid); if (mid-ll + 1-temp_num> =) // at first, I did not write a binary search. Later, the mid-l + 1 error occurred. // the nth error. // the nth + 1 error. r = mid; else l = mid + 1;} return l;} int main () {int ncase; scanf ("% d", & ncase); while (ncase --) {int n, m, I, j; scanf ("% d", & n, & m); n --; build (1, 0, n); for (I = 1; I <= m; I ++) {int a, B, c; scanf ("% d", & a, & B, & c ); if (a = 1) {int right_num = query (1, B, n); // printf ("~ % D \ n ", right_num); if (right_num = n-B + 1) printf (" Can not put any one. \ n "); else {int left_num = B = 0? 0: query (, B-1); // If B = 0, left_num is directly equal to 0 (Note 1) // error at N + 3 // printf ("~ % D \ n ", left_num); int left_pos = bin_sea (0, n, b-left_num + 1); // is it a b-left_num + 1? Instead of + 2 int right_pos = bin_sea (B, n, min (n-B + 1-right_num, c); // (Note: 2, min) printf ("% d \ n", left_pos, right_pos); update (1, left_pos, right_pos, 1) ;}} else if (a = 2) {printf ("% d \ n", query (1, B, c); update (1, B, c, 0 );}} printf ("\ n");} return 0 ;} # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <ctime> # include <iostream> # include <algorithm> # include <string> # incl Ude <queue> # include <set> # include <map> # include <vector> # include <assert. h> using namespace std; # define lowbit (I) (I &-I) # define sqr (x) * (x )) # define enter printf ("\ n") # define is_sqr (x) (x & (x-1) # define pi acos (-1.0) # define clr (x) memset (x, 0, sizeof (x) # define fp1 freopen ("in.txt", "r", stdin) # define fp2 freopen ("out.txt", "w ", stdout) # define pb push_backtypedef long LL; const double eps = 1e-7; co Nst double DINF = 1e100; const int INF = 1000000006; const ll linf = 000000000000000005ll; const int MOD = (int) 1e9 + 7; const int maxn = 300005; template <class T> inline T Min (T a, T B) {return a <B? A: B;} template <class T> inline T Max (T a, T B) {return a> B? A: B;} template <class T> inline T Min (T a, T B, T c) {return min (a, B), c );} template <class T> inline T Max (T a, T B, T c) {return max (a, B), c);} struct Node {int l, r, sum, add;} node [maxn]; // this area also shows how much data the line tree needs to store. For example, the height of the tree is Hvoid build (int root, int l, int r) {node [root]. l = l; node [root]. r = r; node [root]. add =-1; node [root]. sum = 0; if (node [root]. l = node [root]. r) return; else {int mid = (l + r)/2; build (root * 2, l, mid); bu Ild (root * 2 + 1, mid + 1, r) ;}} int query (int root, int l, int r) {if (node [root]. l = l & node [root]. r = r) return node [root]. sum; // another error occurs if (node [root]. add> = 0) // update a subnode? {Int ls = root * 2, rs = root * 2 + 1; // node [root]. sum = (node [root]. r-node [root]. l + 1) * node [root]. add; // this step is not required. You have modified node [ls]. sum = (node [ls]. r-node [ls]. l + 1) * node [root]. add; // two node errors [rs]. sum = (node [rs]. r-node [rs]. l + 1) * node [root]. add; // does the ls rs need to be updated? Node [ls]. add = node [rs]. add = node [root]. add; node [root]. add =-1;} int ans = 0; int mid = (node [root]. l + node [root]. r)/2; if (r <= mid) {return query (root * 2, l, r);} else if (l> mid) {return query (root * 2 + 1, l, r);} else {ans = query (root * 2, l, mid ); ans + = query (root * 2 + 1, mid + 1, r);} return ans;} void update (int root, int l, int r, int add) {if (node [root]. l = l & node [root]. r = r) // What is the relationship between the updates and the above {node [root]? add = add; Node [root]. sum = (node [root]. r-node [root]. l + 1) * node [root]. add; return;} int mid = (node [root]. l + node [root]. r)/2; if (r <= mid) {update (root * 2, l, r, add);} else if (l> mid) {update (root * 2 + 1, l, r, add);} else {update (root * 2, l, mid, add); update (root * 2 + 1, mid + 1, r, add);} node [root]. sum = node [root * 2]. sum + node [root * 2 + 1]. sum;} int bin_sea (int l, int r, int a) {int ll = l; while (l <r) {int mid = (l + r)/2; int temp_num = qu Ery (1, ll, mid); if (mid-ll + 1-temp_num> =) // at first, I did not write a binary search. Later, the mid-l + 1 error occurred. // the nth error. // the nth + 1 error. r = mid; else l = mid + 1;} return l;} int main () {int ncase; scanf ("% d", & ncase); while (ncase --) {int n, m, I, j; scanf ("% d", & n, & m); n --; build (1, 0, n); for (I = 1; I <= m; I ++) {int a, B, c; scanf ("% d", & a, & B, & c ); if (a = 1) {int right_num = query (1, B, n); // printf ("~ % D \ n ", right_num); if (right_num = n-B + 1) printf (" Can not put any one. \ n "); else {int left_num = B = 0? 0: query (, B-1); // If B = 0, left_num is directly equal to 0 (Note 1) // error at N + 3 // printf ("~ % D \ n ", left_num); int left_pos = bin_sea (0, n, b-left_num + 1); // is it a b-left_num + 1? Instead of + 2 int right_pos = bin_sea (B, n, min (n-B + 1-right_num, c); // (Note: 2, min) printf ("% d \ n", left_pos, right_pos); update (1, left_pos, right_pos, 1) ;}} else if (a = 2) {printf ("% d \ n", query (1, B, c); update (1, B, c, 0 );}} printf ("\ n");} return 0 ;}

 

Related Article

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.