• HDU 1698 区间更新


    Just a Hook

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


    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
     
     
    题目意思:
    给一个长度为n的数组,q次操作,每次操作为l, r, w   即把l-r区间数组元素变为w,初始化数组所有元素都为1
     
    思路:
    线段树区间更新,记录颜色
     
    代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <vector>
     6 #include <queue>
     7 #include <cmath>
     8 #include <set>
     9 using namespace std;
    10 
    11 #define N 100005
    12 #define ll root<<1
    13 #define rr root<<1|1
    14 #define mid (a[root].l+a[root].r)/2
    15 
    16 
    17 int max(int x,int y){return x>y?x:y;}
    18 int min(int x,int y){return x<y?x:y;}
    19 int abs(int x,int y){return x<0?-x:x;}
    20 
    21 int n;
    22 
    23 struct node{
    24     int l, r, val;
    25     bool f;
    26 }a[N*4];
    27 
    28 void build(int l,int r,int root){
    29     a[root].l=l;
    30     a[root].r=r;
    31     a[root].val=1;
    32     if(l==r){
    33         return;
    34     }
    35     build(l,mid,ll);
    36     build(mid+1,r,rr);
    37 }
    38 
    39 void update(int l,int r,int val,int root){
    40     if(a[root].val==val) return;
    41     if(a[root].l==l&&a[root].r==r){
    42         a[root].val=val;
    43         return;
    44     }
    45     if(a[root].val){
    46         if(a[root].l!=a[root].r) {
    47             a[ll].val=a[rr].val=a[root].val;
    48         }
    49     }
    50     if(l>=a[rr].l) update(l,r,val,rr);
    51     else if(r<=a[ll].r) update(l,r,val,ll);
    52     else {
    53         update(l,mid,val,ll);
    54         update(mid+1,r,val,rr);
    55     }
    56     if(a[ll].val==a[rr].val) a[root].val=a[ll].val;
    57     else a[root].val=0;
    58 }
    59 
    60 int get_sum(int root){
    61     if(a[root].val) return a[root].val*(a[root].r-a[root].l+1);
    62     return get_sum(ll)+get_sum(rr);
    63 }
    64 
    65 main()
    66 {
    67     int q, i, j, k, x, y, z;
    68     int t;
    69     cin>>t;
    70     for(k=1;k<=t;k++){
    71         scanf("%d %d",&n,&q);
    72         build(1,n,1);
    73         while(q--){
    74             scanf("%d %d %d",&x,&y,&z);
    75             update(x,y,z,1);
    76         }
    77         printf("Case %d: The total value of the hook is %d.
    ",k,get_sum(1));
    78         
    79     }
    80 }
  • 相关阅读:
    数据库数据实时采集Maxwell
    消息中间件之Kafka相关知识
    linux安装软件配置
    ETL工具Sqoop
    Hadoop 概述(三)
    全网最全的权限系统设计方案(图解)
    WebSocket 是什么原理?为什么可以实现持久连接
    封装 axios 拦截器实现用户无感刷新 access_token
    明明加了唯一索引,为什么还是产生了重复数据?
    动态组件和插槽
  • 原文地址:https://www.cnblogs.com/qq1012662902/p/4531693.html
Copyright © 2020-2023  润新知