Codeforces 1109E 线段树
思路及博客: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 线段树的更多相关文章
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 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 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
随机推荐
- Tomcat Jsp环境搭建全过程--重拾jsp
搭建Jsp环境最简单的办法就是 Java+Tomcat 能很好的支持jsp代码 .首先,我们需要下载JDK和Tomcat安装包 JDK下载地址 http://www.oracle.com/techne ...
- HihoCoder1620: 股票价格3 (单调队列 or DP)
股票价格3 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 小Hi想知道,对于第 ...
- 设计模式之组合(compose)模式
树形结构在软件中随处可见,比如操作系统中的目录结构,公司组织结构等等,如何运用面向对象的方式来处理这种树形结构是组合模式需要解决的问题.组合模式通过一种巧妙的设计方案来使得用户可以一致性地处理整个树形 ...
- 设计模式之桥接(bridge)模式
在现实生活中,我们常常会用到两种或多种类型的笔,比如毛笔和蜡笔.假设我们需要大.中.小三种类型的画笔来绘制12中不同的颜色,如果我们使用蜡笔,需要准备3*12=36支.但如果使用毛笔的话,只需要提供3 ...
- 在web.config中配置httpHandlers节点是的说明
<system.web> <httpHandlers> <add verb="*" path="*.lcj" type=" ...
- Redis事件库源码分析
由于老大在新项目中使用redis的事件库代替了libevent,我也趁着机会读了一遍redis的事件库代码,第一次读到“优美,让人愉快”的代码,加之用xmind制作的类图非常帅,所以留文纪念. Red ...
- H5浏览器播放RTMP直播流
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- Eclipse 模拟http 请求插件Rest Client
eclipse update 网址 http://nextinterfaces.com/http4e/install/ 参考 http://www.nextinterfaces.com/eclips ...
- 【转】在Linux下使用Jmeter执行测试任务
想在Linux下运行jmeter必须先安装jdk,安装步骤如下: (1).下载一个linux可用的jdk包(比如:jdk-6u45-linux-i586.bin), (2).然后将jdk-6u45-l ...
- 第六章 深入分析ClassLoader工作机制
补充(非书中): Java 源程序(.java 文件)在经过 Java 编译器编译之后就被转换成 Java 字节代码(.class 文件).类加载器负责读取Java字节代码,并转换成 java.lan ...