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

    Just a Hook

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


    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.
     
     
     
     
    //线段树的lazy应用,不难,题意:刚开始 1 -- n 的钩子价值都为 1 ,q 次操作,将 [l,r] 变为 z 价值,最后问总和
     1 # include <cstdio>
     2 # include <cstring>
     3 # include <cstdlib>
     4 # include <iostream>
     5 # include <vector>
     6 # include <queue>
     7 # include <stack>
     8 # include <map>
     9 # include <bitset>
    10 # include <sstream>
    11 # include <set>
    12 # include <cmath>
    13 # include <algorithm>
    14 using namespace std;
    15 #define INF 0x3f3f3f3f
    16 #define LL long long
    17 #define MX 100005
    18 struct Node
    19 {
    20     int l,r;
    21     int lazy,sum;
    22 }tree[MX*4];
    23 
    24 int n, q;
    25 
    26 void build(int l,int r,int k)
    27 {
    28     tree[k]=(Node){l,r,0,0};
    29     if (l==r)
    30     {
    31         tree[k].sum = 1;
    32         return;
    33     }
    34     int mid = (l+r)>>1;
    35     build(l,mid,2*k); build(mid+1,r,2*k+1);
    36     tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
    37 }
    38 
    39 void push_down(int k)
    40 {
    41     int l=tree[k].l, r=tree[k].r;
    42     int mid = (l+r)>>1, z = tree[k].lazy;
    43     if (l==r||z==0) return;
    44     tree[2*k].lazy=z;  tree[2*k].sum=z*(mid-l+1);
    45     tree[2*k+1].lazy=z;  tree[2*k+1].sum=z*(r-mid);
    46     tree[k].lazy=0;
    47 }
    48 
    49 void update(int l,int r,int k,int v)
    50 {
    51     if (l==tree[k].l&&r==tree[k].r)
    52     {
    53         tree[k].lazy=v;
    54         tree[k].sum=v*(r-l+1);
    55         return;
    56     }
    57     push_down(k);
    58     int mid = (tree[k].l+tree[k].r)>>1;
    59     if (r<=mid) update(l,r,2*k,v);
    60     else if (l>mid) update(l,r,2*k+1,v);
    61     else update(l,mid,2*k,v), update(mid+1,r,2*k+1,v);
    62     tree[k].sum = tree[2*k].sum+tree[2*k+1].sum;
    63 }
    64 
    65 int inqy(int l, int r, int k)
    66 {
    67     if (l==tree[k].l&&r==tree[k].r)
    68     {
    69         return tree[k].sum;
    70     }
    71     push_down(k);
    72     int mid = (tree[k].l+tree[k].r)>>1;
    73     if (r<=mid) return inqy(l,r,2*k);
    74     else if (l>mid) return inqy(l,r,2*k+1);
    75     return inqy(l,mid,2*k) + inqy(mid+1,r,2*k+1);
    76 }
    77 
    78 int main()
    79 {
    80     int t;
    81     scanf("%d",&t);
    82     for (int cas=1;cas<=t;cas++)
    83     {
    84         scanf("%d%d",&n,&q);
    85         build(1,n,1);
    86         while(q--)
    87         {
    88             int l, r, v;
    89             scanf("%d%d%d",&l,&r,&v);
    90             update(l,r,1,v);
    91         }
    92         printf("Case %d: The total value of the hook is %d.
    ",cas,inqy(1,n,1));
    93     }
    94     return 0;
    95 }
    View Code
  • 相关阅读:
    Windows Terminal 配色方案说明
    Windows Terminal 美化简记
    家庭组网启用 NAS
    Eclipse使用Git检出项目
    SVN clean失败解决方法
    httpClient closeableHttpClient
    JSON字符串与Map互转
    httpclient用getStatusCode
    同一台电脑同时装jdk1.8和jdk1.7
    PL/SQL连接远程oracle数据库配置
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6087152.html
Copyright © 2011-2022 走看看