zoukankan      html  css  js  c++  java
  • CodeForces

    F - DZY Loves Colors

    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.

    Examples
    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
    Note

    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.

      1 #include <cstdio>
      2 #include <stack>
      3 #include <cmath>
      4 #include <queue>
      5 #include <string>
      6 #include <queue>
      7 #include <cstring>
      8 #include <iostream>
      9 #include <algorithm>
     10 
     11 #define lid id<<1
     12 #define rid id<<1|1
     13 #define closein cin.tie(0)
     14 #define scac(a) scanf("%c",&a)
     15 #define scad(a) scanf("%d",&a)
     16 //#define print(a) printf("%d
    ",a)
     17 #define debug printf("hello world")
     18 #define form(i,n,m) for(int i=n;i<m;i++)
     19 #define mfor(i,n,m) for(int i=n;i>m;i--)
     20 #define nfor(i,n,m) for(int i=n;i>=m;i--)
     21 #define forn(i,n,m) for(int i=n;i<=m;i++)
     22 #define scadd(a,b) scanf("%d%d",&a,&b)
     23 #define memset0(a) memset(a,0,sizeof(a))
     24 #define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
     25 #define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
     26 
     27 #define INF 0x3f3f3f3f
     28 #define maxn 100005
     29 typedef long long ll;
     30 using namespace std;
     31 //---------AC(^-^)AC---------\
     32 
     33 int n,m,block,num;
     34 int blo[maxn],l[maxn],r[maxn];
     35 ll color[maxn],sum[maxn],allsum[maxn],flag2[maxn],flag[maxn];
     36 
     37 void print()
     38 {
     39     forn(i,1,num)
     40     {
     41         printf("%lld %lld
    ",allsum[i],flag2[i]);
     42     }
     43     debug;
     44     printf("
    ");
     45     forn(i,1,n)
     46         printf("%lld %lld
    ",color[i],sum[i]);
     47 }
     48 void push_down(int id)
     49 {
     50     forn(i,l[id],r[id]) color[i]=flag[id];
     51     flag[id]=-1;
     52 }
     53 void update(int ld,int rd,int add)
     54 {
     55     if(blo[ld]==blo[rd])
     56     {
     57         if(flag[blo[ld]]!=-1)    push_down(blo[ld]);
     58         forn(i,ld,rd)    {
     59             sum[i]+=abs(color[i]-add);
     60             allsum[blo[ld]]+=abs(color[i]-add);
     61             color[i]=add;
     62         }
     63     }else {
     64         if(flag[blo[ld]]!=-1) push_down(blo[ld]);
     65         forn(i,ld,r[blo[ld]]) {
     66             sum[i]+=abs(color[i]-add);
     67             allsum[blo[ld]]+=abs(color[i]-add);
     68             color[i]=add;
     69         }
     70         forn(i,blo[ld]+1,blo[rd]-1)    {
     71             if(flag[i]!=-1)    {
     72                 allsum[i]+=abs(flag[i]-add)*(r[i]-l[i]+1);
     73                 flag2[i]+=abs(flag[i]-add);
     74                 flag[i]=add;
     75             }else {
     76                 forn(j,l[i],r[i])    {
     77                     sum[j]+=abs(color[j]-add);
     78                     allsum[i]+=abs(color[j]-add);
     79                     color[j]=add;
     80                 }
     81                 flag[i]=add;
     82             }
     83         }
     84         if(flag[blo[rd]]!=-1) push_down(blo[rd]);
     85         forn(i,l[blo[rd]],rd)    {
     86             sum[i]+=abs(color[i]-add);
     87             allsum[blo[rd]]+=abs(color[i]-add);
     88             color[i]=add;
     89         }
     90     }
     91     //print();
     92 }
     93 void query(int ld,int rd)
     94 {
     95     ll ans=0;
     96     if(blo[ld]==blo[rd])    {
     97         forn(i,ld,rd) ans+=sum[i]+flag2[blo[i]];
     98     }else {
     99         forn(i,ld,r[blo[ld]])    {
    100             ans+=sum[i]+flag2[blo[i]];
    101         }
    102         forn(i,blo[ld]+1,blo[rd]-1)    {
    103             ans+=allsum[i];
    104         }
    105         forn(i,l[blo[rd]],rd)    {
    106             ans+=sum[i]+flag2[blo[i]];
    107         }
    108     }
    109     printf("%lld
    ",ans);
    110 }
    111 
    112 int main()
    113 {
    114     scadd(n,m);
    115     block=sqrt(n);
    116     num=(n-1)/block+1;
    117     forn(i,1,n)    {
    118         color[i]=i;
    119         blo[i]=(i-1)/block+1;
    120     }
    121     forn(i,1,num) {
    122         l[i]=(i-1)*block+1;
    123         r[i]=i*block;
    124     }
    125     r[num]=n;
    126     forn(i,1,num) flag[i]=-1;
    127     while(m--)
    128     {
    129         int op,l,r;
    130         scaddd(op,l,r);
    131         if(op==1)
    132         {
    133             int x;
    134             scad(x);
    135             update(l,r,x);
    136         }
    137         else
    138         {
    139             query(l,r);
    140             //print();
    141         }
    142     }
    143     return 0;
    144 }
  • 相关阅读:
    JAVA 8的新特性
    JAVA中map的分类和各自的特性
    设计模式之工厂方法模式(附实例代码下载)
    为什么重写equals还要重写hashcode??
    C# static的用法详解
    1-RadioButton控件的用法
    C#三种常用的读取XML文件的方法
    VS2017 中安装SVN
    pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题
    问题:unknown error: call function result missing 'value' 解决方法
  • 原文地址:https://www.cnblogs.com/mile-star/p/10518399.html
Copyright © 2011-2022 走看看