結果
| 問題 |
No.3330 Many Point Chmax Range Sum
|
| ユーザー |
👑 |
| 提出日時 | 2025-06-02 19:56:07 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 2,074 ms / 3,500 ms |
| コード長 | 10,419 bytes |
| コンパイル時間 | 11,805 ms |
| コンパイル使用メモリ | 399,120 KB |
| 実行使用メモリ | 32,112 KB |
| 最終ジャッジ日時 | 2025-11-03 20:51:06 |
| 合計ジャッジ時間 | 58,375 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 21 |
ソースコード
pub trait SortD{ fn sort_d(&mut self); }
impl<T: Ord> SortD for Vec<T>{ fn sort_d(&mut self) {
self.sort_by(|u, v| v.cmp(&u));
} }
pub trait Mx{fn max(&self, rhs: Self)->Self;}
impl Mx for f64{ fn max(&self, rhs: Self)->Self{if *self < rhs{ rhs } else { *self } }}
pub trait Mi{ fn min(&self, rhs: Self)->Self; }
impl Mi for f64{ fn min(&self, rhs: Self)->Self{ if *self < rhs{ rhs } else { *self } } }
pub fn chmax<T: PartialOrd+Clone>(a: &mut T, b: &T){ if *a < *b{ *a = b.clone(); } }
pub fn chmin<T: PartialOrd+Clone>(a: &mut T, b: &T){ if *a > *b{ *a = b.clone(); } }
pub fn gcd(mut a: i64, mut b: i64)->i64{ if b==0{return a;}while b!=0{ let c = a;a = b;b = c%b; }a }
pub fn factorial_i64(n: usize)->(Vec<i64>, Vec<i64>){ let mut res = vec![1; n+1];let mut inv = vec![1; n+1];for i in 0..n{ res[i+1] = (res[i]*(i+1)as i64)%MOD; }inv[n] = mod_inverse(res[n], MOD);for i in (0..n).rev(){ inv[i] = inv[i+1]*(i+1) as i64%MOD; }(res, inv) }
pub fn floor(a:i64, b:i64)->i64{let res=(a%b+b)%b;(a-res)/b}
pub fn extended_gcd(a:i64,b:i64)->(i64,i64,i64)
{if b==0{(a,1,0)}else{let(g,x,y)=extended_gcd(b,a%b);(g,y,x-floor(a,b)*y)}}
pub fn mod_inverse(a:i64,m:i64)->i64{let(_,x,_) =extended_gcd(a,m);(x%m+m)%m}
pub fn comb(a: i64, b: i64, f: &Vec<(i64, i64)>)->i64{
if a<b{return 0;}else if b==0 || a==b{ return 1; }
else{let x=f[a as usize].0;
let y=f[(a-b) as usize].1;let z=f[b as usize].1;return((x*y)%MOD)*z%MOD;}}
pub fn factorial(x: i64)->Vec<(i64, i64)>{
let mut f=vec![(1i64,1i64),(1, 1)];let mut z = 1i64;
let mut inv = vec![0; x as usize+10];inv[1] = 1;
for i in 2..x+1{z=(z*i)%MOD;
let w=(MOD-inv[(MOD%i)as usize]*(MOD/i)%MOD)%MOD;
inv[i as usize] = w;
f.push((z, (f[i as usize-1].1*w)%MOD));}return f;}
pub fn fast_mod_pow(x: i64,p: usize, m: i64)->i64{
let mut res=1;let mut t=x;let mut z=p;while z > 0{
if z%2==1{res = (res*t)%m;}t = (t*t)%m;z /= 2; }res}
#[allow(unused_imports)]
use std::{
convert::{Infallible, TryFrom, TryInto as _}, fmt::{self, Debug, Display, Formatter,},
fs::{File}, hash::{Hash, Hasher}, iter::{Product, Sum}, marker::PhantomData,
ops::{Add, AddAssign, Sub, SubAssign, Div, DivAssign, Mul, MulAssign, Neg, RangeBounds},
str::FromStr, sync::{atomic::{self, AtomicU32, AtomicU64}, Once},
collections::{*, btree_set::Range, btree_map::Range as BTreeRange}, mem::{swap},
cmp::{self, Reverse, Ordering, Eq, PartialEq, PartialOrd},
thread::LocalKey, f64::consts::PI, time::Instant, cell::RefCell,
io::{self, stdin, Read, read_to_string, BufWriter, BufReader, stdout, Write},
};
#[allow(unused_imports)]
use proconio::{input, input_interactive, marker::{*}};
#[allow(unused_imports)]
//use rand::{thread_rng, Rng, seq::SliceRandom};
#[allow(unused_imports)]
//use ac_library::{*, ModInt998244353 as mint};
#[allow(dead_code)]
//type MI = StaticModInt<Mod998244353>;pub fn factorial_mint(n: usize)->(Vec<MI>, Vec<MI>){ let mut res = vec![mint::new(1); n+1];let mut inv = vec![mint::new(1); n+1];for i in 0..n{res[i+1] = res[i]*(i+1);}inv[n] = mint::new(1)/res[n];for i in (0..n).rev(){inv[i] = inv[i+1]*(i+1);}(res, inv)}
#[allow(dead_code)]
const INF: i64 = 1<<60;
#[allow(dead_code)]
const MOD: i64 = 998244353;
#[allow(dead_code)]
const D: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)];
#[allow(dead_code)]
const D2: [(usize, usize); 8] = [(1, 0), (1, 1), (0, 1), (!0, 1), (!0, 0), (!0, !0), (0, !0), (1, !0)];
// 逆元あり
pub trait SqrtDecomposition{
type S: Clone;
type T: Clone;
fn identity()->Self::S;
fn op(a: &Self::S, b: &Self::S)->Self::S;
fn inv(a: &Self::S)->Self::S;
}
pub struct SqrtDecompositionData<M> where M: SqrtDecomposition{
b: usize,
data: Vec<M::S>,
block: Vec<M::S>,
}
impl<M> SqrtDecompositionData<M> where M: SqrtDecomposition{
pub fn new(n: usize, q: usize)->Self{
let b = ((n as f64/(q as f64).sqrt()).ceil() as usize).max(1);
SqrtDecompositionData{
b,
data: vec![M::identity(); n],
block: vec![M::identity(); (n+b-1)/b],
}
}
pub fn from(a: &Vec<M::S>, q: usize)->Self{
let b = ((a.len() as f64/(q as f64).sqrt()).ceil() as usize).max(1);
let data = a.clone();
let mut block = vec![M::identity(); (a.len()+b-1)/b];
for (i, v) in a.iter().enumerate(){
block[i/b] = M::op(&block[i/b], v);
}
SqrtDecompositionData{
b, data, block
}
}
pub fn set(&mut self, p: usize, x: &M::S){
self.block[p/self.b] = M::op(&self.block[p/self.b], &M::inv(&self.data[p]));
self.data[p] = x.clone();
self.block[p/self.b] = M::op(&self.block[p/self.b], &x);
}
pub fn set_both(&mut self, p: usize, x: &M::S, b: &M::S){
self.data[p] = x.clone();
self.block[p/self.b] = b.clone();
}
pub fn get(&self, p: usize)->M::S{
self.data[p].clone()
}
pub fn get_b(&self, p: usize)->M::S{
self.block[p/self.b].clone()
}
pub fn prod(&self, l: usize, r: usize)->M::S{
let (bl, br) = ((l + self.b - 1) / self.b, r / self.b);
if bl >= br{
let mut res = M::identity();
for i in l..r{
res = M::op(&res, &self.data[i]);
}
res
} else {
let mut res = M::identity();
for i in l..bl * self.b {
res = M::op(&res, &self.data[i]);
}
for i in bl..br {
res = M::op(&res, &self.block[i]);
}
for i in self.b * br..r {
res = M::op(&res, &self.data[i]);
}
res
}
}
}
pub trait RollbackMoMonoid{
type S: Clone;
type T;
type U;
type V: Clone;
type X: Clone+Default;
fn init_t(n: usize, q: usize, a: &Vec<Self::S>, b: &Self::U)->Self::T;
fn increase(t: &mut Self::T, s: &Self::S);
fn snapshot(t: &mut Self::T);
fn rollback(t: &mut Self::T);
fn get(t: &Self::T, x: &Self::V)->Self::X;
}
pub fn solve_rollback_mo<M>(a: Vec<M::S>, x: &M::U, query: Vec<(usize, usize, M::V)>)->Vec<M::X> where M: RollbackMoMonoid{
let (n, q) = (a.len(), query.len());
let b = ((n as f64).sqrt() as usize).max(1);
let mut ans = vec![M::X::default(); q];
let mut qs = vec![Vec::new(); (n+b-1)/b];
let mut t = M::init_t(n, q, &a, x);
for (idx, (l, r, z)) in query.iter().enumerate(){
let (bl, br) = ((*l+b-1)/b, *r/b);
if bl >= br{
for i in *l..*r{
M::increase(&mut t, &a[i]);
}
ans[idx] = M::get(&t, z);
M::rollback(&mut t);
} else {
qs[bl].push((*r, *l, z.clone(), idx));
}
}
for i in 0..qs.len(){
qs[i].sort_by(|u, v| u.0.cmp(&v.0));
let st = (i+1)*b;
let mut right = st;
t = M::init_t(n, q, &a, x);
for (r, l, z, idx) in &qs[i]{
while right < *r{
M::increase(&mut t, &a[right]);
right += 1;
}
M::snapshot(&mut t);
for i in (*l..st).rev(){
M::increase(&mut t, &a[i]);
}
ans[*idx] = M::get(&mut t, z);
M::rollback(&mut t);
}
}
ans
}
struct SqMonoid;
impl SqrtDecomposition for SqMonoid{
type S = i64;
type T = i64;
fn identity() -> Self::S {
0
}
fn op(&a: &Self::S, &b: &Self::S) -> Self::S {
a+b
}
fn inv(&a: &Self::S) -> Self::S {
-a
}
}
struct T{
sq: SqrtDecompositionData<SqMonoid>,
hist: Vec<(usize, i64, i64)>,
}
struct MM;
impl RollbackMoMonoid for MM{
type S = (usize, i64);
type T = T;
type U = Vec<i64>;
type V = (usize, usize);
type X = i64;
fn init_t(_: usize, q: usize, _: &Vec<Self::S>, b: &Self::U) -> Self::T {
T{
sq: SqrtDecompositionData::<SqMonoid>::from(b, q),
hist: Vec::new(),
}
}
fn increase(t: &mut Self::T, &s: &Self::S) {
let (p, x) = s;
let px = t.sq.get(p);
let pb = t.sq.get_b(p);
t.hist.push((p, px, pb));
t.sq.set(p, &x.max(px));
}
fn snapshot(t: &mut Self::T) {
t.hist.clear()
}
fn rollback(t: &mut Self::T) {
while let Some((p, px, pb)) = t.hist.pop(){
t.sq.set_both(p, &px, &pb);
}
}
fn get(t: &Self::T, &z: &Self::V) -> Self::X {
let (l, r) = z;
t.sq.prod(l, r)
}
}
//use proconio::fastout;
//#[fastout]
fn main(){
input!{
n: usize, k: usize, q: usize,
a: [i64; n],
op: [(Usize1, i64); k],
query: [(Usize1, usize, Usize1, usize); q],
}
for x in solve(a, op, query){
println!("{}", x);
}
}
fn solve(a: Vec<i64>, op: Vec<(usize, i64)>, query: Vec<(usize, usize, usize, usize)>)->Vec<i64>{
let qs = query.into_iter().map(|(l, r, d, u)| (l, r, (d, u))).collect::<Vec<_>>();
solve_rollback_mo::<MM>(op, &a, qs)
}
/*
fn main() {
let mut rng = thread_rng();
for _ in 0..100{
let (n, k, q) = (rng.gen_range(1..500), rng.gen_range(1..500), rng.gen_range(1..500));
let mut a = Vec::new();
for _ in 0..n{
a.push(rng.gen_range(1..=1000000000));
}
let mut op = Vec::new();
for _ in 0..k{
op.push((rng.gen_range(0..n), rng.gen_range(1..=1000000000)))
}
let mut query = Vec::new();
for _ in 0..q{
let (mut l, mut r) = (rng.gen_range(1..=k), rng.gen_range(1..=k));
(l, r) = (l.min(r), l.max(r));
let (mut d, mut u) = (rng.gen_range(1..=n), rng.gen_range(1..=n));
(d, u) = (d.min(u), d.max(u));
query.push((l-1, r, d-1, u));
}
assert_eq!(solve(a.clone(), op.clone(), query.clone()), naive(a.clone(), op.clone(), query.clone()));
}
}
fn naive(a: Vec<i64>, op: Vec<(usize, i64)>, query: Vec<(usize, usize, usize, usize)>)->Vec<i64>{
let mut ans = vec![0; query.len()];
for (idx, &(l, r, d, u)) in query.iter().enumerate(){
let mut b = a.clone();
for i in l..r{
let (p, x) = op[i];
b[p] = b[p].max(x);
}
let mut res = 0;
for i in d..u{
res += b[i];
}
ans[idx] = res;
}
ans
}
*/