pub trait Magma: Sized + Clone { fn op(&self, rhs: &Self) -> Self; } pub trait Associative: Magma {} pub trait Unital: Magma { fn identity() -> Self; } pub trait Monoid: Magma + Associative + Unital {} impl Monoid for T {} pub struct Node { left: Option>>, right: Option>>, val: M, } impl Node { fn new() -> Self { Node { left: None, right: None, val: M::identity(), } } } pub fn value(node: &Option>>) -> M { match node.as_ref() { Some(n) => n.as_ref().val.clone(), None => M::identity() } } pub fn update_node(node: &mut Node, i: usize, x: M, l: usize, r: usize) { if l + 1 == r { node.val = x; } else { let m = (l + r) >> 1; if i < m { { if node.left.is_none() { node.left = Some(Box::new(Node::new())); } let left = node.left.as_mut().unwrap().as_mut(); update_node(left, i, x, l, m); } } else { { if node.right.is_none() { node.right = Some(Box::new(Node::new())); } let right = node.right.as_mut().unwrap().as_mut(); update_node(right, i, x, m, r); } } node.val = value(&node.left).op(&value(&node.right)); } } pub fn fold(node: &Node, a: usize, b: usize, l: usize, r: usize) -> M { if a <= l && r <= b { node.val.clone() } else if r <= a || b <= l { M::identity() } else { match node.left.as_ref() { Some(le) => fold(le, a, b, l, (l + r) >> 1), None => M::identity(), }.op(& match node.right.as_ref() { Some(ri) => fold(ri, a, b, (l + r) >> 1, r), None => M::identity(), }) } } pub struct DynamicSegmentTree { root: Node, n: usize, } impl DynamicSegmentTree { pub fn new(n: usize) -> Self { let mut sz = 1; while sz < n { sz = sz << 1; } DynamicSegmentTree { root: Node::new(), n: sz } } pub fn update(&mut self, i: usize, x: M) { update_node(&mut self.root, i, x, 0, self.n); } pub fn fold(&self, a: usize, b: usize) -> M { fold(&self.root, a, b, 0, self.n) } } #[derive(Clone)] struct A(usize); impl Magma for A { fn op(&self, a: &Self) -> Self { A(self.0 + a.0) } } impl Associative for A {} impl Unital for A { fn identity() -> Self { A(0) } } use std::io::Read; fn main() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let q: usize = iter.next().unwrap().parse().unwrap(); let mut dyseg = DynamicSegmentTree::::new(1_000_000_100); let mut ans = A(0); for _ in 0..q { let query: usize = iter.next().unwrap().parse().unwrap(); let a: usize = iter.next().unwrap().parse().unwrap(); let b: usize = iter.next().unwrap().parse().unwrap(); if query == 0 { let val = dyseg.fold(a, a + 1).op(&A(b)); dyseg.update(a, val); } else if query == 1 { ans = ans.op(&dyseg.fold(a, b + 1)); } } println!("{}", ans.0); }