Summer Training Mania Brush Series--hdu 1698 Just a Hook (segment tree interval update)

Source: Internet
Author: User

Topic Connection:

http://acm.hdu.edu.cn/showproblem.php?pid=1698

Main topic:

There is a hook consisting of n sticks, the stick has three kinds of copper and silver gold, the value is three-in-one. In order to deal with each battle you need to adjust the stick that makes up a certain interval of the hook. Q. What is the total value of the hooks after Q-tuning?

Problem Solving Ideas:

Segment Tree Update. Each update to the node will be tle because of a data range problem. The interval update will save a lot of time, for each interval, if the stick inside the interval is the same type, a node variable is used to record the stick type, and if the stick is different type in the interval, the variable is recorded as-1.

1 //#include <bits/stdc++.h>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intManx =400000;8 structnode9 {Ten     intL, R, X; One     intMid () A     { -         return(L+R)/2; -     } the } Tree[manx]; - voidBuild (intRootintLintR) -{//achievements, sticks are copper -TREE[ROOT].L =l; +TREE[ROOT].R =R; -tree[root].x =1; +     if(L = =R) A         return ; atBuild (2*root+1, L, Tree[root]. Mid ()); -Build (2*root+2, Tree[root]. Mid () +1, R); - } - voidUpdate (intRootintLintRints) - { -     if(Tree[root].l==l && tree[root].r==R) in{//the current range is a stick of type S -tree[root].x =s; to         return ; +     } -     Else if(tree[root].x! =-1) the{//current interval Stick is not the same, need to transfer the state of the current interval to the left and right sub-tree and mark the current interval *tree[2*root+1].x = tree[2*root+2].x =tree[root].x; $tree[root].x =-1;Panax Notoginseng     } -     if(R <=Tree[root]. Mid ()) theUpdate (2*root+1, L, R, s); +     Else if(L >Tree[root]. Mid ()) AUpdate (2*root+2, L, R, s); the     Else +     { -Update (2*root+1, L, Tree[root]. Mid (), s); $Update (2*root+2, Tree[root]. Mid () +1, R, s); $     } - } - intSum (intRootintLints) the { -     if(tree[root].x! =-1)//The current interval stick type is no longer queried downwardWuyi         returntree[root].x * (S-l +1); the     Else -     { Wu         returnSum (2*root+1, L, Tree[root]. Mid ()) + Sum (2*root+2, Tree[root]. Mid () +1, s); -     } About } $ intMain () - { -     intT, L =0; -scanf ("%d", &t); A      while(T--) +     { the         intN, Q; -scanf ("%d%d", &n, &q); $Build (0,1, n); the          while(Q--) the         { the             intA, B, X; thescanf (" %d%d%d", &a, &b, &x); -Update (0, A, b, x); in         } the         intres = Sum (0,1, n); theprintf ("Case%d:the Total value of the hook is%d.\n", ++L, RES); About     } the     return 0; the}

  

Summer Training Mania Brush Series--hdu 1698 Just a Hook (segment tree interval update)

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.