1820-Just a Hook(线段树区间延时更新)

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.

题意:一段长为n的链子,由n块金属连接而成,每块金属可能是金银铜三种材质。每种材质有一定价值。现在给出m次更新,每次更新对【L,R】区间内的金属更新成金->3 银->2 铜->1三种材质,最后输出整条链子的总价值。

线段树区间更新、区间求和模板题。注意此处的更新时直接赋值而不是加加减减。
记住如果Mark延时标记如果放结构体中一定要在一开始赋值l和r的建树部分初始化。否则后果很严重。如果单独放mark值也要记得memset清空。

#include<stdio.h>
#include<string.h>
const int N = 100010;
int n,m,k;
struct node
{
    int l,r,sum;
    int mark;
} tree[N<<2];

void build(int rt, int l, int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].mark=0;///注意mark延时标记的初始化位置
    if(l==r)
    {
        tree[rt].sum=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    return ;
}

void pushdown(int rt)
{
    if(tree[rt].mark)
    {
        tree[rt<<1|1].sum=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*tree[rt].mark;///这里的push_down是直接赋值,而非增加
        tree[rt<<1].sum=(tree[rt<<1].r-tree[rt<<1].l+1)*tree[rt].mark;
        tree[rt<<1|1].mark=tree[rt].mark;
        tree[rt<<1].mark=tree[rt].mark;
        tree[rt].mark=0;
    }
    return ;
}
void update(int rt, int l, int r, int val)
{
    if(tree[rt].l>=l&&tree[rt].r<=r)
    {
        tree[rt].sum=(tree[rt].r-tree[rt].l+1)*val;
        tree[rt].mark=val;
        return ;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(l<=mid) update(rt<<1,l,r,val);
    if(r>mid) update(rt<<1|1,l,r,val);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    return ;
}

int query(int rt, int l, int r)
{
    int ans=0;
    if(tree[rt].l>=l&&tree[rt].r<=r) return tree[rt].sum;
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(l<=mid) ans+=query(rt<<1,l,r);
    if(r>mid) ans+=query(rt<<1|1,l,r);
    return ans;
}

int main()
{
    int ca=0,t,l,r,val;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--)
        {
            scanf("%d%d%d",&l,&r,&val);
            update(1,l,r,val);
        }
        printf("Case %d: The total value of the hook is %d.\n",++ca,query(1,1,n));
    }
    return 0;
}
CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值