hdu 1698 Just a Hook

来源:互联网 发布:js让多个功能延迟执行 编辑:程序博客网 时间:2024/06/02 07:30

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 up of several consecutive metallic sticks which are of the same length.

这里写图片描述

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks 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 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.
You may consider the original hook is made up of cupreous sticks.

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an 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: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output
For each case, print a number in 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 Output

Case 1: The total value of the hook is 24.


【分析】
线段树
区间打懒标记,注意初始化的memset


【代码】

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio> #define ll long long#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100005;struct node {int l,r,sum,col;} t[mxn*4];int T,n,m,L,R,w;inline void build(int num,int l,int r){    t[num].l=l,t[num].r=r;    t[num].col=0;    if(l==r)    {        t[num].sum=1;        return;    }    int mid=(l+r)>>1;    build(num<<1,l,mid);    build((num<<1)+1,mid+1,r);    t[num].sum=t[num<<1].sum+t[(num<<1)+1].sum;}inline void pushdown(int num){    if(t[num].col)    {        t[num<<1].col=t[(num<<1)+1].col=t[num].col;        t[num<<1].sum=(t[num<<1].r-t[num<<1].l+1)*t[num].col;        t[(num<<1)+1].sum=(t[(num<<1)+1].r-t[(num<<1)+1].l+1)*t[num].col;        t[num].col=0;    }}inline void change(int num){    if(L<=t[num].l && t[num].r<=R)    {        t[num].col=w;        t[num].sum=(t[num].r-t[num].l+1)*w;        return;    }    pushdown(num);    int mid=(t[num].l+t[num].r)>>1;    if(L<=mid) change(num<<1);    if(mid<R) change((num<<1)+1);    t[num].sum=t[num<<1].sum+t[(num<<1)+1].sum;}int main(){    int i,j,k;    scanf("%d",&T);    fo(k,1,T)    {        scanf("%d%d",&n,&m);        build(1,1,n);        while(m--)        {            scanf("%d%d%d",&L,&R,&w);            change(1);        }        printf("Case %d: The total value of the hook is %d.\n",k,t[1].sum);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 没发货申请退款卖家不处理怎么办 淘宝卖家帐号被骗了怎么办 淘宝网下单忘了用返利网怎么办 该地域无法观看此直播怎么办 宝宝喝了有活虫的奶粉怎么办 淘宝商家店铺状态异常怎么办 淘宝购物提示买家信息错误怎么办 苹果淘宝占用空间大怎么办 苹果手机淘宝占内存太大怎么办 苹果手机淘宝图标找不到了怎么办 苹果手机看淘宝很卡怎么办 苹果手机淘宝忘了密码怎么办 苹果手机更新后淘宝打不开怎么办 淘宝买到苹果翻新机怎么办 淘宝网密码忘了怎么办 淘宝改密码要拍摄脸部怎么办 苹果一体机键盘没反应怎么办 淘宝买东西退货卖家拒绝怎么办? 淘宝被限制下单怎么办 淘宝扫码登录后怎么办 花呗选项被隐藏怎么办 新换手机支付宝怎么办 ih5点击按钮跳转页面怎么办 sap点安装程序没反应怎么办 淘宝店账号忘了怎么办 点击电脑桌面图标没反应怎么办 为什么淘宝打不开已停止运行怎么办 移动宽带打不开淘宝网怎么办 淘宝发布宝贝没有品牌怎么办 烫了卷发显老怎么办 唯品会商品不支持退换货怎么办 京东没有自提怎么办 不支持七天无理由的商品怎么办 淘宝评价忘记点亮星星怎么办 淘宝宝贝权重降低了该怎么办 换卡支付宝账号怎么办 支付宝免密支付无法关闭怎么办 手机换了支付宝怎么办 支付宝版本过低怎么办 支付宝无故扣钱怎么办 支付宝里面的钱不见了怎么办