HDU 1698 just a hook segment tree, Interval setpoint, sum

Source: Internet
Author: User

Just a Hook

Time limit:1 Sec Memory limit:256 MB

Topic Connection

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

DescriptionIn the game of DotA, Pudge ' s meat hook is actually the most horrible thing for the very 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 each case, print a number with a line representing the total value of the hook after the operations. use the format in the example.Sample Input
1
10
2
1 5 2
5 9 3

Sample outputcase 1:the Total value of the hook is 24.
HINT

Test instructions

Interval update to the fixed value, then ask you the interval and for how much

Exercises

Ah, line tree and lazy operation, registration

Code:

#include <stdio.h>#include<string.h>Const intMAXN =100010;intsum[maxn<<2];intlazy[maxn<<2];voidPushup (intRT) {Sum[rt]= sum[rt<<1] + sum[rt<<1|1];}voidPushdown (intRtintx) {    if(LAZY[RT]! =-1) {Lazy[rt<<1] = lazy[rt<<1|1] =Lazy[rt]; Sum[rt<<1] = (x (x>>1)) *lazy[rt];///!!!sum[rt<<1|1] = (x>>1) *LAZY[RT];///!!!LAZY[RT] =-1; }}voidCreat (intLintRintRT) {Lazy[rt]= -1, Sum[rt] =1; if(L = = r)return; intMid = (l+r) >>1; Creat (L, Mid, RT<<1); Creat (Mid+1, R, rt<<1|1); Pushup (RT);}voidModifyintLintRintXintLintRintRT) {    if(l <= l && R >=R) {Lazy[rt]=x; SUM[RT]= x* (r-l+1);///!!!        return; } pushdown (RT, R-l+1);///!!!    intMid = (l+r) >>1; if(L <= mid) modify (L, R, X, L, Mid, rt<<1); if(R > Mid) Modify (L, R, X, mid+1, R, rt<<1|1); Pushup (RT);}intMain () {intI, j, k =0; intN, T, q; intx, Y, W;  while(SCANF ("%d", &t)! =EOF) while(t--) {scanf ("%d%d", &n, &q); Creat (1N1);  while(q--) {scanf (" %d%d%d", &x, &y, &W); Modify (x, Y, W,1N1); } printf ("Case%d:the Total value of the hook is%d.\n", ++k, sum[1]); }    return 0;}

HDU 1698 just a hook segment tree, Interval setpoint, sum

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.