Hdu 4578 Transformation (线段树 分类分析)
Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 10082 Accepted Submission(s): 2609
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<---ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<---ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
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.
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
7489
题解
这道题有三种询问:set , add , mul。所以lazy标记要有三个,如果三个标记同时出现的处理方法——当更新set操作时,就把add标记和mul标记全部取消;当更新mul操作时,如果当前节点add标记存在,就把add标记改为:add * mul。这样的话就可以在PushDown()操作中先执行set,然后mul,最后add。
C++代码一
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = ;
#define left v<<1
#define right v<<1|1
#define mod 10007
struct node
{
int l ,r , value ;
int eq , add , mul ;
}tree[maxn<<];
void build(int l , int r , int v)
{
tree[v].l = l ;
tree[v].r = r ;
tree[v].add = ; tree[v].mul = ;tree[v].eq = - ;
if(l == r)
{tree[v].eq = ; return ;}
int mid = (l + r) >> ;
build(l , mid , left) ;
build(mid + , r , right) ;
}
void push_down(int v)
{
if(tree[v].l == tree[v].r)return ;
if(tree[v].eq != -)
{
tree[left].eq = tree[right].eq = tree[v].eq ;
tree[left].add = tree[right].add = ;
tree[left].mul = tree[right].mul = ;
tree[v].eq = -;
return ;
}
if(tree[v].mul != )
{
if(tree[left].eq != -)
tree[left].eq = (tree[left].eq*tree[v].mul)%mod ;
else
{
push_down(left) ;
tree[left].mul = (tree[left].mul*tree[v].mul)%mod ;
}
if(tree[right].eq != -)
tree[right].eq = (tree[right].eq*tree[v].mul)%mod ;
else
{
push_down(right) ;
tree[right].mul = (tree[right].mul*tree[v].mul)%mod ;
}
tree[v].mul = ;
}
if(tree[v].add)
{
if(tree[left].eq != -)
tree[left].eq = (tree[left].eq + tree[v].add)%mod ;
else
{
push_down(left) ;
tree[left].add = (tree[left].add + tree[v].add)%mod ;
}
if(tree[right].eq != -)
tree[right].eq = (tree[right].eq + tree[v].add)%mod ;
else
{
push_down(right) ;
tree[right].add = (tree[right].add + tree[v].add)%mod ;
}
tree[v].add = ;
}
}
void update(int l , int r , int v , int op , int c)
{
if(l <= tree[v].l && tree[v].r <= r)
{
if(op == )
{
tree[v].add = ;tree[v].mul = ;
tree[v].eq = c ;
return ;
}
if(tree[v].eq != -)
{
if(op == )tree[v].eq = (tree[v].eq + c)%mod ;
else tree[v].eq = (tree[v].eq*c)%mod ;
}
else
{
push_down(v) ;
if(op == )tree[v].add = (tree[v].add + c)%mod ;
else tree[v].mul = (tree[v].mul*c)%mod ;
}
return ;
}
push_down(v) ;
int mid = (tree[v].l + tree[v].r) >> ;
if(l <= mid)update(l , r ,left , op , c) ;
if(r > mid)update(l , r , right , op , c) ;
}
int query(int l , int r , int v , int q)
{
if(tree[v].l >= l && tree[v].r <= r && tree[v].eq != -)
{
int ans = ;
for(int i = ;i <= q;i++)
ans = (ans * tree[v].eq)%mod ;
return (ans*((tree[v].r - tree[v].l + )%mod))%mod ;
}
push_down(v) ;
int mid = (tree[v].l + tree[v].r) >> ;
if(l > mid)return query(l , r , right, q) ;
else if(r <= mid)return query(l , r ,left ,q) ;
else return (query(l , mid , left , q) + query(mid + , r , right , q))%mod ;
}
int main()
{
//freopen("in.txt" ,"r" , stdin) ;
int n , m ;
while(scanf("%d%d" , &n , &m) &&(n+m))
{
int op , x , y , c;
build( , n , ) ;
while(m--)
{
scanf("%d%d%d%d" , &op , &x , &y , &c) ;
if(op == )
printf("%d\n" , (query(x, y , , c)%mod)) ;
else update(x , y , , op , c) ;
}
}
return ;
}
C++代码二
解释
平方和这样来推:(a + c)2 = a2 + c2 + 2ac , 即sum2[rt] = sum2[rt] + (r - l + 1) * c * c + 2 * sum1[rt] * c;
立方和这样推:(a + c)3 = a3 + c3 + 3a(a2 + ac) , 即sum3[rt] = sum3[rt] + (r - l + 1) * c * c * c + 3 * c * (sum2[rt] + sum1[rt] * c);
几个注意点:add标记取消的时候是置0,mul标记取消的时候是置1;在PushDown()中也也要注意取消标记,如set操作中取消add和mul,mul操作中更新add; 在add操作中要注意sum3 , sum2 , sum1的先后顺序,一定是先sum3 , 然后sum2 , 最后sum1; int容易爆,还是用LL要保险一点; 最后就是运算较多,不要漏掉东西。
当然这种方法有取巧的成分
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
typedef long long ll;
#define eps 1e-8
#define INF INT_MAX
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
const int N = ;
ll add[maxn << ] , set[maxn << ] , mul[maxn << ];
ll sum1[maxn << ] , sum2[maxn << ] , sum3[maxn << ];
void PushUp(int rt)
{
sum1[rt] = (sum1[rt << ] + sum1[rt << | ]) % MOD;
sum2[rt] = (sum2[rt << ] + sum2[rt << | ]) % MOD;
sum3[rt] = (sum3[rt << ] + sum3[rt << | ]) % MOD;
}
void build(int l , int r , int rt)
{
add[rt] = set[rt] = ;
mul[rt] = ;
if(l == r) {
sum1[rt] = sum2[rt] = sum3[rt] = ;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
PushUp(rt);
}
void PushDown(int rt , int len)
{
if(set[rt]) {
set[rt << ] = set[rt << | ] = set[rt];
add[rt << ] = add[rt << | ] = ; //注意这个也要下放
mul[rt << ] = mul[rt << | ] = ;
ll tmp = ((set[rt] * set[rt]) % MOD) * set[rt] % MOD;
sum1[rt << ] = ((len - (len >> )) % MOD) * (set[rt] % MOD) % MOD;
sum1[rt << | ] = ((len >> ) % MOD) * (set[rt] % MOD) % MOD;
sum2[rt << ] = ((len - (len >> )) % MOD) * ((set[rt] * set[rt]) % MOD) % MOD;
sum2[rt << | ] = ((len >> ) % MOD) * ((set[rt] * set[rt]) % MOD) % MOD;
sum3[rt << ] = ((len - (len >> )) % MOD) * tmp % MOD;
sum3[rt << | ] = ((len >> ) % MOD) * tmp % MOD;
set[rt] = ;
}
if(mul[rt] != ) { //这个就是mul[rt] != 1 , 当时我这里没注意所以TLE了
mul[rt << ] = (mul[rt << ] * mul[rt]) % MOD;
mul[rt << | ] = (mul[rt << | ] * mul[rt]) % MOD;
if(add[rt << ]) //注意这个也要下放
add[rt << ] = (add[rt << ] * mul[rt]) % MOD;
if(add[rt << | ])
add[rt << | ] = (add[rt << | ] * mul[rt]) % MOD;
ll tmp = (((mul[rt] * mul[rt]) % MOD * mul[rt]) % MOD);
sum1[rt << ] = (sum1[rt << ] * mul[rt]) % MOD;
sum1[rt << | ] = (sum1[rt << | ] * mul[rt]) % MOD;
sum2[rt << ] = (sum2[rt << ] % MOD) * ((mul[rt] * mul[rt]) % MOD) % MOD;
sum2[rt << | ] = (sum2[rt << | ] % MOD) * ((mul[rt] * mul[rt]) % MOD) % MOD;
sum3[rt << ] = (sum3[rt << ] % MOD) * tmp % MOD;
sum3[rt << | ] = (sum3[rt << | ] % MOD) * tmp % MOD;
mul[rt] = ;
}
if(add[rt]) {
add[rt << ] += add[rt]; //add是+= , mul是*=
add[rt << | ] += add[rt];
ll tmp = (add[rt] * add[rt] % MOD) * add[rt] % MOD; //注意sum3 , sum2 , sum1的先后顺序
sum3[rt << ] = (sum3[rt << ] + (tmp * (len - (len >> )) % MOD) + * add[rt] * ((sum2[rt << ] + sum1[rt << ] * add[rt]) % MOD)) % MOD;
sum3[rt << | ] = (sum3[rt << | ] + (tmp * (len >> ) % MOD) + * add[rt] * ((sum2[rt << | ] + sum1[rt << | ] * add[rt]) % MOD)) % MOD;
sum2[rt << ] = (sum2[rt << ] + ((add[rt] * add[rt] % MOD) * (len - (len >> )) % MOD) + ( * sum1[rt << ] * add[rt] % MOD)) % MOD;
sum2[rt << | ] = (sum2[rt << | ] + (((add[rt] * add[rt] % MOD) * (len >> )) % MOD) + ( * sum1[rt << | ] * add[rt] % MOD)) % MOD;
sum1[rt << ] = (sum1[rt << ] + (len - (len >> )) * add[rt]) % MOD;
sum1[rt << | ] = (sum1[rt << | ] + (len >> ) * add[rt]) % MOD;
add[rt] = ;
}
}
void update(int L , int R , int c , int ch , int l , int r , int rt)
{
if(L <= l && R >= r) {
if(ch == ) {
set[rt] = c;
add[rt] = ;
mul[rt] = ;
sum1[rt] = ((r - l + ) * c) % MOD;
sum2[rt] = ((r - l + ) * ((c * c) % MOD)) % MOD;
sum3[rt] = ((r - l + ) * (((c * c) % MOD) * c % MOD)) % MOD;
} else if(ch == ) {
mul[rt] = (mul[rt] * c) % MOD;
if(add[rt])
add[rt] = (add[rt] * c) % MOD;
sum1[rt] = (sum1[rt] * c) % MOD;
sum2[rt] = (sum2[rt] * (c * c % MOD)) % MOD;
sum3[rt] = (sum3[rt] * ((c * c % MOD) * c % MOD)) % MOD;
} else if(ch == ) {
add[rt] += c;
ll tmp = (((c * c) % MOD * c) % MOD * (r - l + )) % MOD; //(r - l + 1) * c^3
sum3[rt] = (sum3[rt] + tmp + * c * ((sum2[rt] + sum1[rt] * c) % MOD)) % MOD;
sum2[rt] = (sum2[rt] + (c * c % MOD * (r - l + ) % MOD) + * sum1[rt] * c) % MOD;
sum1[rt] = (sum1[rt] + (r - l + ) * c) % MOD;
}
return;
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
if(L > m)
update(L , R , c , ch , rson);
else if(R <= m)
update(L , R , c , ch , lson);
else {
update(L , R , c , ch , lson);
update(L , R , c , ch , rson);
}
PushUp(rt);
}
ll query(int L , int R , int p , int l , int r , int rt)
{
if(L <= l && R >= r) {
if(p == )
return sum1[rt] % MOD;
else if(p == )
return sum2[rt] % MOD;
else
return sum3[rt] % MOD;
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
if(L > m)
return query(L , R , p , rson);
else if(R <= m)
return query(L , R , p , lson);
else
return (query(L , R , p , lson) + query(L , R , p , rson)) % MOD;
}
int main()
{
int n , m;
int a , b , c , ch;
while(~scanf("%d %d" , &n , &m))
{
if(n == && m == )
break;
build( , n , );
while(m--) {
scanf("%d %d %d %d" , &ch , &a , &b , &c);
if(ch != ) {
update(a , b , c , ch , , n , );
} else {
printf("%lld\n" , query(a , b , c , , n , ));
}
}
}
return ;
}
Hdu 4578 Transformation (线段树 分类分析)的更多相关文章
- HDU 4578 Transformation --线段树,好题
题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...
- hdu 4578 Transformation 线段树
没什么说的裸线段树,注意细节就好了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- Transformation HDU - 4578(线段树——懒惰标记的妙用)
Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial val ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 4578 - Transformation - [加强版线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is puzzled with the ...
- HDU - 4578 Transformation(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...
随机推荐
- C++ GUI Qt4学习笔记07
C++ GUI Qt4 qtc++scrollobject编程 事件(event)是由串口系统或者Qt自身产生的,用以响应所发生的各类事情.当用户按下或者松开键盘或者鼠标上的按键时,就可以产生一个 ...
- 微信小程序支付功能讲解
前言:虽然小程序做过很多,但是一直觉得微信支付功能很是神秘,现在终于有机会接触心里还是有点小激动的,经过一番折腾发现支付也不过如此,在此记录下支付功能的实现过程 小程序的官方文档介绍到发起微信支付即调 ...
- luogu P1068 分数线划定 x
P1068 分数线划定 题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据 ...
- ACM 求全排列(字典序、邻位对换、递增进位制数,递减进位制数)
字典序:(联合康托展开就也可以按照中介数求) 邻位对换.递增进位制数,递减进位制数:具体的实现和算法讲解如下: 代码..C++版的实现并不好..因为是挨个向后找的,如果K很大的时候会超时,不过...思 ...
- flex兼容问题
display:flex作为C3的新属性,还是有的浏览器不支持的,那下面我们就来说一下他的兼容写法 .box{ display: -webkit-box; /* 老版本语法: Safari, iOS, ...
- android 任务栈及启动模式
1.一个应用程序一般都是由多个activity组成的.2.任务栈(task stack)(别名back stack后退栈) 记录存放用户开启的activity的.3.一个应用程序一被开启系统就给他分配 ...
- IDEA插件之自动查找bug工具
打开idea 插件搜索界面 输入 FindBugs-IDEA,安装完成后重启,选中要查找的包,右键找到对应的 FindBugs就可以开始进行自动扫描了
- Linux驱动开发4——并发和竞态
Linux系统处于一个高并发的运行环境,不管是系统调用还是中断都要求可重入,但是有一些系统资源处于临界区,因此,必须保证临界区资源访问的原子性. 对于临界区资源被占用时,发起访问的进程,有三种处理方法 ...
- websocket 无需通过轮询服务器的方式以获得响应 同步在线用户数 上线下线 抓包 3-way-handshake web-linux-shell 开发
https://code.google.com/archive/p/phpwebsocket/source/default/source The WebSocket API (WebSockets) ...
- First-order logic
w https://en.wikipedia.org/wiki/First-order_logic