-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP18610.rs
150 lines (137 loc) · 4.52 KB
/
P18610.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Author : quickn (quickn.ga)
Email : quickwshell@gmail.com
*/
use std::str;
use std::io::{self, BufWriter, Write};
/* https://github.com/EbTech/rust-algorithms */
/// Same API as Scanner but nearly twice as fast, using horribly unsafe dark arts
/// **REQUIRES** Rust 1.34 or higher
pub struct UnsafeScanner<R> {
reader: R,
buf_str: Vec<u8>,
buf_iter: str::SplitAsciiWhitespace<'static>,
}
impl<R: io::BufRead> UnsafeScanner<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buf_str: Vec::new(),
buf_iter: "".split_ascii_whitespace(),
}
}
/// This function should be marked unsafe, but noone has time for that in a
/// programming contest. Use at your own risk!
pub fn token<T: str::FromStr>(&mut self) -> T {
loop {
if let Some(token) = self.buf_iter.next() {
return token.parse().ok().expect("Failed parse");
}
self.buf_str.clear();
self.reader
.read_until(b'\n', &mut self.buf_str)
.expect("Failed read");
self.buf_iter = unsafe {
let slice = str::from_utf8_unchecked(&self.buf_str);
std::mem::transmute(slice.split_ascii_whitespace())
}
}
}
}
use std::cmp::{max, min};
use std::collections::HashMap;
const MAX: usize = 100_001;
fn main() {
let (stdin, stdout) = (io::stdin(), io::stdout());
let (mut scan, mut sout) = (UnsafeScanner::new(stdin.lock()), BufWriter::new(stdout.lock()));
let n: usize = scan.token();
let mut a: Vec<(i32,u32)> = vec![(0,0);n];
let mut b: Vec<(i32,u32)> = vec![(0,0);n];
for i in 0..n {
a[i] = (scan.token(),i+1);
b[i] = (scan.token(),i+1);
}
a.sort();
b.sort();
// Calculate mü function
let mut mu: Box<[i8;MAX]> = Box::new([0;MAX]);
let mut is_composite: Box<[bool;MAX]> = Box::new([false;MAX]);
let mut primes: Vec<u32> = Vec::new();
mu[1] = 1;
for i in 2..(MAX as u32) {
if !is_composite[i as usize] {
primes.push(i);
mu[i as usize] = -1;
}
for j in primes.iter().take_while(|&j| i*j < (MAX as u32)) {
let val = i*j;
is_composite[val as usize] = true;
if i % j == 0 {
break;
} else {
mu[val as usize] = -mu[i as usize];
}
}
}
// Calculate set of divisors
// O(n^{\frac{3}{2}})
let mut divisors: Vec<Vec<u32>> = vec![vec![];n];
for i in 1..=n {
let sqrt_i = (i as f64).sqrt().floor() as usize;
for j in 1..=sqrt_i {
if i % j == 0 {
divisors[i-1].push(j as u32);
let v = i / j;
if v != j {
divisors[i-1].push(v as u32);
}
}
}
}
let f = |m: u32| -> u32 {
// (i, d) -> (l, r)
let mut hash: HashMap<(u32,u32),(u32,u32)> = HashMap::new();
let mut sum: i64 = 0;
for i in 1..=n {
for d in divisors[a[i-1].1-1] {
if let Some(&res) = hash.get(&(i-1,d)) {
} else {
let (mut res_l, mut res_r) = (0, 0);
let (mut l, mut r) = (0,i-1);
while l < r {
let m = (l+r)>>1;
if (a[i-1] as i32-b[m] as i32).abs() as u32 < m {
r = m;
} else {
l = m+1;
}
}
if l == 0 {
if (a[i-1] as i32-b[0] as i32).abs() as u32 >= m {
res_l = 1;
}
} else {
res_l = l+1;
}
l = min(i,n-1);
r = n-1;
while l < r {
let m = (l+r)>>1;
if (a[i-1] as i32-b[m] as i32).abs() as u32 < m {
l = m;
} else {
r = m-1;
}
}
if l == n-1 {
if (a[i-1] as i32-b[b-1] as i32).abs() as u32 >= m {
res_r = n;
}
} else {
res_r = l+1;
}
}
}
}
};
}