当前位置:网站首页>Luogu P3373: Segment tree
Luogu P3373: Segment tree
2022-08-01 00:32:00 【JackDawn!?】
题目
Click me to find a topic
The main idea is to have a sequence of numbers,有三种操作,One is to add a number to each number in an interval;One is to multiply each number in an interval by a number;The last one is a query.
思路
This is actually a template question,只不过相比The most basic line segment treeThere is one more mark,Then the main problem we have to solve is how to maintain these two kinds of marks.
There are two main problems during maintenance:
- How to hit when marking
- How to drop when the mark is dropped
The contradiction is actually there,I am adding a kind of markup,Need to process another tag;如果需要,What to do to make the correctness of this tree unchanged.
We can solve it by artificially setting priorities:
对于一个节点k, f[k]represents the interval sum, madd[k]Represents an addition mark, mmul[k]Represents the multiplication flag
- Consider addition first
则对于节点k的子节点来说, Specifies when updating child nodes,First process the addition token passed from the parent node,Then process the multiplication mark passed down from the parent node,i.e. add and multiply.Then we are adding numbers to an intervalz的时候,The properties of the four arithmetic operations learned in elementary school,我们可以得到madd[k] += z/mmul[k]
.Obviously, after this processing, the value of the child node can be updated correctly according to the specified priority. - Consider multiplication first
对于节点k的子节点来说, Specifies when updating child nodes,The multiplication mark passed down from the parent node is processed first,Then process the addition mark passed down from the parent node,That is, multiply and then add.So we are multiplying a number by a rangez的时候,according to the distributive law of multiplicationmadd[k] *= z
.
for the above two approaches,The first approach is due to the existence of integer division,Therefore, there may be problems with accuracy;The second approach may cause overflow though,But the question states that the answer needs to be modulo,So the overflow problem is also solved,Then we choose the second option.
代码
#include <cstdio>
using namespace std;
#define lc k<<1
#define rc k<<1|1
typedef long long ll;
const int maxn = 1e5+5;
ll a[maxn], f[4*maxn], madd[4*maxn], mmul[4*maxn];
int n, m, p;
inline void buildTree(int k, int l, int r) {
// k l r are the subscripts of the current processing node, respectively 当前区间的左端点 当前区间的右端点 下同
madd[k] = 0; mmul[k] = 1;
if(l == r) {
f[k] = a[l]; return; }
int m = (l+r) >> 1;
buildTree(lc, l, m);
buildTree(rc, m+1, r);
f[k] = (f[lc] + f[rc])%p;
}
inline void pushdown(int k, int l, int r) {
int m = (l+r) >> 1;
f[lc] = (f[lc]*mmul[k] + madd[k]*(m-l+1))%p;
f[rc] = (f[rc]*mmul[k] + madd[k]*(r-m))%p;
mmul[lc] = (mmul[lc]*mmul[k]) % p;
mmul[rc] = (mmul[rc]*mmul[k]) % p;
madd[lc] = (madd[lc]*mmul[k] + madd[k]) % p;
madd[rc] = (madd[rc]*mmul[k] + madd[k]) % p;
mmul[k] = 1;
madd[k] = 0;
}
inline void add(int k, int l, int r, int x, int y, ll z) {
if(l == x && r == y) {
madd[k] = (madd[k] + z) % p;
f[k] = (f[k] + z*(r-l+1)) % p;
return;
}
pushdown(k, l, r);
int m = (l+r) >> 1;
if(y <= m) {
add(lc, l, m, x, y, z);
} else if(x > m) {
add(rc, m+1, r, x, y, z);
} else {
add(lc, l, m, x, m, z);
add(rc, m+1, r, m+1, y, z);
}
f[k] = (f[lc] + f[rc]) % p;
}
inline void mul(int k, int l, int r, int x, int y, ll z) {
if(l == x && r == y) {
madd[k] = (madd[k]*z) % p;
mmul[k] = (mmul[k]*z) % p;
f[k] = (f[k] * z) % p;
return;
}
pushdown(k, l, r);
int m = (l+r) >> 1;
if(y <= m) {
mul(lc, l, m, x, y, z);
} else if(x > m) {
mul(rc, m+1, r, x, y, z);
} else {
mul(lc, l, m, x, m, z);
mul(rc, m+1, r, m+1, y, z);
}
f[k] = (f[lc] + f[rc]) % p;
}
inline ll query(int k, int l, int r, int x, int y) {
if(l == x && r == y) {
return f[k];
}
pushdown(k, l, r);
int m = (l+r) >> 1;
if(y <= m) {
return query(lc, l, m, x, y);
} else if(x > m) {
return query(rc, m+1, r, x, y);
} else {
return query(lc, l, m, x, m) + query(rc, m+1, r, m+1, y);
}
}
int main(void)
{
// freopen("in.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &p);
for(int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
buildTree(1, 1, n);
int x, y, op;
ll k;
while(m--) {
scanf("%d", &op);
if(op == 1) {
scanf("%d%d%lld", &x, &y, &k);
mul(1, 1, n, x, y, k);
} else if(op == 2) {
scanf("%d%d%lld", &x, &y, &k);
add(1, 1, n, x, y, k);
} else {
scanf("%d%d", &x, &y);
printf("%lld\n", query(1, 1, n, x, y)%p);
}
}
return 0;
}
边栏推荐
- 2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
- Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
- Matlab/ArcGIS processing GPM global monthly precipitation data
- The principle of virtual inheritance
- MVCC总结
- Flink 1.13(八)CDC
- [AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
- Compose principle - the view and the principle of two-way data binding
- 一体化步进电机在无人机自动机场的应用
- LeetCode--打家劫舍问题
猜你喜欢
Matlab / ArcGIS 处理GPM全球月均降水数据
C# Rectangle basic usage and picture cutting
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
/etc/sysconfig/network-scripts 配置网卡
MYSQL经典面试题
Mysql environment installation under Linux (centos)
从零造键盘的键盘超级喜欢,IT人最爱
类和对象:中
欧拉系统(euleros):升级Mysql
随机推荐
WindowInsetsControllerCompat is simple to use
Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Exam preparation plan
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
TFC CTF 2022 WEB Diamand WriteUp
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
Item 36: Specify std::launch::async if asynchronicity is essential.
Application of integrated stepper motor in UAV automatic airport
thymeleaf iterates the map collection
Matlab/ArcGIS processing GPM global monthly precipitation data
Rasa 3.x 学习系列-使用查找表改进实体提取
date命令
Design the message queue storage MySQL form of message data
NIO编程
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
信奥学习规划 信息学竞赛之路(2022.07.31)
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
Matlab / Arcgis处理nc数据
两院院士直言:不要迷信院士