zoukankan      html  css  js  c++  java
  • HDU 1698 Just a Hook(线段树)

    Just a Hook

    题目链接

    Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 34312 Accepted Submission(s): 16747

    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.

    Source
    2008 “Sunline Cup” National Invitational Contest

    线段树 区间修改。 {{{(>_<)}}}

    
    
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node
    {
        int l,r,te;
    }as[100010<<2];
    int n;
    void buildd(int left,int right,int num)
    {
        as[num].l=left;
        as[num].r=right;
        as[num].te=1;
        if(left!=right)
        {
            int mid=(left+right)>>1;
            buildd(left,mid,num<<1);
            buildd(mid+1,right,num<<1|1);
        }
    }
    void update(int x,int y,int modify,int num)
    {
        if(as[num].te==modify)//与要修改的在相同就不用先修改了
            return ;
        if(as[num].l==x&&as[num].r==y)//知道区间,总结修改
        {
            as[num].te=modify;
            return ;
        }
        if(as[num].te!=-1)//如果区间只有一个颜色
        {
            as[num<<1].te=as[num<<1|1].te=as[num].te;//由于后面必定对子树进行修改,修改子树等于父树
            as[num].te=-1;//由于该区域颜色与修改不同,而且不是给定区域,所以该区域必定为杂色 
        }
        int mid=(as[num].l+as[num].r)>>1;//对子树进行修改
        if(x>mid) update(x,y,modify,num<<1|1);
        else if(y<=mid) update(x,y,modify,num<<1);
        else
        {
            update(x,mid,modify,num*2);
            update(mid+1,y,modify,num<<1|1);
        }
    }
    int summ(int num)
    {
        if(as[num].te!=-1)
            return (as[num].r-as[num].l+1)*as[num].te;
        else
            return summ(num<<1)+summ(num<<1|1);
    }
    int main()
    {
        int t,m;
        scanf("%d",&t);
        int num=1;
        int x1,y1,z1;
        while(t--)
        {
            scanf("%d%d",&n,&m);
            buildd(1,n,1);
            while(m--)
            {
                scanf("%d%d%d",&x1,&y1,&z1);
                update(x1,y1,z1,1);
            }
            printf("Case %d: The total value of the hook is %d.
    ",num++,summ(1));
        }
        return 0;
    }
    
    
  • 相关阅读:
    XHTML学习笔记 Part3:核心属性
    XHTML学习笔记 Part2:核心元素
    XHTML学习笔记 part1
    北航非全日制-软件学院考研攻略(经验仅来自于2019年,2020年招生简章有变动,需谨慎)
    为什么能抓到网站https传输的明文密码?------顺便说说“知乎”和“支付宝”的安全性对比
    JetBrain系列学生免费授权
    印象笔记模板推荐使用
    测试用例评审总结与规范
    Django入门
    Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900069.html
Copyright © 2011-2022 走看看