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,问是 ...
随机推荐
- 〖C语言学习笔记 〗(一) HelloWorld
前言 本文为c基础入门学习笔记 正文 HelloWorld #include <stdio.h> //标准输出流 int main() //每种语言都有一个执行入口,main方法就是其一 ...
- SQL 语言类型
结构化查询语言(Structured Query Language),简称SQL,是数据库编程的核心语言. SQL的发展是从1974年开始的,其发展过程如下: 1974年 - 由Boyce和Chamb ...
- python2的比较函数,cmp
class Shu(object): def __init__(self,ss): self.ss = ss def __str__(self): return '(%s: %s)' % (self. ...
- canny 算子python实现
1. Canny介绍 Canny算子与Marr(LoG)边缘检测方法类似,也属于是先平滑后求导数的方法.John Canny研究了最优边缘检测方法所需的特性,给出了评价边缘检测性能优劣的三个指标: 1 ...
- shell正常运行,加入定时任务执行失败
例如简单的ifconfig命令,在shell中运行成功,但是在crontab 中执行失败. 定位原因:环境变量 解决方案: whereis ifconfig 然后在shell中加入: PATH=PAT ...
- MySQL日志——Undo | Redo【转】
本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版 ...
- Python3学习笔记19-继承和多态
在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承, 新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Sup ...
- C++ virtual函数重写,在继承的时候没有在函数前写virtual关键字也依然是虚函数吗?
比如: class Base { Base() {}; ~Base() {}; virtual void Init(); }; class Derived:public Base { virtual ...
- VS C# xamarin 开发android 调试正常 发布分发后运行闪退出错
我强烈推荐大家如果不是很有必要就不要引用一些.NET STD的库,比如json库newtonsoft.JSON,直接引用官方的system.Json就足够了,否则会导致体积变得巨大 好了废话不多说,这 ...
- linux /proc目录说明(访问内核数据结构,修改内核设置)
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...