Just a Hook
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 21856 Accepted Submission (s): 10963
Problem Description
In the game of DotA, Pudge's Meat hook is actually the most horrible thing for most of the heroes. The hook is made to several consecutive metallic sticks which is of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hooks from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver s Ticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each of the silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
Consider the original hook is a made up of cupreous sticks.
Inputthe input consists of several test cases. The first line of the input is the number of the cases. There is no more than cases.
For each case, the first line contains a integer N, 1<=n<=100,000, which is the number of the sticks of Pudge ' s MEA T Hook and the second line contains a integer Q, 0<=q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=x<=y<=n, Z, 1<=z<=3, which defines an operation:c Hange the sticks numbered from X to Y into the metal kind Z, where z=1 represents the Cupreous kind, z=2 represents the SI Lver Kind and z=3 represents the golden kind.
Outputfor, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
11021 5 25) 9 3
Sample Output
Case 1:the total value of the hook is 24.
Source2008 "Sunline Cup" National Invitational Contest
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1698
The main idea: The starting interval 1-n value is 1,x y z means the values of the interval [x, y] are changed to Z, the last output interval and
Topic Analysis: The basis of line segment tree interval update
#include <cstdio> #include <cstring> #include <algorithm> #define Lson L, Mid, Rt << 1#define Rson m ID + 1, r, RT << 1 | 1int Const MAX = 100005;int sum[4 * Max], lazy[4 * max];int n;void pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[r T << 1 | 1];} void pushdown (int l, int r, int rt) {if (Lazy[rt]) {int mid = (L + r) >> 1; Lazy[rt << 1] = Lazy[rt]; Lazy[rt << 1 | 1] = LAZY[RT]; Sum[rt << 1] = lazy[rt] * (mid-l + 1); Sum[rt << 1 | 1] = lazy[rt] * (R-MID); LAZY[RT] = 0; }}void Build (int l, int r, int rt) {Lazy[rt] = 0; if (L = = r) {Sum[rt] = 1; Return } int mid = (L + r) >> 1; Build (Lson); Build (Rson); Pushup (RT);} void Update (int l, int r, int c, int l, int r, int rt) {if (R < l | | l > R) return; if (l <= l && R <= R) {Lazy[rt] = C; SUM[RT] = c * (r-l + 1); ReturN } pushdown (L, R, RT); int mid = (L + r) >> 1; if (l <= mid) Update (L, R, C, Lson); if (R > Mid) Update (L, R, C, Rson); Pushup (RT);} int main () {int T; scanf ("%d", &t); for (int CA = 1; CA <= T; ca++) {scanf ("%d", &n); Build (1, N, 1); int q; scanf ("%d", &q); while (q--) {int L, r, C; scanf ("%d%d%d", &l, &r, &c); Update (L, R, c, 1, N, 1); } printf ("Case%d:the total value of the hook is%d.\n", CA, sum[1]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1698 Just a Hook (segment tree interval update base)