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,问是 ...
随机推荐
- centos7安装telnet
yum list |grep telnet yum install telnet.x86_64 安装后再测试
- MySQL与宿主Linux之间交互式执行命令
在MySQL里面执行Linux的命令并返回结果 system commands root@localhost 11:36:23> system cal March 2017 Su Mo Tu W ...
- Python发邮件的小脚本
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText mailto_list = ['hitwh_Gy ...
- ;(function ($, undefined){ })(jQuery); 的使用及说明
对于很多初学者来说很难明白这表示什么,下边我将为大家介绍其相应的作用. 1.代码最前面的分号,可以防止多个文件压缩合并以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2.匿名函数(func ...
- VxWorks Fuzzing 之道:VxWorks 工控实时操作系统漏洞挖掘调试与利用揭秘
转载:freebuf 0×00 前言 关于VxWorks,这里引用44CON议题<攻击 VxWorks:从石器时代到星际>探究 一文章中的介绍: VxWorks 是世界上使用最广泛的一种在 ...
- [HNOI2009]最小圈 (二分答案+负环)
题面:[HNOI2009]最小圈 题目描述: 考虑带权的有向图\(G=(V,E)\)以及\(w:E\rightarrow R\),每条边\(e=(i,j)(i\neq j,i\in V,j\in V) ...
- maven私服内容补充
1.添加阿里云中央仓库 注意Download Remote Indexes选项为True 1.登陆nexus私服(默认账号密码:admin/admin123) 2.点击右侧Repositories 3 ...
- 20165227《网络对抗技术》Exp0 Kali安装 Week1
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 kali下载:镜像文件通过同学获得 kali具体安装步骤: 打开VMware,点击新建虚拟机,进行创建 创建完成 ...
- 使用SpringSecurity保护方法应用
(1)pom添加依赖 <dependency> <groupId>org.springframework.security</groupId> <artifa ...
- 015_NGINX作为WebSocket Proxy的设置
产研那边有通过nginx代理进行长连接的需求,咱们都知道默认nginx只支持短连接的,使用长连接需要单独配置 一. websocket协议提供创建一种支持在server和client之前双向通信的we ...