Hdu1689 (just a hook)

Source: Internet
Author: User

Question address: Just a hook

Question:

In data, a hero named "Butcher" has a hook consisting of several metal rods. The metal rods can be divided into copper rods whose Val is 1 and the silver rod whose Val is 2, the Val value of the golden rod is 3. The initial value of the butchers' hook is 1. The Butch' wants to perform some operations to change the Val value of the metallic rod in some ranges. The changed property is changed from the value of all metal rods in the X-> Y range to Z. Finally, find the sum of hook values.

Solution:

The number of line segments. Lazy mark is used to obtain the sum of the updated segments.

Code:

1 # include 2 # include 3 # include 4 # include 5 # include 6 # include 7 # include 8 # include 9 # include 10 # include 11 # include 12 # include 13 # include 14 // # include 15 # include 16 using namespace STD; 17 /************************************** */18 # define ll long 19 # define int64 _ int64 20 # define PI 3.1415927 21 /************************** * ***********/22 const int INF = 0x7f7f7f7f; 23 const double EPS = 1e-8; 24 const double PIE = ACOs (-1.0); 25 const int d1x [] = {0,-, 1 }; 26 const int d1y [] = {-,}; 27 const int d2x [] = {0,-, 1}; 28 const int d2y [] =, -}; 29 const int FX [] = {-1,-1,-,}; 30 const int FY [] = {-, 1, -,-, 1}; 31 C Onst int dirx [] = {-,-}; 32 const int diry [] = {-2,-2,-1, 1, 2}; 33/* vector map [N]; Map [A]. push_back (B); int Len = map [v]. size (); */34 /************************************ ***/35 void openfile () 36 {37 freopen ("data. in "," rb ", stdin); 38 freopen (" data. out "," WB ", stdout); 39} 40 priority_queue qi1; 41 priority_queue , greater qi2; 42 /**************** * ****** Gorgeous split line. The preceding section is the template section *******************/43 const int M = 100100; 44 struct tree 45 {46 int sum; // sum of 47 int lazy, tag; 48 int left, right; // records the coordinates of the left and right closed intervals of the interval 49} node [M * 4]; // The space size to be quadrupled 50 int P [m]; 51 int CNT; 52 void build _ tree (int id, int L, int R) // build 53 {54 int mid = (L + r)/2; 55 node [ID]. left = L; // initialize the coordinate 56 node [ID] of the value range. right = r; 57 node [ID]. tag = 1; 58 node [ID]. lazy = 0; // lazy initialize 59 If (L = r) 60 {6 1 node [ID]. sum = 1; // The Val value of the initialization interval node 62 Return; 63} 64 build _ tree (ID * 2, L, mid ); // left child 65 build _ tree (ID * 2 + 1, Mid + 1, R); // right child 66 node [ID]. sum = node [ID * 2]. sum + node [ID * 2 + 1]. SUM; // recursively update each interval node Val 67} 68 void updata (int id, int L, int R, int V) // Interval Update 69 {70 int mid = (node [ID]. left + node [ID]. right)/2; 71 If (node [ID]. left = L & node [ID]. right = r) 72 {73 node [ID]. lazy = 1; 74 node [ID]. tag = V; 75 node [ID]. sum = (R-l + 1) * V; 76 return; 77} 78 If (node [ID]. lazy = 1) 79 {80 node [ID]. lazy = 0; 81 updata (ID * 2, node [ID]. left, mid, node [ID]. tag); 82 updata (ID * 2 + 1, Mid + 1, node [ID]. right, node [ID]. tag); 83} 84 If (L> mid) // [L, R] In the right child section 85 updata (ID * 2 + 1, L, R, V ); 86 else if (r <= mid) // [L, R] in the left part of the child 87 updata (ID * 2, L, R, V ); 88 else // [L, R] intervals. The left and right children have recursion respectively. 89 {90 updata (ID * 2, L, mid, V); 91 updata (ID * 2 + 1, Mid + 1, R, V ); 92} 93 node [ID]. sum = node [ID * 2]. sum + node [ID * 2 + 1]. SUM; // recursively update each interval node Val 94} 95 96 int main () 97 {98 int CAS; 99 scanf ("% d", & CAS ); 100 int ICAs = 1; 101 while (CAS --) 102 {103 int N, Q; 104 scanf ("% d", & N, & Q ); 105 build _ tree (106, n); 107 int x, y, z; 108 while (Q --) 109 {scanf ("% d ", & X, & Y, & Z); 110 updata (1, x, y, z); 111} 112 printf ("case % d: the total value of the hook is % d. \ n ", ICAs ++, node [1]. sum); 113} 114 return 0; 115}
View code

Hdu1689 (just a hook)

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.