AtCoder Beginner Contest 278 D

https://atcoder.jp/contests/abc278/tasks/abc278_d

HashMapを使えば簡単ですね。

// All Assign Point Add
#![allow(non_snake_case)]

use std::collections::HashMap;

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}

fn read_input() -> (Vec<usize>, Vec<Vec<usize>>) {
    let _N: usize = read();
    let A = read_vec();
    let Q: usize = read();
    let queries = (0..Q).map(|_| read_vec()).collect::<Vec<Vec<usize>>>();
    (A, queries)
}

struct Array {
    base: usize,
    M: HashMap<usize, usize>
}

impl Array {
    fn query(&mut self, q: Vec<usize>) {
        match q[0] {
            1 => self.set_all_value(q[1]),
            2 => self.set_value(q[1], q[2]),
            3 => println!("{}", self.get_value(q[1])),
            _ => ()
        }
    }
    
    fn set_all_value(&mut self, x: usize) {
        self.base = x;
        self.M = HashMap::<usize, usize>::new()
    }
    
    fn set_value(&mut self, i: usize, x: usize) {
        let v = self.M.entry(i).or_insert(0);
        *v += x
    }
    
    fn get_value(&self, i: usize) -> usize {
        match self.M.get(&i) {
            Some(&e) => e + self.base,
            None     => self.base
        }
    }
}

fn f(A: Vec<usize>, queries: Vec<Vec<usize>>) {
    let M = (0..A.len()).map(|i| (i+1, A[i])).collect::<HashMap<usize, usize>>();
    let mut a = Array { base: 0, M: M };
    for q in queries.into_iter() {
        a.query(q)
    }
}

fn main() {
    let v = read_input();
    f(v.0, v.1)
}