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.....)快很多。 代码:
 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
const int mod = ;
struct node
{
int l, r;
int v;
bool flag;
}t[maxn << ];
int n, m; void pushup(int tar)
{
if (t[tar << ].flag && t[tar << | ].flag && t[tar << ].v == t[tar << | ].v)
t[tar].flag = true, t[tar].v = t[tar << ].v;
else t[tar].flag = false;
} void pushdown(int tar)
{
if (t[tar].flag)
{
t[tar << ].flag = t[tar << | ].flag = true;
t[tar << ].v = t[tar << | ].v = t[tar].v;
t[tar].flag = false;
}
} void build(int l, int r, int tar)
{
t[tar].l = l, t[tar].r = r, t[tar].v = , t[tar].flag = true;
if (l == r) return;
int mid = (l + r) >> ;
build(l, mid, tar << );
build(mid + , r, tar << | );
} void update(int ope, int l, int r, int v, int tar)
{
if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
{
if (ope == ) t[tar].v += v;
else if (ope == ) t[tar].v *= v;
else t[tar].v = v;
t[tar].v %= mod;
return;
}
pushdown(tar);
int mid = (t[tar].l + t[tar].r) >> ;
if (r <= mid) update(ope, l, r, v, tar << );
else if (l > mid) update(ope, l, r, v, tar << | );
else update(ope, l, mid, v, tar << ), update(ope, mid + , r, v, tar << | );
pushup(tar);
} int query(int l, int r, int v, int tar)
{
if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
{
int res = ;
for (int i = ; i < v; i++)
res *= t[tar].v, res %= mod;
return res * (t[tar].r - t[tar].l + ) % mod;
}
pushdown(tar);
int mid = (t[tar].l + t[tar].r) >> ;
if (r <= mid) return query(l, r, v, tar << );
else if (l > mid) return query(l, r, v, tar << | );
else return (query(l, mid, v, tar << ) + query(mid + , r, v, tar << | )) % mod;
} int main()
{
int ope, x, y, c; while (cin >> n >> m && n && m)
{
build(, n, );
while (m--)
{
scanf("%d%d%d%d", &ope, &x, &y, &c);
if (ope != )
update(ope, x, y, c, );
else cout << query(x, y, c, ) << endl;
}
}
}

Transformation HDU - 4578(线段树——懒惰标记的妙用)的更多相关文章

  1. K - Transformation HDU - 4578 线段树经典题(好题)

    题意:区间  加   变成定值 乘  区间查询:和 平方和 立方和 思路:超级超级超级麻烦的一道题  设3个Lazy 标记分别为  change 改变mul乘 add加  优先度change>m ...

  2. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  3. 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  4. HDU 4578 线段树玄学算法?

    Transformation 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is p ...

  5. Codeforces 444C 线段树 懒惰标记

    前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记. 然后昨天真的是给这道题跪了,写了好久好久,...我本来是写了个add标 ...

  6. HDU 4578 线段树复杂题

    题目大意: 题意:有一个序列,有四种操作: 1:区间[l,r]内的数全部加c. 2:区间[l,r]内的数全部乘c. 3:区间[l,r]内的数全部初始为c. 4:询问区间[l,r]内所有数的P次方之和. ...

  7. HDU - 4578 线段树+三重操作

    这道题自己写了很久,还是没写出来,也看了很多题解,感觉多数还是看的迷迷糊糊,最后面看到一篇大佬的才感觉恍然大悟. 先上一篇大佬的题解:https://blog.csdn.net/aqa20372995 ...

  8. hdu 4578 线段树 ****

    链接:点我  1

  9. hdu 3954 线段树 (标记)

    Level up Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 常见Code Review过程中发现的问题-续

    上一篇列举了一些比较常见的Code Review问题列表,文末有链接,可追溯查看.本篇为上篇的姊妹篇,继续列举一些上篇遗漏的或不易发现的问题清单,希望能整体性把一些常见的问题表述出来. 测试数据不具有 ...

  2. Greenplum主备节点切换

    1. 场景描述 Greenplum主节点出现故障,需要将standby节点手动切换为master节点,当master节点修复完成后,再将新修复的master节点设置为standyb节点加入到集群中. ...

  3. BFM使用 - 获取平均脸模型的68个特征点坐标

    使用版本:2009 数据说明网址:https://faces.dmi.unibas.ch/bfm/index.php?nav=1-1-0&id=details 数据下载网址:https://f ...

  4. [机器学习] k-近邻算法(knn)

    最近在参加大数据的暑期培训,记录一下学习的东西. 引言 懒惰学习法:简单的存储数据,并且一直等待,直到给定一个检验数据,才进行范化,以便根据与存储的训练元组的相似性对该检验数据进行分类.懒惰学习法在 ...

  5. C语言入门6-选择结构--f语句-switch

    一.     什么是选择结构? 选择结构,也称为分支结构!! 选择结构就是根据    给定的判定条件,判断结果, 并根据  判断的结果   来控制程序的流程 (流程图中,   菱形框 是有来判断的 , ...

  6. python基础之list列表的增删改查以及循环、嵌套

    Python的列表在JS中又叫做数组,是基础数据类型之一,以[]括起来,以逗号隔开,可以存放各种数据类型.嵌套的列表.对象.列表是有序的,即有索引值,可切片,方便取值.列表的操作和对字符串的操作是一样 ...

  7. 一文带你了解Java反射机制

    想要获取更多文章可以访问我的博客 - 代码无止境. 上周上班的时候解决一个需求,需要将一批数据导出到Excel.本来公司的中间件组已经封装好了使用POI生成Excel的工具方法,但是无奈产品的需求里面 ...

  8. bean的创建(五)第四部分 bean构造器的查找

    前面分析了bean的静态工厂查找 bean的构造器查找过程和bean的静态工厂查找类似 protected BeanWrapper createBeanInstance(String beanName ...

  9. 一、PyTorch 入门实战—Tensor(转)

    目录 一.Tensor的创建和使用 二.Tensor放到GPU上执行 三.Tensor总结 一.Tensor的创建和使用 1.概念和TensorFlow的是基本一致的,只是代码编写格式的不同.我们声明 ...

  10. js里的window对象

    alert(<msg>)                   向用户显示对话框窗口并等候其被关闭 blur()                               让窗口失去键盘焦 ...