思路及博客: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. Python打包上传

    你可以用pip导出你的dependency: $ pip freeze > requirements.txt 然后在通过以下命令安装dependency: pip install -r requ ...

  2. I.MX6 ethtool 移植

    /************************************************************************* * I.MX6 ethtool 移植 * 说明: ...

  3. php 设计数据库连接池

    摘要 之前总是以脚本面向过程的方式写PHP代码,所以很大程度上来说,既不规范,也不安全,更不容易维护.为了代码的重用,准备写一套自己的工具库,这样的话,以后写项目的时候就可以很轻松的进行使用啦. 今天 ...

  4. 运行python代码

    IPython IPython 'magic' function documentation

  5. phpcms URL修改

    修改caches\configs\system.php中'html_root' => 'html',//生成静态文件路径改成'html_root' => '',//生成静态文件路径,然后修 ...

  6. centos下配置dns,gateway,ip

    centos网络配置实例 1,配置DNS vi /etc/resolv.conf 加入:   代码如下: nameserver 192.168.0.1 nameserver 8.8.8.8 names ...

  7. WCF NetTcpBinding 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作

    背景:WindowsService + WCF + NetTcpBinding 之前一直使用http协议模式,改为net.tcp之后隔段时间出现:由于系统缓冲区空间不足或队列已满,不能执行套接字上的操 ...

  8. fft蝶形算法的特点

  9. 软件开发中IT用语-日文和英文对照版

    開発工程 † 要件定義…Requirement Definition (Analyze) 外部設計…External Design 内部設計…Internal Design 開発…Coding テスト ...

  10. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing

    题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...