QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#714721 | #6236. 购票 | TAMREF | 30 | 7ms | 6200kb | Rust | 8.6kb | 2024-11-06 03:56:44 | 2024-11-06 03:56:45 |
Judging History
answer
use std::{array::from_fn, rc::Rc};
#[derive(Clone, Copy)]
pub struct Line {
a: i64,
b: i64,
}
impl Line {
fn new(a: i64, b: i64) -> Self {
Self { a, b }
}
fn aligned(&self, r: &Self, rr: &Self) -> bool {
((r.b - self.b) as i128 * (self.a - rr.a) as i128
- (self.a - r.a) as i128 * (rr.b - self.b) as i128)
< 0
}
fn eval(&self, x: i128) -> i128 {
self.a as i128 * x + self.b as i128
}
}
#[derive(Clone)]
pub struct Node {
val: Line,
prev: Vec<Rc<Node>>,
}
impl Node {
fn new(l: &Line) -> Self {
Node {
val: *l,
prev: vec![],
}
}
fn link(&mut self, prev: Rc<Node>) {
self.prev.push(Rc::clone(&prev));
for i in 0.. {
if let Some(anc) = self.prev[i].prev.get(i) {
self.prev.push(Rc::clone(anc));
} else {
break;
}
}
assert!(self.prev.len() < 30);
}
}
mod persistent_cht {
use crate::*;
pub fn insert(mut top: Rc<Node>, line: Line) -> Rc<Node> {
let mut node = Node::new(&line);
while !top.prev.is_empty() {
let prev = top.prev[0].val;
if !prev.aligned(&top.val, &line) {
top = Rc::clone(&top.prev[0]);
} else {
break;
}
}
node.link(top);
// eprintln!("prev len = {}", node.prev.len());
Rc::new(node)
}
pub fn max_eval(mut top: Rc<Node>, x: i128) -> i128 {
if top.prev.is_empty() {
return top.val.eval(x);
}
let l = top.prev.len() - 1;
for k in (0..=l).rev() {
if let Some(anc) = top.prev.get(k) {
if let Some(panc) = anc.prev.first() {
if panc.val.eval(x) > anc.val.eval(x) {
top = Rc::clone(anc);
}
}
}
}
top.val
.eval(x)
.max(top.prev.get(0).map_or(i128::MIN, |p| p.val.eval(x)))
}
}
struct SegNode {
node: Rc<Node>,
left: Option<Rc<SegNode>>,
right: Option<Rc<SegNode>>,
}
impl SegNode {
fn insert(now: Option<Rc<SegNode>>, s: usize, e: usize, x: usize, l: &Line) -> Rc<SegNode> {
Rc::new({
if let Some(now) = now {
let node = persistent_cht::insert(Rc::clone(&now.node), *l);
let (left, right) = if s != e {
let m = (s + e) >> 1;
if x <= m {
(
Some(Self::insert(
now.left.as_ref().map(|x| Rc::clone(x)),
s,
m,
x,
l,
)),
now.right.as_ref().map(|x| Rc::clone(x)),
)
} else {
(
now.left.as_ref().map(|x| Rc::clone(x)),
Some(Self::insert(
now.right.as_ref().map(|x| Rc::clone(x)),
m + 1,
e,
x,
l,
)),
)
}
} else {
(None, None)
};
Self { node, left, right }
} else {
let (left, right) = if s != e {
let m = (s + e) >> 1;
if x <= m {
(
Some(Self::insert(
None,
s,
m,
x,
l,
)),
None,
)
} else {
(
None,
Some(Self::insert(
None,
m + 1,
e,
x,
l,
)),
)
}
} else {
(None, None)
};
Self {
node: Rc::new(Node::new(l)),
left: left,
right: right,
}
}
})
}
fn query_max(
now: Option<Rc<SegNode>>,
s: usize,
e: usize,
l: usize,
r: usize,
x: i128,
) -> i128 {
// eprintln!(
// "node ok?: {}, s, e = [{}, {}], [l, r] = [{}, {}], x = {}",
// now.is_some(),
// s,
// e,
// l,
// r,
// x
// );
if let Some(now) = now {
let ret = {
if r < s || e < l {
i128::MIN
} else if l <= s && e <= r {
// eprintln!(
// "evaluating CHT for se=[{},{}], lr=[{},{}], x={}",
// s, e, l, r, x
// );
persistent_cht::max_eval(Rc::clone(&now.node), x)
} else {
let m = (s + e) >> 1;
Self::query_max(now.left.as_ref().map(|x| Rc::clone(&x)), s, m, l, r, x).max(
Self::query_max(
now.right.as_ref().map(|x| Rc::clone(x)),
m + 1,
e,
l,
r,
x,
),
)
}
};
// eprintln!("ans = {}", ret);
ret
} else {
i128::MIN
}
}
}
fn solve(stdin: &str) {
let mut tokens = stdin.split_whitespace();
let mut next = || tokens.next().unwrap();
let [n, _]: [usize; 2] = from_fn(|_| next().parse().unwrap());
let mut dep = vec![0; n];
let mut dep_comp = vec![0];
let d = (1..n)
.map(|i| {
let [f, s, p, q, mut l]: [i64; 5] = from_fn(|_| next().parse().unwrap());
let f = (f - 1) as usize;
assert!(f < i);
dep[i] = dep[f] + s;
l = l.min(dep[i]);
dep_comp.push(dep[i]);
dep_comp.push(dep[i] - l);
(f, p, q, l)
})
.collect::<Vec<_>>();
dep_comp.sort();
dep_comp.dedup();
let m = dep_comp.len();
let mut roots = vec![SegNode::insert(None, 0, m - 1, 0, &Line::new(0, 0))];
for (i, &(f, p, q, l)) in d.iter().enumerate() {
let i = i + 1; // index gap
let root = Rc::clone(&roots[f]);
let ei = dep_comp.binary_search(&dep[i]).unwrap();
let si = dep_comp.binary_search(&(dep[i] - l)).unwrap();
let mut base = (q as i128) + (dep[i] as i128) * (p as i128);
// eprintln!("base = {}", base);
base -= {
let qv = SegNode::query_max(Some(Rc::clone(&root)), 0, m - 1, si, ei, p as i128);
// eprintln!("query value = {}", qv);
qv
};
println!("{}", base);
roots.push(SegNode::insert(
Some(root),
0,
m - 1,
ei,
&Line::new(dep[i], -base as i64),
));
}
}
thread_local! {
static STDOUT: std::cell::RefCell<std::io::BufWriter<std::io::StdoutLock<'static>>> = std::cell::RefCell::new(std::io::BufWriter::with_capacity(1 << 17, std::io::stdout().lock()));
}
#[macro_export]
macro_rules! println {
($($t:tt)*) => {
STDOUT.with(|refcell| {
use std::io::*;
writeln!(refcell.borrow_mut(), $($t)*).unwrap();
});
};
}
#[macro_export]
macro_rules! print {
($($t:tt)*) => {
STDOUT.with(|refcell| {
use std::io::*;
write!(refcell.borrow_mut(), $($t)*).unwrap();
});
};
}
fn main() {
use std::io::*;
solve(&read_to_string(stdin()).unwrap());
STDOUT.with(|refcell| std::io::Write::flush(&mut *refcell.borrow_mut()).unwrap());
}
詳細信息
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 2116kb
input:
20 2 1 595431 0 3545381 595431 2 154087 25 0 170849 3 77016 86 0 782123 4 180999 12 1660623 948380 5 927727 0 27301085 1051700 6 149402 0 33306929 1167066 7 946649 48 2991155 2588304 8 19022 92 0 1070711 9 663052 0 46003818 1118009 10 836464 162335 167834728462 4029319 11 56248 195886 656934273302 2...
output:
3545381 7397556 14020932 10151228 37452313 43458157 91888464 93638488 137892282 303760004184 831941948416 1066087742411 1425893154880 1668334592207 2052151949662 2073566223966 3082367854154 2956603741427 4298403138179
result:
ok 19 numbers
Test #2:
score: 10
Accepted
time: 3ms
memory: 6088kb
input:
2000 0 1 721571 0 5206648 200000000000 2 643793 28 0 200000000000 3 55710 105 0 200000000000 4 536152 93 0 200000000000 5 904453 0 81892067 200000000000 6 869691 1 135499851 200000000000 7 521321 28 76775800 200000000000 8 106084 0 189989196 200000000000 9 829328 85 8800300 200000000000 10 721242 54...
output:
5206648 23232852 29082402 78276018 81892067 139231221 180853808 189989196 269164128 349203584 376499369 459350667 527252143 541762095 586547667 661519353 831039356 1006388170 1146131688 1277656761 1306508437 1498850990 1520085069 1613634398 1723642867 1912014433 1979804618 2235740266 2261356394 2367...
result:
ok 1999 numbers
Test #3:
score: 10
Accepted
time: 7ms
memory: 6200kb
input:
2000 3 1 608010 14 0 608010 2 571063 95 0 868847 3 267642 0 0 292924 4 900778 78 0 1980310 5 608031 0 0 1185692 6 974052 58 0 1249548 7 849689 85 0 4522536 8 302234 64 0 1204816 9 24087 104 0 3052911 10 329975 0 105934409 2132661 11 17449 36 74419876 2808429 12 38710 113 0 4145305 13 776986 99 15032...
output:
8512140 62763125 62763125 133023809 133023809 189518825 261742390 263241897 265746945 295453234 297353181 301727411 392966750 503069040 584580864 645577379 699483498 710773433 864596289 972692022 1155270923 1238899670 1399850312 1642535983 1827981065 1937506429 2158040915 2199552093 2309828212 24079...
result:
ok 1999 numbers
Test #4:
score: 0
Memory Limit Exceeded
input:
200000 0 1 540687 0 2923425 200000000000 2 262436 0 6450066 200000000000 3 76856 0 7743631 200000000000 4 991398 112 0 200000000000 5 80943 87 0 200000000000 6 243628 18 16790804 200000000000 7 613357 0 78921946 200000000000 8 885638 5 118051323 200000000000 9 316886 2 152924062 200000000000 10 8475...
output:
2923425 6450066 7743631 118780207 101037298 48221877 78921946 136526038 160947720 266037968 296746353 378356868 381851451 421509592 453812534 504175121 543762811 656966825 788829785 958353088 1117216445 1151292733 1235785955 1358451323 1439004892 1523378247 1637596259 1901105986 1959652816 223895618...
result:
Test #5:
score: 0
Memory Limit Exceeded
input:
200000 2 1 960309 29 0 960309 2 464482 45 0 749730 3 415113 0 6003507 977571 4 667738 94 0 2218799 5 952921 0 85902495 1952205 6 655418 47 18853387 1859709 7 118309 65 9244901 893931 8 644071 0 141364221 2371904 9 579470 127 0 3328473 10 916126 74 69770059 2496858 11 270938 30 94357625 1478135 12 54...
output:
27848961 48750651 33852468 96619840 119754963 169412996 179292119 237984061 311576751 406273279 441546296 517050304 559851875 716311370 836970689 850164770 953560258 1102882024 1121561062 1336127004 1560212588 1646150328 1737751161 1978591359 1990636271 2267622171 2456685707 2480031891 2709157987 28...
result:
Test #6:
score: 0
Memory Limit Exceeded
input:
200000 1 1 62114 0 38582 200000000000 2 560037 40 0 200000000000 3 844298 40 0 200000000000 4 598108 27 0 200000000000 5 198327 115 0 200000000000 6 321531 0 66792009 200000000000 7 330030 0 84939897 200000000000 8 236584 0 99289838 200000000000 9 57510 0 102947226 200000000000 10 891475 18 95380367...
output:
38582 22440062 56211982 54104543 76912148 66792009 84939897 99289838 102947226 168101149 257698165 332114775 392911838 439438399 510923887 559357851 576278054 726972802 888662618 917886245 1100304704 1156923965 1362016673 1427739410 1629259249 1736889404 1993523735 2162590537 2283675269 2578195096 2...
result:
Test #7:
score: 0
Memory Limit Exceeded
input:
200000 1 1 306724 75 0 200000000000 2 246075 0 3055868 200000000000 3 259997 105 0 200000000000 4 195729 36 0 200000000000 5 712059 67 0 200000000000 6 677198 95 0 200000000000 7 747833 88 0 200000000000 8 166256 58 0 200000000000 9 446938 137 0 200000000000 10 194548 43 10200536 200000000000 11 254...
output:
23004300 3055868 30355553 19462004 67169957 131503767 192572685 153056072 214286578 156290316 177087200 223478948 230827460 254372958 255406418 319359146 425647159 521075652 663032865 722593334 900309112 1003019677 1102434901 1160080432 1380694091 1567325971 1825690365 1910966724 2001079580 20855469...
result:
Test #8:
score: 0
Memory Limit Exceeded
input:
200000 1 1 593997 86 0 200000000000 2 633212 42 0 200000000000 3 392844 37 0 200000000000 4 791884 0 58174401 200000000000 5 986414 0 115487896 200000000000 6 464673 0 149229545 200000000000 7 413780 122 0 200000000000 8 237545 17 127049536 200000000000 9 957805 87 12322190 200000000000 10 927957 19...
output:
51083742 51542778 59941961 58174401 115487896 149229545 199710705 203793469 299444694 409614209 506464891 610735278 644322314 725872477 835631646 892148823 1040591157 1193174343 1416562234 1627939184 1716605960 1829497274 2109833815 2200787976 2348902608 2553960047 2775991177 2846879378 2998782120 3...
result:
Test #9:
score: 0
Time Limit Exceeded
input:
200000 3 1 954193 30 0 954193 2 881367 115 0 1762010 3 146908 10 0 554868 4 437217 84 0 668009 5 841885 114 0 1505741 6 100411 15 0 2176828 7 907898 0 30173897 1217590 8 138350 54 4535263 732764 9 466796 7 74922610 2623204 10 638906 140 0 1104724 11 619018 155 0 2366471 12 727561 47 166587651 432411...
output:
28625790 129982995 131452075 168178303 264153193 152144770 182318667 194324830 237658688 327105528 423053318 470665975 526880725 599968135 659919834 777620093 822592042 927991010 946281863 991490979 1034203167 1193709424 1268314728 1296632452 1510679436 1602257742 1758643852 1781283950 2014192789 23...
result:
Test #10:
score: 0
Time Limit Exceeded
input:
200000 3 1 164606 30 0 164606 2 457042 0 0 541153 3 336294 0 4238349 659934 4 472245 71 0 1267489 5 90955 19 3261401 628681 6 769346 0 47525173 2095686 7 276954 35 6158355 1621323 8 962997 89 0 2174898 9 711230 115 0 1195693 10 537692 0 76798598 1559422 11 813113 118 0 3117017 12 334021 106 1214310 ...
output:
4938180 4938180 9176529 42705924 23138730 52463353 65917585 151624318 233415768 228422916 324370250 351233430 412081957 538322786 563163046 624784960 719473776 751759923 867910422 976092032 1133336539 1172508898 1282075210 1321912186 1481545167 1682719020 1885052388 2048883058 2337337681 2442579829 ...