Codeforces 1114F Please, another Queries on Array? 线段树
Please, another Queries on Array?
利用欧拉函数的计算方法, 用线段树搞一搞就好啦。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 4e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, q, tot, b[N], prime[], mul[];
LL S[];
char op[]; bool ok(int x) {
for(int i = ; i * i <= x; i++)
if(x % i == ) return false;
return true;
} int Power(int a, int b) {
int ans = ;
while(b) {
if(b & ) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod; b >>= ;
}
return ans;
} int a[N << ], lazy[N << ];
LL mask[N << ], mlazy[N << ]; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 inline void pull(int rt) {
a[rt] = 1ll * a[rt << ] * a[rt << | ] % mod;
mask[rt] = mask[rt << ] | mask[rt << | ];
} void push(int rt, int l, int mid, int r) {
if(lazy[rt] != ) {
a[rt << ] = 1ll * a[rt << ] * Power(lazy[rt], mid - l + ) % mod;
a[rt << | ] = 1ll * a[rt << | ] * Power(lazy[rt], r - mid) % mod;
lazy[rt << ] = 1ll * lazy[rt << ] * lazy[rt] % mod;
lazy[rt << | ] = 1ll * lazy[rt << | ] * lazy[rt] % mod;
lazy[rt] = ;
}
if(mlazy[rt] != ) {
mask[rt << ] |= mlazy[rt]; mask[rt << | ] |= mlazy[rt];
mlazy[rt << ] |= mlazy[rt]; mlazy[rt << | ] |= mlazy[rt];
mlazy[rt] = ;
}
} void build(int l, int r, int rt) {
lazy[rt] = ;
mlazy[rt] = ;
if(l == r) {
a[rt] = b[l];
mask[rt] = S[b[l]];
return;
}
int mid = l + r >> ;
build(lson); build(rson);
pull(rt);
} void update(int L, int R, int mul, int l, int r, int rt) {
if(l >= L && r <= R) {
a[rt] = 1ll * a[rt] * Power(mul, r - l + ) % mod;
lazy[rt] = 1ll * lazy[rt] * mul % mod;
mask[rt] |= S[mul];
mlazy[rt] |= S[mul];
return;
}
int mid = l + r >> ;
push(rt, l, mid, r);
if(L <= mid) update(L, R, mul, lson);
if(R > mid) update(L, R, mul, rson);
pull(rt);
} PLI query(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return mk(mask[rt], a[rt]);
int mid = l + r >> ;
push(rt, l, mid, r);
PLI ans = mk(, );
if(L <= mid) {
PLI tmp = query(L, R, lson);
ans.fi |= tmp.fi;
ans.se = 1ll * ans.se * tmp.se % mod;
}
if(R > mid) {
PLI tmp = query(L, R, rson);
ans.fi |= tmp.fi;
ans.se = 1ll * ans.se * tmp.se % mod;
}
return ans;
} int main() {
for(int i = ; i <= ; i++)
if(ok(i)) prime[tot++] = i;
for(int i = ; i < tot; i++)
mul[i] = ( - Power(prime[i], mod - ) + mod) % mod;
for(int i = ; i <= ; i++)
for(int j = ; j < tot && prime[j] <= i; j++)
if(i % prime[j] == ) S[i] |= 1ll << j;
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++) scanf("%d", &b[i]);
build(, n, );
while(q--) {
scanf("%s", op);
if(op[] == 'T') {
int L, R; scanf("%d%d", &L, &R);
PLI ret = query(L, R, , n, );
int ans = ret.se; LL mask = ret.fi;
for(int i = ; i < tot; i++)
if(mask >> i & ) ans = 1ll * ans * mul[i] % mod;
printf("%d\n", ans);
} else {
int L, R, x;
scanf("%d%d%d", &L, &R, &x);
update(L, R, x, , n, );
}
}
return ;
} /*
*/
Codeforces 1114F Please, another Queries on Array? 线段树的更多相关文章
- Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]
Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- 暑假集训单切赛第一场 CF 266E More Queries to Array(线段树+二项式展开式)
比赛时,第二题就是做的这个,当时果断没仔细考虑,直接用线段树暴力求.结果易想而知,超时了. 比赛后搜了搜题解,恍然大悟. 思路:显然用线段树,但是由于每次查询都会有变,所以不可能存储题目中的式子. ...
- [Codeforces266E]More Queries to Array...——线段树
题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...
- Codeforces 1114F(欧拉函数、线段树)
AC通道 要点 欧拉函数对于素数有一些性质,考虑将输入数据唯一分解后进行素数下的处理. 对于素数\(p\)有:\(\phi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\) ...
- codeforces 671C Ultimate Weirdness of an Array 线段树+构造
题解上说的很清楚了,我照着写的,表示膜拜题解 然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn) #include <stdio.h> #inc ...
- Can you answer these queries? HDU 4027 线段树
Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...
- codeforces 719E E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
随机推荐
- 高版本sonar安装遇到的坑-sonar 6.6
最近安装了6.6版本的sonar,发现里面的坑还是很多,下面列举下遇到的坑 sonar插件地址:https://docs.sonarqube.org/display/PLUG/Plugin+Libra ...
- .NET中制做对象的副本(二)继承对象之间的数据拷贝
定义学生 /// <summary> /// 学生信息 /// </summary> public class Student { /// <summary> // ...
- python 历险记(四)— python 中常用的 json 操作
目录 引言 基础知识 什么是 JSON? JSON 的语法 JSON 对象有哪些特点? JSON 数组有哪些特点? 什么是编码和解码? 常用的 json 操作有哪些? json 操作需要什么库? 如何 ...
- v4l2文档之——color and format【转】
转自:https://blog.csdn.net/zoe6553/article/details/17715407 v4l2文档第五A--颜色与格式 颜色与格式这是不定期发布的关于写视频驱动程序 ...
- MHL技术剖析,比HDMI更强【转】
转自:http://blog.chinaunix.net/uid-22030783-id-3294750.html MHL这个只是经常听说,没有见过的东西,现在已经非常火热了,我们才刚刚开始做,人家三 ...
- kafka系列八、kafka消息重复和丢失的场景及解决方案分析
消息重复和丢失是kafka中很常见的问题,主要发生在以下三个阶段: 生产者阶段 broke阶段 消费者阶段 一.生产者阶段重复场景 1.根本原因 生产发送的消息没有收到正确的broke响应,导致pro ...
- R-TREE
原文地址:http://blog.csdn.net/sunmenggmail/article/details/8122743 1984年,加州大学伯克利分校的Guttman发表了一篇题为“R-tree ...
- 解决walle报错:宿主机代码检出检测出错,请确认svn用户名密码无误
使用walle检测报错: 查看日志 # tail -f /tmp/walle/walle-20161010.log 报错: 2016-10-10 14:20:30 -- --------------- ...
- myeclipse的web项目导入到eclipse中
环境组成: java8 eclipse4.4.2 for javaee tomcat 7.0.61 1.导入myeclipse项目 2.设置JDK环境 3.将导入的项目修改为web项目 将myecli ...
- mysql数据库基于LVM快照的备份
lvm-snapshot: 基于LVM快照的备份 1.事务日志跟数据文件必须在同一个卷上 2.创建快照卷之前,要请求mysql的全局锁,在快照创建完成之后释放锁 3 ...