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? 线段树的更多相关文章

  1. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]

    Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...

  2. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  3. 暑假集训单切赛第一场 CF 266E More Queries to Array(线段树+二项式展开式)

    比赛时,第二题就是做的这个,当时果断没仔细考虑,直接用线段树暴力求.结果易想而知,超时了. 比赛后搜了搜题解,恍然大悟. 思路:显然用线段树,但是由于每次查询都会有变,所以不可能存储题目中的式子.   ...

  4. [Codeforces266E]More Queries to Array...——线段树

    题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...

  5. Codeforces 1114F(欧拉函数、线段树)

    AC通道 要点 欧拉函数对于素数有一些性质,考虑将输入数据唯一分解后进行素数下的处理. 对于素数\(p\)有:\(\phi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\) ...

  6. codeforces 671C Ultimate Weirdness of an Array 线段树+构造

    题解上说的很清楚了,我照着写的,表示膜拜题解 然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn) #include <stdio.h> #inc ...

  7. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  8. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  9. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

随机推荐

  1. C语言入门教程-(6)运算符

    1.运算符概述 运算符是一种编译器执行特定的数学或逻辑操作的符号.C语言提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 条件运算符 其他运算符 2.算术运算符 算术 ...

  2. 计算正多边形的面积 Gym - 101840G

    http://codeforces.com/gym/101840/attachments 题目大意:输入n,r,k .n代表往外扩张几次,r代表圆的内接圆半径,k代表多边形的边长.问你每次扩张多边形和 ...

  3. mysql 原理 ~ innodb恢复机制

    举例说明 机制 数据页A的lsn为100,数据页B的lsn为200,checkpoint lsn为150,系统lsn为300,表示当前系统已经更新到300,小于150的数据页已经被刷到磁盘上,因此数据 ...

  4. 上传程序Dictionary 字典 哈希--多读一写锁ReaderWriterLock

    //上传程序Dictionary 字典 哈希 /// <summary> /// 车辆控制信息哈斯表,Key是终端号,Value是车辆信息控制对象 /// </summary> ...

  5. Python 爬虫学习

    #coding:utf-8 #author:Blood_Zero ''' 1.获取网页信息 2.解决编码问题,通过charset库(默认不安装这个库文件) ''' import urllib impo ...

  6. sqlldr和sqludr使用笔记

    导出语句: 参数:file=aaa    生成文件的名字 导入语句: 导出语句会生成一个控制文件(XX.ctl),导入语句直接使用这个控制文件就可以 readsize=  控制缓存大小,控制文件里面的 ...

  7. ROS 多台计算机联网控制机器人

    0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...

  8. float/double 浮点数据*100精度丢失问题

    工作中微信支付碰到的一个问题,金额是float数字,微信参数需要分且必须是整数,所以*100的时候就有问题了 System.out.println(9.9f*100); //989.99994Syst ...

  9. ReLu、LeakyRelu、PReLu(转载)

    转载链接:http://blog.csdn.net/cham_3/article/details/56049205

  10. 使用 Linux 系统调用的内核命令【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-system-calls/ 探究 SCI 并添加自己的调用 Linux® 系统调用 —— 我们每天都在使 ...