Hdoj 1698-Just a Hook line segment tree exercise

Source: Internet
Author: User
/*************************************** * ******* Copyright: glderAuthor: GlderDate: 2013-08-03 14: 54: 46 Destription: 1. Plug-in problem (segment update) 2. Add more element weights in the build structure. 3. Note A in the build operation: if (l = r) {s [root]. num = 1; s [root]. x = 0; cout <"root:" <root <endl; return;} if this is the case, all s [root] cannot be written. x is updated and wa appears. You only need to replace s [root]. x = 0; when it comes to the if statement, it can be ac, because the last pushup statement can ensure that every num can be updated. B: s [root]. num = 1; s [root]. x = 0; cout <"root:" <root <endl; if (l = r) {return;} if this is the case, the order of creation is as follows: root: 1 2 4 8 16 17 9 5 10 11 3 6 12 24 25 13 7 14 154. During the update operation, pay attention to the need to migrate data from the parent node to the child node, this is an important part of this question. 5. The sum of all values must be queried at the end of this question. s [1] can be output directly. num, you can also directly use query (, n ); **************************************** * *****/# include <iostream> # include <cstdio> using namespace std; struct Segtree {int left; int right; int num; int x; // weight} s [100005 <2]; void build (int root, int l, int r) {s [root]. left = l; s [root]. right = r; s [root]. x = 0; if (l = r) {s [root]. num = 1; return;} int m = (l + r)/2; build (root <1, l, m); build (root <1 | 1, m + 1, r); s [root]. num = s [root <1]. num + s [root <1 | 1]. num;} void update (int root, int a, int B, int x) {int l = s [root]. left; int r = s [root]. right; if (a <= l & B> = r) {s [root]. num = x * (r-l + 1); s [root]. x = x; return;} if (s [root]. x) // update the current node information to the leaf node {int p = r-l + 1; s [root <1]. x = s [root]. x; s [root <1 | 1]. x = s [root]. x; s [root <1]. num = (p-p/2) * s [root]. x; s [root <1 | 1]. num = (p/2) * s [root]. x; s [root]. x = 0;} int m = (l + r)> 1; if (a <= m) update (root <1, a, B, x ); if (B> m) update (root <1 | 1, a, B, x); s [root]. num = s [root <1]. num + s [root <1 | 1]. num;} int query (int root, int a, int B) {int l = s [root]. left; int r = s [root]. right; if (a = l & B = r) return s [root]. num; int m = (l + r)> 1; int cnt = 0; if (a <= m) cnt + = query (root <1,, b); if (B> m) cnt + = query (root <1 | 1, a, B); return cnt;} int main () {int t, n, q; int x, y, c; scanf ("% d", & t); int cas = 1; while (t --) {scanf ("% d ", & n); build (1,1, n); scanf ("% d", & q); while (q --) {scanf ("% d ", & x, & y, & c); update (1, x, y, c);} printf ("Case % d: The total value of the hook is % d. \ n ", cas ++, query (, n ));}}

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.