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. Eclipse中将web项目自动发布到Tomcat webapps下(转)

    A:FileàDynamic Web Project[工程名:test] B:右键WebContent,New-->Jsp File C:右键test,Run AsàRun on Serverà ...

  2. springboot+rabbitmq整合示例程

    关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...

  3. Table Dragger - 简单的 JS 拖放排序表格插件

    Table Dragger 是一个极简的实现拖放排序的表格插件,纯 JavaScript 库,不依赖 jQuery.用于构建操作方便的拖放排序功能,超级容易设置,有平滑的动画,支持触摸事件. 在线演示 ...

  4. sock_ntop.c

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in ...

  5. 二、主目录 Makefile 分析(2)

    2.7 编译选项---config.mk 代码 163 164 行 # load other configuration include $(TOPDIR)/config.mk 此段就是包含顶层目录下 ...

  6. string中substr,find函数使用

    2.string函数 find:某子串的起始位(0开始),函数的第二个参数使用代表从该位开始的后缀 substr:1) x开始的连续y位 2) x开始的后缀 #include<bits/stdc ...

  7. luogu P3304 [SDOI2013]直径

    树的直径两遍dfs救星了 至于一定在直径中的边数,可以发现这些边一定是连续的(不然你两条直径中间能有空挡?),然后,如果某个点往下有多条直径,那么这条点以下都不算入答案.所以以直径分别两端点为根,找出 ...

  8. 使用Jupyter lab前应该读的几篇文章

    知乎上的一篇文章: 如何优雅的使用Jupyter? Jupyter Lab原来还有如下使用方式: 执行Shell命令 Hintland(提示命令).snippets(插入代码段).一键美化代码等功能( ...

  9. <video>标签:视频播放器动态设置src

    HTML代码 <div  id="my_div">    <video id="my_video"  width="600" ...

  10. Freemarker list 的简单使用

    freemarker list (长度,遍历,下标,嵌套,排序) 1. freemarker获取list的size : Java ArrayList<String> list = new ...