zoukankan      html  css  js  c++  java
  • Transformation HDU

    Yuanfang is puzzled with the question below: 
    There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations. 
    Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<---a k+c, k = x,x+1,…,y. 
    Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<---a k×c, k = x,x+1,…,y. 
    Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<---c, k = x,x+1,…,y. 
    Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y p
    Yuanfang has no idea of how to do it. So he wants to ask you to help him. 

    InputThere are no more than 10 test cases. 
    For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000. 
    Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3) 
    The input ends with 0 0. 
    OutputFor each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.Sample Input

    5 5
    3 3 5 7
    1 2 4 4
    4 1 5 2
    2 2 5 8
    4 3 5 3
    0 0

    Sample Output

    307
    7489

    分析;本题极其巧妙地运用了线段树懒惰标记的功能,由于本题的特殊性,一开始每个元素都是相等的,而且操作也是对于区间进行的,这就会导致很多相同的元素挨在一起,形成一个个区间,为懒惰标记发挥作用和批量操作创造条件;
    flag(懒惰标记)表示该区间的元素是否是相同的,v表示这个区间里面元素的值;
    然后再通过合理的pushdown和pushup操作便可以快速的实现对于区间的统计工作。这比网上其他的思路(统计sum1,sum2, sum3.....)快很多。

    代码:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5 + 10;
     4 const int mod = 10007;
     5 struct node
     6 {
     7     int l, r;
     8     int v;
     9     bool flag;
    10 }t[maxn << 2];
    11 int n, m;
    12 
    13 void pushup(int tar)
    14 {
    15     if (t[tar << 1].flag && t[tar << 1 | 1].flag && t[tar << 1].v == t[tar << 1 | 1].v)
    16         t[tar].flag = true, t[tar].v = t[tar << 1].v;
    17     else t[tar].flag = false;
    18 }
    19 
    20 void pushdown(int tar)
    21 {
    22     if (t[tar].flag)
    23     {
    24         t[tar << 1].flag = t[tar << 1 | 1].flag = true;
    25         t[tar << 1].v = t[tar << 1 | 1].v = t[tar].v;
    26         t[tar].flag = false;
    27     }
    28 }
    29 
    30 void build(int l, int r, int tar)
    31 {
    32     t[tar].l = l, t[tar].r = r, t[tar].v = 0, t[tar].flag = true;
    33     if (l == r) return;
    34     int mid = (l + r) >> 1;
    35     build(l, mid, tar << 1);
    36     build(mid + 1, r, tar << 1 | 1);
    37 }
    38 
    39 void update(int ope, int l, int r, int v, int tar)
    40 {
    41     if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
    42     {
    43         if (ope == 1) t[tar].v += v;
    44         else if (ope == 2) t[tar].v *= v;
    45         else t[tar].v = v;
    46         t[tar].v %= mod;
    47         return;
    48     }
    49     pushdown(tar);
    50     int mid = (t[tar].l + t[tar].r) >> 1;
    51     if (r <= mid) update(ope, l, r, v, tar << 1);
    52     else if (l > mid) update(ope, l, r, v, tar << 1 | 1);
    53     else update(ope, l, mid, v, tar << 1), update(ope, mid + 1, r, v, tar << 1 | 1);
    54     pushup(tar);
    55 }
    56 
    57 int query(int l, int r, int v, int tar)
    58 {
    59     if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
    60     {
    61         int res = 1;
    62         for (int i = 0; i < v; i++)
    63             res *= t[tar].v, res %= mod;
    64         return res * (t[tar].r - t[tar].l + 1) % mod;
    65     }
    66     pushdown(tar);
    67     int mid = (t[tar].l + t[tar].r) >> 1;
    68     if (r <= mid) return query(l, r, v, tar << 1);
    69     else if (l > mid) return query(l, r, v, tar << 1 | 1);
    70     else return (query(l, mid, v, tar << 1) + query(mid + 1, r, v, tar << 1 | 1)) % mod;
    71 }
    72 
    73 int main()
    74 {
    75     int ope, x, y, c;
    76 
    77     while (cin >> n >> m && n && m)
    78     {
    79         build(1, n, 1);
    80         while (m--)
    81         {
    82             scanf("%d%d%d%d", &ope, &x, &y, &c);
    83             if (ope != 4)
    84                 update(ope, x, y, c, 1);
    85             else cout << query(x, y, c, 1) << endl;
    86         }
    87     }
    88 }
  • 相关阅读:
    使用keras构建简单的网络分类鸢尾花
    矩阵的秩 rank(A)
    矩阵的迹
    数学符号大全速查表
    迷茫的不是青春,是你们回望青春时失焦的眼神。
    服务器Windows Server 2008 远程控制安全设置技巧
    服务器安全维护配置和优化八大要点
    怎么把html页面中共用的底部代码做成共享模块
    回首2017,展望2018,今后的路我们一起走
    手机端rem如何适配_rem详解及使用方法2
  • 原文地址:https://www.cnblogs.com/liuwenhan/p/11342978.html
Copyright © 2011-2022 走看看