CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:Saksunoob
Submission time:2024-11-01 17:37:53 +0200
Language:Rust (2021)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5details
#20.02 s2, 3, 4, 5details
#30.17 s3, 4, 5details
#40.24 s4, 5details
#50.24 s5details
#60.24 s5details

Code

use std::{collections::HashSet, io};

fn is_valid(sums: &Vec<Vec<Vec<usize>>>, unique_letters: &HashSet<usize>, 
    y_start: usize, x_start: usize, y_end: usize, x_end: usize ) -> bool {

        for l in unique_letters {
            let mut count = sums[y_end][x_end][*l];
            if y_start != 0 && x_start != 0 {
                count += sums[y_start-1][x_start-1][*l];
            }
            if y_start != 0 {
                count -= sums[y_start-1][x_end][*l]
            }
            if x_start != 0 {
                count -= sums[y_end][x_start-1][*l];
            }

            if count == 0 {
                return false;
            }
        }

        return true;
    }

fn main() {
    let mut n_str = String::new();
    io::stdin().read_line(&mut n_str).unwrap();
    n_str = n_str.trim().to_string();
    let n: usize = n_str.parse().unwrap();

    let mut rows: Vec<Vec<usize>> = Vec::with_capacity(n);
    let mut unique_letters = HashSet::new();

    for _ in 0..n {
        let mut row_str = String::new();
        io::stdin().read_line(&mut row_str).unwrap();
        row_str = row_str.trim().to_string();
        rows.push(Vec::with_capacity(n));

        for c in row_str.chars() {
            let c_int = (c as usize)-65;
            unique_letters.insert(c_int);
            rows.last_mut().unwrap().push(c_int);
        }
    }

    let mut sums: Vec<Vec<Vec<usize>>> = vec![vec![vec![0; 26]; n]; n];
    for row in 0..n {
        for col in 0..n {
            sums[row][col][rows[row][col]] = 1;
            for l in unique_letters.iter() {
                if row != 0 {
                    sums[row][col][*l] += sums[row-1][col][*l];
                }
                if col != 0 {
                    sums[row][col][*l] += sums[row][col-1][*l];
                }
                if row != 0 && col != 0 {
                    sums[row][col][*l] -= sums[row-1][col-1][*l];
                }
            }
        }
    }

    let mut count = 0;

    'y: for y_start in 0..n {
        for x_start in 0..n {
            if !is_valid(&sums, &unique_letters, y_start, x_start, n-1, n-1) {
                continue 'y;
            }

            let mut last_y = y_start;
            let mut x_end = n-1;

            loop {

                let mut low = last_y;
                let mut high = n-1;

                while low < high {
                    let mid = (low + high) / 2;

                    if !is_valid(&sums, &unique_letters, y_start, x_start, mid, x_end) {
                        low = mid+1;
                    } else {
                        high = mid;
                    }
                    
                }

                if !is_valid(&sums, &unique_letters, y_start, x_start, low, x_end) {
                    break;
                }
                
                last_y = low;

                let mut low = x_start;
                let mut high = x_end;

                while low < high {
                    let mid = (low + high) / 2;

                    if !is_valid(&sums, &unique_letters, y_start, x_start, last_y, mid) {
                        low = mid+1;
                    } else {
                        high = mid;
                    }
                }
                count +=(n-last_y)*(x_end-low+1);
                if low == x_start {
                    break;
                }
                x_end = low-1;
            }
        }
    }
    println!("{}", count);
}

Test details

Test 1

Group: 1, 2, 3, 4, 5

Verdict:

input
54
4 4 0
3 1 3
3 2 2
4 0 4
...

correct output
0
0
0
0
0
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551603', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 2

Group: 2, 3, 4, 5

Verdict:

input
284
6 1 0
5 0 2
7 1 5
7 7 5
...

correct output
0
0
35280
0
36720
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551605', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 3

Group: 3, 4, 5

Verdict:

input
841
19 3 12
19 19 13
19 7 13
20 11 15
...

correct output
40291066
0
0
0
0
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551600', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 4

Group: 4, 5

Verdict:

input
1000
15 12 6
7 1 6
44 4 26
6 6 5
...

correct output
0
5040
494558320
0
340694548
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551600', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 5

Group: 5

Verdict:

input
1000
892 638 599
966 429 655
1353 576 1140
1403 381 910
...

correct output
0
0
0
249098285
0
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551607', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 6

Group: 5

Verdict:

input
1000
2000 1107 508
2000 1372 249
2000 588 65
2000 1739 78
...

correct output
750840601
678722180
744501884
159164549
868115056
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 26 but the index is 18446744073709551601', input/code.rs:51:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace