思路及博客: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. Chrome设置允许ajax跨域

    最近在做一个前后端分离的项目,在Windows上用.Net WebApi时候的后端,在Mac上用ng2搭建的前端. 要实现前后端对接进行调试,就必须要解决ajax跨域的问题,实现方法如下: //在te ...

  2. python库之threading

    This module constructs higher-level threading interfaces on top of the lower level python库之_threadmo ...

  3. Net Core ElasticSearch入门

    ElasticSearch入门 附.Net Core例子 https://www.cnblogs.com/CoderAyu/p/9564977.html 1.什么是ElasticSearch? Ela ...

  4. 《DSP using MATLAB》示例Example7.23

    代码: wp = 0.2*pi; ws = 0.3*pi; Rp = 0.25; As = 50; [delta1, delta2] = db2delta(Rp, As); [N, f, m, wei ...

  5. 剑指offer-第五章优化时间和空间效率(数组中的逆序对的总数)

    题目:在数组中如果两个数字的前面的数比后面的数大,则称为一对逆序对.输入一个数组求出数组中逆序对的总数. 以空间换时间:思路:借助一个辅助数组,将原来的数组复制到该数组中.然后将该数组分成子数组,然后 ...

  6. Windows Server 2008用IIS部署FTP简述

    1.安装IIS 2.在IIS中勾选FTP选项 3. 新建FTP站点

  7. BZOJ3214 [Zjoi2013]丽洁体

    题意 平时的练习和考试中,我们经常会碰上这样的题:命题人给出一个例句,要我们类比着写句子.这种往往被称为仿写的题,不单单出现在小学生的考试中,也有时会出现在中考中.许多同学都喜欢做这种题,因为较其它题 ...

  8. webstorm-前端javascript开发神器中文教程和技巧分享(转)

    webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载:百度网盘下载:http://pan.baidu. ...

  9. 缓存(Cache)管理 ---- 系列文章

    利用Cache防止同一帐号重复登录 .net中Cache管理操作 系统缓存全解析 (下) 系统缓存全解析 (中) 系统缓存全解析 (上) 出处:http://www.cnblogs.com/luckd ...

  10. Delphi2010中DataSnap高级技术(转)

    一. 为DataSnap系统服务程序添加描述 这几天一直在研究Delphi 2010的DataSnap,感觉功能真是很强大,现在足有理由证明Delphi7该下岗了. DataSnap有三种服务模式,其 ...