zoukankan      html  css  js  c++  java
  • CodeForces 445E DZY Loves Colors

    DZY Loves Colors

    Time Limit: 2000ms
    Memory Limit: 262144KB
    This problem will be judged on CodeForces. Original ID: 445E
    64-bit integer IO format: %I64d      Java class name: (Any)
    DZY loves colors, and he enjoys painting.

    On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.

    DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.

    DZY wants to perform m operations, each operation can be one of the following:

    1. Paint all the units with numbers between l and r (both inclusive) with color x.
    2. Ask the sum of colorfulness of the units between l and r (both inclusive).

    Can you help DZY?

    Input

    The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).

    Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation.

    If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1.

    If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

     

    Output

    For each operation 2, print a line containing the answer — sum of colorfulness.

     

    Sample Input

    Input
    3 3
    1 1 2 4
    1 2 3 5
    2 1 3
    Output
    8
    Input
    3 4
    1 1 3 4
    2 1 1
    2 2 2
    2 3 3
    Output
    3
    2
    1
    Input
    10 6
    1 1 5 3
    1 2 7 9
    1 10 10 11
    1 3 8 12
    1 1 10 3
    2 1 10
    Output
    129

    Hint

    In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].

    After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].

    After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].

    So the answer to the only operation of type 2 is 8.

     

    Source

     
    解题:线段树
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 100010;
     5 struct node{
     6     int lt,rt,color;
     7     LL sum,len,add;
     8 }tree[maxn<<2];
     9 void pushup(int v){
    10     tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum;
    11     if(tree[v<<1].color == tree[v<<1|1].color)
    12         tree[v].color = tree[v<<1].color;
    13     else tree[v].color = 0;
    14 }
    15 void pushdown(int v){
    16     if(tree[v].add) {
    17         tree[v<<1].add += tree[v].add;
    18         tree[v<<1|1].add += tree[v].add;
    19         tree[v<<1].sum += tree[v].add*tree[v<<1].len;
    20         tree[v<<1|1].sum += tree[v].add*tree[v<<1|1].len;
    21         tree[v].add = 0;
    22     }
    23     if(tree[v].color){
    24         tree[v<<1].color = tree[v<<1|1].color = tree[v].color;
    25         tree[v].color = 0;
    26     }
    27 }
    28 void build(int lt,int rt,int v){
    29     tree[v].lt = lt;
    30     tree[v].rt = rt;
    31     tree[v].len = rt - lt + 1;
    32     tree[v].add = 0;
    33     tree[v].sum = 0;
    34     if(lt == rt){
    35         tree[v].color = lt;
    36         return;
    37     }
    38     int mid = (lt + rt)>>1;
    39     build(lt,mid,v<<1);
    40     build(mid+1,rt,v<<1|1);
    41     pushup(v);
    42 }
    43 void update(int lt,int rt,int color,int v){
    44     if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].color){
    45         tree[v].add += abs(tree[v].color - color);
    46         tree[v].sum += abs(tree[v].color - color)*tree[v].len;
    47         tree[v].color = color;
    48         return;
    49     }
    50     pushdown(v);
    51     if(lt <= tree[v<<1].rt) update(lt,rt,color,v<<1);
    52     if(rt >= tree[v<<1|1].lt) update(lt,rt,color,v<<1|1);
    53     pushup(v);
    54 }
    55 LL query(int lt,int rt,int v){
    56     if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].sum;
    57     pushdown(v);
    58     LL sum = 0;
    59     if(lt <= tree[v<<1].rt) sum += query(lt,rt,v<<1);
    60     if(rt >= tree[v<<1|1].lt) sum += query(lt,rt,v<<1|1);
    61     pushup(v);
    62     return sum;
    63 }
    64 int main(){
    65     int n,m,op,x,y,color;
    66     scanf("%d%d",&n,&m);
    67     build(1,n,1);
    68     while(m--){
    69         scanf("%d%d%d",&op,&x,&y);
    70         if(op == 1){
    71             scanf("%d",&color);
    72             update(x,y,color,1);
    73         }else printf("%I64d
    ",query(x,y,1));
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    链式编程思想
    iOS开发:后台运行以及保持程序在后台长时间运行
    iOS懒加载
    mysql命令行的一些小技巧【实用:多屏显示,格式化输出等】
    iOS UITextField实时监听获取输入内容,中文状态去除预输入拼音字符
    'vector' file not found错误解决 && xcode archive 去掉dsym文件和添加dsym文件
    iOS APP 中H5视频默认全屏播放问题解决
    Sublime Text 3 插件的安装、升级和卸载,以及安装package control 出现问题解决过程记录
    ES5和ES6对象导出和导入(转载,待整理)
    react/React Native 在 import 导入时,有的带花括号{},有的不带原理解析
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4691658.html
Copyright © 2011-2022 走看看