思路及博客:https://www.cnblogs.com/uid001/p/10507346.html

代码:

#include <bits/stdc++.h>
#define LL long long
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
using namespace std;
const int maxn = 100010;
int m;
int p[20], b[maxn], tot, re[10];
LL Phi;
LL qpow(LL a, LL b) {
LL ans = 1;
for (; b; b >>= 1) {
if(b & 1) ans = (ans * a) % m;
a = a * a % m;
}
return ans;
}
LL phi(LL n) {
LL ans = n;
for (int i = 2; i * i <= n; i++) {
if(n % i == 0) {
ans = ans / i * (i - 1);
p[tot++] = i;
while(n % i == 0) n /= i;
}
}
if(n > 1) {
p[tot++] = n;
ans = ans / n * (n - 1);
}
return ans;
}
struct SegmentTree {
LL remain, rtag;
LL tag, sum;
LL a[10], flag[10];
};
SegmentTree tr[maxn * 4];
void pushup(int o) {
tr[o].sum = (tr[ls(o)].sum + tr[rs(o)].sum) % m;
}
void pushdown(int o) {
if(tr[o].rtag != 1) {
tr[ls(o)].remain = (tr[ls(o)].remain * tr[o].rtag) % m;
tr[ls(o)].rtag = (tr[ls(o)].rtag * tr[o].rtag) % m;
tr[rs(o)].remain = (tr[rs(o)].remain * tr[o].rtag) % m;
tr[rs(o)].rtag = (tr[rs(o)].rtag * tr[o].rtag) % m;
tr[o].rtag = 1;
}
if(tr[o].tag != 1) {
tr[ls(o)].sum = (tr[ls(o)].sum * tr[o].tag) % m;
tr[ls(o)].tag = (tr[ls(o)].tag * tr[o].tag) % m;
tr[rs(o)].sum = (tr[rs(o)].sum * tr[o].tag) % m;
tr[rs(o)].tag = (tr[rs(o)].tag * tr[o].tag) % m;
tr[o].tag = 1;
}
for (int i = 0; i < tot; i++) {
tr[ls(o)].a[i] = (tr[ls(o)].a[i] + tr[o].flag[i]);
tr[ls(o)].flag[i] = (tr[ls(o)].flag[i] + tr[o].flag[i]);
tr[rs(o)].a[i] = (tr[rs(o)].a[i] + tr[o].flag[i]);
tr[rs(o)].flag[i] = (tr[rs(o)].flag[i] + tr[o].flag[i]);
tr[o].flag[i] = 0;
}
}
void build(int o, int l, int r) {
tr[o].remain = tr[o].tag = tr[o].sum = tr[o].rtag = 1;
memset(tr[o].a, 0, sizeof(tr[o].a));
memset(tr[o].flag, 0, sizeof(tr[o].flag));
if(l == r) {
tr[o].sum = b[l] % m;
for (int i = 0; i < tot; i++) {
while(b[l] % p[i] == 0) {
b[l] /= p[i];
tr[o].a[i]++;
}
}
tr[o].remain = b[l] % m;
return;
}
int mid = (l + r) >> 1;
build(ls(o), l, mid);
build(rs(o), mid + 1, r);
pushup(o);
}
void mul(int o, int l, int r, int ql, int qr, LL x, LL y) {
if(l >= ql && r <= qr) {
tr[o].sum = (tr[o].sum * x) % m;
tr[o].tag = (tr[o].tag * x) % m;
tr[o].remain = (tr[o].remain * y) % m;
tr[o].rtag = (tr[o].rtag * y) % m;
for (int i = 0; i < tot; i++) {
tr[o].flag[i] += re[i];
tr[o].a[i] += re[i];
}
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(ql <= mid) mul(ls(o), l, mid, ql, qr, x, y);
if(qr > mid) mul(rs(o), mid + 1, r, ql, qr, x, y);
pushup(o);
}
void div(int o, int l, int r, int pos, LL y) {
if(l == r) {
for (int i = 0; i < tot; i++) {
while(y % p[i] == 0) y /= p[i], tr[o].a[i]--;
}
tr[o].remain = (tr[o].remain * qpow(y, Phi - 1)) % m;
tr[o].sum = tr[o].remain;
for (int i = 0; i < tot; i++)
tr[o].sum = (tr[o].sum * qpow(p[i], tr[o].a[i])) % m;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(pos <= mid) div(ls(o), l, mid, pos, y);
else div(rs(o), mid + 1, r, pos, y);
pushup(o);
}
LL query(int o, int l, int r, int ql, int qr) {
if(l >= ql && r <= qr) return tr[o].sum;
pushdown(o);
int mid = (l + r) >> 1;
LL ans = 0;
if(ql <= mid) ans = (ans + query(ls(o), l, mid, ql, qr)) % m;
if(qr > mid) ans = (ans + query(rs(o), mid + 1, r, ql, qr)) % m;
return ans;
}
int main() {
int n;
scanf("%d%lld", &n, &m);
Phi = phi(m);
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
int t, op, x, y, z;
build(1, 1, n);
scanf("%d", &t);
while(t--) {
scanf("%d", &op);
if(op == 1) {
scanf("%d%d%d", &x, &y, &z);
int tmp = z;
for (int i = 0; i < tot; i++) {
re[i] = 0;
while(tmp % p[i] == 0) {
tmp /= p[i];
re[i]++;
}
}
mul(1, 1, n, x, y, z, tmp);
} else if(op == 2) {
scanf("%d%d", &x, &y);
div(1, 1, n, x, y);
} else {
scanf("%d%d", &x, &y);
printf("%lld\n", query(1, 1, n, x, y));
}
}
}

  

Codeforces 1109E 线段树的更多相关文章

  1. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  2. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  3. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  4. codeforces 1136E 线段树

    codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...

  5. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  6. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  7. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

  8. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  9. Linear Kingdom Races CodeForces - 115E (线段树优化dp)

    大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...

随机推荐

  1. 解决"hibernate.hbm2ddl.auto" update值 无效

    <property name="schemaUpdate"> <value>true</value> </property> 若果是 ...

  2. iOS UI调试工具 -- UIDebuggingInformationOverlay

    英文原文: http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/ 无意中看到iOS自带调试工具 ...

  3. C# 自定义exe引用的dll路径

    MSDN原文:https://msdn.microsoft.com/library/twy1dw1e(v=vs.100).aspx <runtime> 的 <assemblyBind ...

  4. LOJ10050 The XOR Largest Pair

    题意 在给定的 \(N\) 个整数 \(A_1,A_2,-,A_N\) 中选出两个进行异或运算,得到的结果最大是多少? 对于 \(100\%\) 的数据,\(1\le N\le 10^5, 0\le ...

  5. Jam的计数法

    Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数 ...

  6. 7天学会HTML--HTML综述

    一周学会HTML 1.HTML是什么? HTML 指的是超文本标记语言 (Hyper Text Markup Language) 2.HTML发展历程 HTML版本从1.0到4.0不断升级,其版本的规 ...

  7. Linux IO 监控与深入分析

    https://jaminzhang.github.io/os/Linux-IO-Monitoring-and-Deep-Analysis/ Linux IO 监控与深入分析 引言 接昨天电话面试,面 ...

  8. HTMLTestRunner 汉化版---来源一个大神的源码(加了失败截图,用例失败重新执行 功能)

    HTMLTestRunner 汉化版 20170925 测试报告完全汉化,包括错误日志的中文处理 针对selenium UI测试增加失败自动截图功能 增加失败自动重试功能 增加饼图统计 同时兼容pyt ...

  9. 第三方引擎应用场景分析--Tokudb,infobright

    TokuDBTokuDB的特色:• Fractal Tree而不是B-Tree• 内部结点不仅有指向父子的指针还有Buffer区,数据写入先写buffer区,FIFO结构,写入只需要顺序添加到Buff ...

  10. Linux学习笔记 -- 磁盘的管理

    df df命令参数功能:检查文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 语法: df [-ahikHTm] [目录或文件名] 选项与参数: -a ...