• hdu 1698 Just a Hook (线段树)


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

    Just a Hook

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

    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.
     
    【题解】: 区间的更新,线段树
    【code】:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<algorithm>
     4 #include<string.h>
     5 #define N 100010
     6 using namespace std;
     7 
     8 struct Nod
     9 {
    10     int l,r;  //左右
    11     int flag;  //标记是不是同一颜色
    12     int c,v;   //c是颜色,v区间长度
    13 }node[N<<2];
    14 
    15 void building(int l,int r,int p)
    16 {
    17     node[p].l = l;
    18     node[p].r = r;
    19     node[p].flag = 1;
    20     node[p].c = 1;
    21     node[p].v = r-l+1;
    22     if(l==r)    return;
    23     int mid = (l+r)>>1;
    24     building(l,mid,p<<1);
    25     building(mid+1,r,p<<1|1);
    26 }
    27 
    28 void update(int l,int r,int p,int c)
    29 {
    30     if(node[p].l==l&&node[p].r==r)
    31     {
    32         node[p].c = c;
    33         node[p].flag = 1;
    34         return;
    35     }
    36     if(node[p].c!=c&&node[p].flag)
    37     {
    38         node[p].flag = 0;
    39         node[p<<1|1].flag=node[p<<1].flag = 1;
    40         node[p<<1|1].c=node[p<<1].c = node[p].c;
    41     }
    42     int mid = (node[p].l+node[p].r)>>1;
    43     if(r<=mid)  update(l,r,p<<1,c);
    44     else if(l>mid)  update(l,r,p<<1|1,c);
    45     else
    46     {
    47         update(l,mid,p<<1,c);
    48         update(mid+1,r,p<<1|1,c);
    49     }
    50     //合并:颜色相同
    51     if(node[p<<1].c==node[p<<1|1].c&&node[p<<1].flag&&node[p<<1|1].flag)
    52     {
    53         node[p].flag = 1;
    54         node[p].c = node[p<<1].c;
    55     }
    56 }
    57 
    58 int cal_dfs(int p)  //最后的查询
    59 {
    60     if(node[p].flag==1)
    61     {
    62         return node[p].c*node[p].v;
    63     }
    64     return cal_dfs(p<<1)+cal_dfs(p<<1|1);
    65 }
    66 
    67 int main()
    68 {
    69     int t,cas;
    70     scanf("%d",&t);
    71     for(cas=1;cas<=t;cas++)
    72     {
    73         int n,q;
    74         scanf("%d%d",&n,&q);
    75         building(1,n,1);
    76         while(q--)
    77         {
    78             int x,y,z;
    79             scanf("%d%d%d",&x,&y,&z);
    80             update(x,y,1,z);
    81         }
    82         printf("Case %d: The total value of the hook is %d.
    ",cas,cal_dfs(1));
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    linux 的iptables失效解决方法
    解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误
    HTTP 请求中的 Form Data 与 Request Payload 的区别
    Git本地初始化并推送到远程仓库
    git 提交大小超过100M
    Linux压缩解压
    关闭seLinux
    MAT内存分析
    JVM&GC
    IDEA 好用的插件
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3230312.html
Copyright © 2020-2023  润新知