HDU 1698 Just a Hook (segment tree, segment update, lazy tag)

Source: Internet
Author: User

Just a HookTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 18384 Accepted Submission (s): 9217


Problem DescriptionIn 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
Recommendwangye | We have carefully selected several similar problems for you:1542 1255 1828 1540 2871
Test instructions: Give you an interval of 1~n, just start all 1, after the Q operation, each operation gives X, Y, Z, indicates that the interval [x, y] all the number of changes to Z. Q The number of intervals after the operation is calculated.
Puzzle: Simple line segment tree, update q times, query once, lazy mark.
CODE:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1# Define Rson mid+1,r,idx<<1|1typedef Long long ll;const int INF = 1000010;using namespace Std;int add[n << 2],        Sum[n << 2];void pushdown (int idx, int m) {if (Add[idx]) {add[idx << 1] = Add[idx];        Add[idx << 1 | 1] = ADD[IDX];        Sum[idx << 1] = (M-(M >> 1)) * ADD[IDX];        Sum[idx << 1 | 1] = (M >> 1) * ADD[IDX];    ADD[IDX] = 0;    }}void Build (int l, int r, int idx) {Add[idx] = 0;    if (L = = r) {Sum[idx] = 1;///initialized to 1 return;    } int mid = (L + r) >> 1;    Build (Lson);    Build (Rson); SUM[IDX] = Sum[idx << 1] + SUm[idx << 1 | 1];} void Update (int l, int r, int idx, int x, int y, int k) {if (x <= l && r <= y) {Sum[idx]        = (r-l + 1) * k;        ADD[IDX] = k;    Return    } pushdown (idx, r-l + 1);    int mid = (L + r) >> 1;    if (x <= mid) Update (Lson, x, Y, k);    if (Y > Mid) Update (Rson, x, Y, k); SUM[IDX] = Sum[idx << 1] + sum[idx << 1 | 1];}    int main () {int T, n, M; while (~SCANF ("%d", &t)) {for (int i = 1; I <= t; i++) {scanf ("%d%d", &n            , &m);            Build (1, N, 1);            int x, y, Z;                while (m--) {scanf ("%d%d%d", &x, &y, &z);            Update (1, N, 1, x, y, z);        } printf ("Case%d:the total value of the hook is%d.\n", I, sum[1]); }} return 0;}


HDU 1698 Just a Hook (segment tree, segment update, lazy tag)

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.