Codeforces 594D REQ 线段树
把询问离线, 我们从n 到 1遍历过去的时候, 把(1 - 1 / p)乘在最靠近当前位置的地方, 然后区间求乘积就好啦。
#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 = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; struct SegmentTree {
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
int a[N << ];
void build(int *b, int l, int r, int rt) {
if(l == r) {
a[rt] = b[l];
return;
}
int mid = l + r >> ;
build(b, lson); build(b, rson);
a[rt] = 1ll * a[rt << ] * a[rt << | ] % mod;
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
a[rt] = 1ll * a[rt] * val % mod;
return;
}
int mid = l + r >> ;
if(p <= mid) update(p, val, lson);
else update(p, val, rson);
a[rt] = 1ll * a[rt << ] * a[rt << | ] % mod;
}
int query(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return a[rt];
int mid = l + r >> ;
if(R <= mid) return query(L, R, lson);
else if(L > mid) return query(L, R, rson);
else return (1ll * query(L, R, lson) * query(L, R, rson)) % mod;
}
}; int n, q, a[N], mul[N], del[N], ans[N], Map[N];
vector<int> prime;
vector<int> fac[N];
vector<PII> qus[N];
SegmentTree seg;
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 main() {
for(int i = ; i < N; i++) {
if(SZ(fac[i])) continue;
prime.push_back(i);
for(int j = i; j < N; j += i)
fac[j].push_back(i);
}
for(auto& x : prime) {
mul[x] = ( - Power(x, mod - ) + mod) % mod;
del[x] = Power(mul[x], mod - );
}
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
seg.build(a, , n, );
scanf("%d", &q);
for(int i = ; i <= q; i++) {
int L, R; scanf("%d%d", &L, &R);
qus[L].push_back(mk(R, i));
}
for(int i = n; i >= ; i--) {
for(auto& t : fac[a[i]]) {
if(Map[t]) seg.update(Map[t], del[t], , n, );
seg.update(i, mul[t], , n, );
Map[t] = i;
}
for(auto& t : qus[i]) {
ans[t.se] = seg.query(i, t.fi, , n, );
}
}
for(int i = ; i <= q; i++) printf("%d\n", ans[i]);
return ;
} /*
*/
Codeforces 594D REQ 线段树的更多相关文章
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- codeforces 787D - Legacy 线段树优化建图,最短路
题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...
- Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)
题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...
- Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)
Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...
随机推荐
- zabbix添加对centos系统cpu使用率百分比的监控
cpu使用率key: system.cpu.util[] 在grafana现实的时候配置,单位选择percent(0-100),范围0-100
- PHP header 允许跨域请求
2018-1-29 17:36:14 星期一 header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Methods ...
- boost.Asio lib
Documentation for Boost.Asio http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio.html https://w ...
- 自定义redis连接池(字典操作)
pool=redis.ConnectionPool(host='127.0.0.1', port=6379,max_connections=1000)conn=redis.Redis(connecti ...
- Linux 的 OOM 终结者(Out Of Memory killer)
现在是早晨6点钟.已经醒来的我正在总结到底是什么事情使得我的起床闹铃提前了这么多.故事刚开始的时候,手机铃声恰好停止.又困又烦躁的我看了下手机,看看是不是我自己疯了把闹钟调得这么早,居然是早晨5点.然 ...
- linux之xxx 不在 sudoers 文件中,此事将被报告(转载)
linux中创建用户命令为:useradd 用户名, eg: useradd test 指定密码:passwd test 但是有时候我们需要使用test运行执行一些root用户才有权限执行的命令,此时 ...
- day02 编程语言和变量
编程语言 编程语言分为三种 机器语言:用计算机能听得懂的二进位制语言来写程序 汇编语言:用英文字母来代替二进位制来写的程序 高级语言(两种) 编译型语言(C语言):相当于谷歌翻译整个程序写好一次性写好 ...
- 高并发编程基础(java.util.concurrent包常见类基础)
JDK5中添加了新的java.util.concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为同步容器将所有对容器状态的访问都串行化了,这样保证了线程的安全性,所以这种方法 ...
- jquery----Ajax补充
jquery实现ajax请求 <script> //$.ajax的两种使用方式: //$.ajax(settings); //$.ajax(url,[settings]); $(" ...
- 2017-2018-2 20165314实验二《Java面向对象程序设计》实验报告
实验报告封面 实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习提交最后三个JUnit测试用例 ...