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
// Zadanie: grm (Gra mobilna)
// runda 1
// Marcin Machura <marcin.machura@hotmail.com>

use std::io::{self, Write, Read, BufWriter};
use std::process;

const C: i128 = 1_000_000_007;

fn main() {
    let stdout = io::stdout();
    let mut out = BufWriter::new(stdout.lock()); // buforowanie zapisu
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).expect("parsing!");
    let mut tokens = input.split_whitespace().filter_map(|s| s.parse::<i32>().ok());
    
    // Wczytaj n, q
    let n = tokens.next().unwrap_or(0);
    let q = tokens.next().unwrap_or(0);
    
    //  Wczytaj wydarzenia
    let mut events: Vec<(i32, i32)> = Vec::with_capacity(n as usize);
    for _ in 0..n {
        if let (Some(a), Some(b)) = (tokens.next(), tokens.next()) {
            events.push((a, b));
        }
    }
    
    // Wczytaj zapytania
    let mut queries: Vec<(i32, i32, i32)> = Vec::with_capacity(q as usize);
    for _ in 0..q {
        if let (Some(x), Some(l), Some(r)) = (tokens.next(), tokens.next(), tokens.next()) {
            queries.push((x, l, r));
        }
    }

    for (xx, l, r) in queries {
        let mut x = xx as i128;
        let mut overflow = false;
        for i in (l as usize)..(r as usize) {
            if overflow{
                if events[i].1 > 1 {
                    // Zawsze opłaca sie pomnozyc gdy jestesmy ponad modulo-limitem
                    x = x * (events[i].1 as i128);
                }
                else
                {
                    x = x + (events[i].0 as i128);
                }
                x = x % C;
            }
            else
            { // jesli nie było overflow, to po prostu symulujemy co się bardziej opłaca
                let x1 = x * (events[i].1 as i128);
                let x2 = x + (events[i].0 as i128);
                x = std::cmp::max(x1, x2);
                if x >= C {
                    overflow = true;
                    x = x % C;
                }
            }
        }
        writeln!(out, "{}", x).unwrap();
    }
    
    out.flush().expect("uho od sledzia");
    process::exit(0);
}