Please, another Queries on Array?

利用欧拉函数的计算方法, 用线段树搞一搞就好啦。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PLL pair<LL, LL>
  7. #define PLI pair<LL, int>
  8. #define PII pair<int, int>
  9. #define SZ(x) ((int)x.size())
  10. #define ull unsigned long long
  11.  
  12. using namespace std;
  13.  
  14. const int N = 4e5 + ;
  15. const int inf = 0x3f3f3f3f;
  16. const LL INF = 0x3f3f3f3f3f3f3f3f;
  17. const int mod = 1e9 + ;
  18. const double eps = 1e-;
  19. const double PI = acos(-);
  20.  
  21. int n, q, tot, b[N], prime[], mul[];
  22. LL S[];
  23. char op[];
  24.  
  25. bool ok(int x) {
  26. for(int i = ; i * i <= x; i++)
  27. if(x % i == ) return false;
  28. return true;
  29. }
  30.  
  31. int Power(int a, int b) {
  32. int ans = ;
  33. while(b) {
  34. if(b & ) ans = 1ll * ans * a % mod;
  35. a = 1ll * a * a % mod; b >>= ;
  36. }
  37. return ans;
  38. }
  39.  
  40. int a[N << ], lazy[N << ];
  41. LL mask[N << ], mlazy[N << ];
  42.  
  43. #define lson l, mid, rt << 1
  44. #define rson mid + 1, r, rt << 1 | 1
  45.  
  46. inline void pull(int rt) {
  47. a[rt] = 1ll * a[rt << ] * a[rt << | ] % mod;
  48. mask[rt] = mask[rt << ] | mask[rt << | ];
  49. }
  50.  
  51. void push(int rt, int l, int mid, int r) {
  52. if(lazy[rt] != ) {
  53. a[rt << ] = 1ll * a[rt << ] * Power(lazy[rt], mid - l + ) % mod;
  54. a[rt << | ] = 1ll * a[rt << | ] * Power(lazy[rt], r - mid) % mod;
  55. lazy[rt << ] = 1ll * lazy[rt << ] * lazy[rt] % mod;
  56. lazy[rt << | ] = 1ll * lazy[rt << | ] * lazy[rt] % mod;
  57. lazy[rt] = ;
  58. }
  59. if(mlazy[rt] != ) {
  60. mask[rt << ] |= mlazy[rt]; mask[rt << | ] |= mlazy[rt];
  61. mlazy[rt << ] |= mlazy[rt]; mlazy[rt << | ] |= mlazy[rt];
  62. mlazy[rt] = ;
  63. }
  64. }
  65.  
  66. void build(int l, int r, int rt) {
  67. lazy[rt] = ;
  68. mlazy[rt] = ;
  69. if(l == r) {
  70. a[rt] = b[l];
  71. mask[rt] = S[b[l]];
  72. return;
  73. }
  74. int mid = l + r >> ;
  75. build(lson); build(rson);
  76. pull(rt);
  77. }
  78.  
  79. void update(int L, int R, int mul, int l, int r, int rt) {
  80. if(l >= L && r <= R) {
  81. a[rt] = 1ll * a[rt] * Power(mul, r - l + ) % mod;
  82. lazy[rt] = 1ll * lazy[rt] * mul % mod;
  83. mask[rt] |= S[mul];
  84. mlazy[rt] |= S[mul];
  85. return;
  86. }
  87. int mid = l + r >> ;
  88. push(rt, l, mid, r);
  89. if(L <= mid) update(L, R, mul, lson);
  90. if(R > mid) update(L, R, mul, rson);
  91. pull(rt);
  92. }
  93.  
  94. PLI query(int L, int R, int l, int r, int rt) {
  95. if(l >= L && r <= R) return mk(mask[rt], a[rt]);
  96. int mid = l + r >> ;
  97. push(rt, l, mid, r);
  98. PLI ans = mk(, );
  99. if(L <= mid) {
  100. PLI tmp = query(L, R, lson);
  101. ans.fi |= tmp.fi;
  102. ans.se = 1ll * ans.se * tmp.se % mod;
  103. }
  104. if(R > mid) {
  105. PLI tmp = query(L, R, rson);
  106. ans.fi |= tmp.fi;
  107. ans.se = 1ll * ans.se * tmp.se % mod;
  108. }
  109. return ans;
  110. }
  111.  
  112. int main() {
  113. for(int i = ; i <= ; i++)
  114. if(ok(i)) prime[tot++] = i;
  115. for(int i = ; i < tot; i++)
  116. mul[i] = ( - Power(prime[i], mod - ) + mod) % mod;
  117. for(int i = ; i <= ; i++)
  118. for(int j = ; j < tot && prime[j] <= i; j++)
  119. if(i % prime[j] == ) S[i] |= 1ll << j;
  120. scanf("%d%d", &n, &q);
  121. for(int i = ; i <= n; i++) scanf("%d", &b[i]);
  122. build(, n, );
  123. while(q--) {
  124. scanf("%s", op);
  125. if(op[] == 'T') {
  126. int L, R; scanf("%d%d", &L, &R);
  127. PLI ret = query(L, R, , n, );
  128. int ans = ret.se; LL mask = ret.fi;
  129. for(int i = ; i < tot; i++)
  130. if(mask >> i & ) ans = 1ll * ans * mul[i] % mod;
  131. printf("%d\n", ans);
  132. } else {
  133. int L, R, x;
  134. scanf("%d%d%d", &L, &R, &x);
  135. update(L, R, x, , n, );
  136. }
  137. }
  138. return ;
  139. }
  140.  
  141. /*
  142. */

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. centos7安装telnet

    yum list |grep telnet yum install telnet.x86_64 安装后再测试

  2. MySQL与宿主Linux之间交互式执行命令

    在MySQL里面执行Linux的命令并返回结果 system commands root@localhost 11:36:23> system cal March 2017 Su Mo Tu W ...

  3. Python发邮件的小脚本

    # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText mailto_list = ['hitwh_Gy ...

  4. ;(function ($, undefined){ })(jQuery); 的使用及说明

    对于很多初学者来说很难明白这表示什么,下边我将为大家介绍其相应的作用. 1.代码最前面的分号,可以防止多个文件压缩合并以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2.匿名函数(func ...

  5. VxWorks Fuzzing 之道:VxWorks 工控实时操作系统漏洞挖掘调试与利用揭秘

    转载:freebuf 0×00 前言 关于VxWorks,这里引用44CON议题<攻击 VxWorks:从石器时代到星际>探究 一文章中的介绍: VxWorks 是世界上使用最广泛的一种在 ...

  6. [HNOI2009]最小圈 (二分答案+负环)

    题面:[HNOI2009]最小圈 题目描述: 考虑带权的有向图\(G=(V,E)\)以及\(w:E\rightarrow R\),每条边\(e=(i,j)(i\neq j,i\in V,j\in V) ...

  7. maven私服内容补充

    1.添加阿里云中央仓库 注意Download Remote Indexes选项为True 1.登陆nexus私服(默认账号密码:admin/admin123) 2.点击右侧Repositories 3 ...

  8. 20165227《网络对抗技术》Exp0 Kali安装 Week1

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 kali下载:镜像文件通过同学获得 kali具体安装步骤: 打开VMware,点击新建虚拟机,进行创建 创建完成 ...

  9. 使用SpringSecurity保护方法应用

    (1)pom添加依赖 <dependency> <groupId>org.springframework.security</groupId> <artifa ...

  10. 015_NGINX作为WebSocket Proxy的设置

    产研那边有通过nginx代理进行长连接的需求,咱们都知道默认nginx只支持短连接的,使用长连接需要单独配置 一. websocket协议提供创建一种支持在server和client之前双向通信的we ...