当前位置:网站首页>HDU ACM 4578 Transformation-> Segment tree - interval change
HDU ACM 4578 Transformation-> Segment tree - interval change
2022-07-07 03:11:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
analysis : Complex business segment tree .
There is only one query operation , It's a requirement [l,r] Between the number of p Sum of thorium . Not all queries, all nodes , meeting TLE. The best part is the query part [a.b]. We are equal for all of these interval values , I can return to (b-a+1)*val Value . An interval in which all values in the interval are the same . For initial value setting and addition and multiplication operations . There are two situations :1、 When it is set to initial value . Just cover the interval directly . And assign the marked addition and multiplication operation as the initial value .2、 When it is an addition and multiplication operation . First, infer whether the current interval segment is the same value , If yes, add and multiply directly , Maintain the same value . Assumptions are different , See whether the interval has an addition and multiplication mark , Pass this addition and multiplication mark all the way down . Stop until you meet the same value of that interval . Finally, assign this addition and multiplication to the initial interval . Simply put , The override operation can be maintained directly , If it is not an override operation . Only one addition and multiplication operation can be reserved in the interval .
#include<iostream>
using namespace std;
#define lz t<<1,l,mid // Left interval
#define rz (t<<1)|1,mid+1,r // The right range
#define N 100005
#define MOD 10007
__int64 add[N<<2],mul[N<<2],chan[N<<2],sum[N<<2];
void Build(int t,int l,int r) // Create a line tree
{
int mid;
mul[t]=1;
add[t]=sum[t]=0;
chan[t]=0;
if(l==r)
{
chan[t]=1; // Set leaf node to 1. Convenient inquiry
return ;
}
mid=(l+r)>>1;
Build(lz);
Build(rz);
}
void PushDown(int t,int l,int r) // Mark down
{
int mid;
if(l==r) return ;
mid=(l+r)>>1;
if(chan[t]) //set Mark down
{
add[t<<1]=0,mul[t<<1]=1;
add[(t<<1)|1]=0,mul[(t<<1)|1]=1;
chan[t<<1]=chan[(t<<1)|1]=1;
sum[t<<1]=sum[(t<<1)|1]=sum[t];
chan[t]=0;
}
else
{
if(add[t]) // Mark and download
{
if(chan[t<<1]) sum[t<<1]=(sum[t<<1]+add[t])%MOD; // Zuo Zi Shu you set Mark
else
{
PushDown(lz); // Next
add[t<<1]=(add[t<<1]+add[t])%MOD;
}
if(chan[(t<<1)|1]) sum[(t<<1)|1]=(sum[(t<<1)|1]+add[t])%MOD; // Zuo Zi Shu you set Mark
else
{
PushDown(rz); // Next
add[(t<<1)|1]=(add[(t<<1)|1]+add[t])%MOD;
}
add[t]=0;
}
if(mul[t]>1) // Multiply the mark and pass it down
{
if(chan[t<<1]) sum[t<<1]=(sum[t<<1]*mul[t])%MOD; // Zuo Zi Shu you set Mark
else
{
PushDown(lz); // Next
mul[t<<1]=(mul[t<<1]*mul[t])%MOD;
}
if(chan[(t<<1)|1]) sum[(t<<1)|1]=(sum[(t<<1)|1]*mul[t])%MOD; // Zuo Zi Shu you set Mark
else
{
PushDown(rz); // Next
mul[(t<<1)|1]=(mul[(t<<1)|1]*mul[t])%MOD;
}
mul[t]=1;
}
}
}
void Update(int t,int l,int r,int ul,int ur,int c,int op)
{
int mid;
if(l>=ul && ur>=r) // The border
{
if(op==3)
chan[t]=1,mul[t]=1,add[t]=0,sum[t]=c;
else if(chan[t])
{
if(op==1) sum[t]=(sum[t]+c)%MOD;
else sum[t]=(sum[t]*c)%MOD;
}
else
{
PushDown(t,l,r); // Next
if(op==1) add[t]=(add[t]+c)%MOD;
else mul[t]=(mul[t]*c)%MOD;
}
return ;
}
PushDown(t,l,r);
mid=(l+r)>>1;
if(ur<=mid) Update(lz,ul,ur,c,op);
else if(ul>mid) Update(rz,ul,ur,c,op);
else
{
Update(lz,ul,mid,c,op);
Update(rz,mid+1,ur,c,op);
}
}
__int64 Query(int t,int l,int r,int ul,int ur,int p)
{
int mid,i;
__int64 ans,tp,t1,t2;
if(ul<=l && r<=ur)
if(chan[t])
{
ans=1;
tp=sum[t];
for(i=1;i<=p;i++) ans=(ans*tp)%MOD;
return (r-l+1)*ans%MOD; // Because every part of the interval is the same
}
PushDown(t,l,r); // Mark down
mid=(l+r)>>1;
if(ur<=mid) return Query(lz,ul,ur,p);
else if(ul>mid) return Query(rz,ul,ur,p);
else
{
t1=Query(lz,ul,mid,p);
t2=Query(rz,mid+1,ur,p);
return (t1+t2)%MOD;
}
}
int main()
{
int n,m,i,l,r,c,op;
while(scanf("%d%d",&n,&m)==2 && n+m)
{
Build(1,1,n); //1 Root node
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&op,&l,&r,&c);
if(op<=3) Update(1,1,n,l,r,c,op);
else
printf("%I64d\n",Query(1,1,n,l,r,c)%MOD);
}
}
return 0;
}
Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116783.html Link to the original text :https://javaforall.cn
边栏推荐
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
- 杰理之FM 模式单声道或立体声选择设置【篇】
- 杰理之关于 DAC 输出功率问题【篇】
- Use of promise in ES6
- Appx代码签名指南
- Summary of research status of inertial navigation calibration at home and abroad (abridged version)
- 「小样本深度学习图像识别」最新2022综述
- input_delay
- 凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
- uniapp适配问题
猜你喜欢
Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
How to verify accesstoken in oauth2 protocol
C language exercises_ one
[cpk-ra6m4 development board environment construction based on RT thread studio]
Household appliance industry under the "retail is king": what is the industry consensus?
uniapp适配问题
Redis入门完整教程:复制配置
input_delay
Analysis of USB network card sending and receiving data
Redis getting started complete tutorial: client management
随机推荐
How-PIL-to-Tensor
Cocos2d-x Box2D物理引擎编译设置
Software testing -- common assertions of JMeter interface testing
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
Contribution of Writing Series
Cloud Mail . NET Edition
Redis入门完整教程:AOF持久化
房费制——登录优化
Leetcode 77: combination
Andrews - multimedia programming
左程云 递归+动态规划
Hazel engine learning (V)
Use of promise in ES6
Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
Redis入门完整教程:客户端案例分析
Lavel PHP artisan automatically generates a complete set of model+migrate+controller commands
Simple bubble sort
Dotconnect for DB2 Data Provider
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)