思路及博客: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. 进程(并发,并行) join start 进程池 (同步异步)

    一.背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象.进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有 ...

  2. python之Beautiful Soup库

    1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...

  3. bzoj 3124 直径

    Written with StackEdit. Description 小\(Q\)最近学习了一些图论知识.根据课本,有如下定义. 树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一 ...

  4. 初识ADO.NET

    摘要 作为.NET框架最重要的组件之一,ADO.NET扮演着应用程序与数据交互的重要的角色.本文将从宏观的角度来探讨ADO.NET,和大家一起了解ADO.NET来龙去脉以及ADO.NET的主要组成部分 ...

  5. 阿里云接口异常-Can not find endpoint to access

    最近在做公司的资产盘点,需要请求阿里云的接口获取公司的云服务器信息.在获取实例列表的过程中,通过异常机制捕获了 Can not find endpoint to access 这个错误.经过多次排查, ...

  6. Redis 字符串(String)

    Redis 字符串(String) Redis 字符串数据类型的相关命令用于管理 redis 字符串值,基本语法如下: 语法 redis 127.0.0.1:6379> COMMAND KEY_ ...

  7. numpy之初识ndarray

    Numpy ndarray numpy的最重要特点就是其N维数组对象(ndarray). ndarray的可以对整块数据执行数学运算,语法与标量元素的元素的运算一致. 如: import numpy ...

  8. Linux 安装交叉编译工具链

    交叉编译工具链下载地址: 链接:http://pan.baidu.com/s/1dE7P9rb 密码:300i 声明:下面每一步中的“pwd”指令都是为了看清楚当前的目录,没有其他实际意义. 系统:u ...

  9. contOS 下安装mysql

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  10. java调用python

    本文记录下使用jython包来实现java代码中调用Python. 一.Maven加入 <dependency> <groupId>org.python</groupId ...