CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:snude
Submission time:2024-10-16 15:26:41 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.74 sdetails
#7--details
#8--details
#90.74 sdetails
#100.74 sdetails
#11ACCEPTED0.00 sdetails
#120.74 sdetails

Compiler report

input/code.cpp: In function 'void print_matrix(std::vector<std::vector<int> >, std::string)':
input/code.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         for (int i = 0; i < in.size(); i++) {
      |                         ~~^~~~~~~~~~~
input/code.cpp:33:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |                 for (int j = 0; j < in[i].size(); j++) {
      |                                 ~~^~~~~~~~~~~~~~
input/code.cpp: In function 'void build_sparse_table(std::vector<int>&, std::vector<std::vector<int> >&)':
input/code.cpp:60:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for (int i = 0; i < in.size(...

Code

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// Debug printing
#ifdef DEBUG
#define deb(fmt, args...) printf("DEBUG: %d: " fmt, __LINE__, ##args)
#else
#define deb(fmt, args...)
#endif
void print_array(vector<int> in, const string title = "Vector")
{
cout << title << " [\n";
for (const auto &el : in) {
cout << el << " ";
}
cout << "\n] END\n";
}
void print_matrix(vector<vector<int> > in, const string title = "Matrix")
{
cout << title << " [\n";
for (int i = 0; i < in.size(); i++) {
for (int j = 0; j < in[i].size(); j++) {
cout << in[i][j] << " ";
}
cout << "\n";
}
cout << "] END\n";
}
void dfs(int s, vector<bool> visited, vector<vector<int>> adj, vector<int> &traversal)
{
if (visited[s]) {
return;
};
visited[s] = true;
traversal.push_back(s);
for (auto u : adj[s]) {
dfs(u, visited, adj, traversal);
traversal.push_back(s);
}
}
void build_sparse_table(vector<int> &in, vector<vector<int> > &sparse)
{
int rows = in.size() % 2 == 0 ? in.size() / 2 : in.size() / 2 + 1;
sparse.clear(); // clear table
sparse.push_back(vector<int>()); // needed rows
for (int i = 0; i < in.size(); i++) {
sparse[0].push_back(in[i]);
}
for (int i = 1; i < rows; i++) {
sparse.push_back(vector<int>()); // needed rows
int len = 1
<< (i - 1); // len * 2 is the interval for this round
for (int j = 0; j < in.size() - len; j++) {
sparse[i].push_back(
min(sparse[i - 1][j], sparse[i - 1][j + len]));
}
}
}
int query_sparse_table(int a, int b, vector<vector<int> > &sparse)
{
int L = min(a, b);
int R = max(a, b);
int num = 1;
while (num < R - L)
num <<= 1;
num >>= 1; // To get the number below the range width
int row = num >> 1; // to get the row number
return min(sparse[row][a], sparse[row][b - num]);
}
int main(int argc, char *argv[])
{
// Read the input parameters
int n, q;
cin >> n >> q;
// Read values from one line
vector<vector<int>> kids(n + 1, vector<int>());
int tmp;
for (int i = 2; i < n + 1; i++) {
cin >> tmp;
kids[tmp].push_back(i);
}
// print_matrix(kids);
vector<bool> visited(n, false);
vector<int> traversal;
dfs(1, visited, kids, traversal);
// print_array(traversal);
// vector<int> traversal = { 1, 3, 4, 8, 6, 1, 4, 2 };
vector<vector<int> > sparse(1, vector<int>());
build_sparse_table(traversal, sparse);
// print_matrix(sparse);
// Read pairs from multiple lines
vector<pair<int, int>> input_pairs(q);
int l, r;
for (int i = 0; i < q; i++) {
cin >> l >> r;
cout << query_sparse_table(l - 1, r - 1, sparse) << "\n";
}
return 0;
}

Test details

Test 1

Verdict:

input
10 10
1 2 3 4 5 6 7 8 9
6 9
8 10
10 3
...

correct output
6
8
3
1
8
...

user output
(empty)

Test 2

Verdict:

input
10 10
1 1 1 1 1 1 1 1 1
1 7
3 4
4 1
...

correct output
1
1
1
1
1
...

user output
(empty)

Test 3

Verdict:

input
10 10
1 1 1 1 2 3 4 4 1
1 8
2 7
8 3
...

correct output
1
1
1
1
1
...

user output
(empty)

Test 4

Verdict:

input
10 10
1 1 3 1 2 2 5 3 9
7 2
7 6
3 9
...

correct output
2
2
3
1
1
...

user output
(empty)

Test 5

Verdict:

input
10 10
1 2 3 2 5 3 2 2 4
6 1
1 3
1 9
...

correct output
1
1
1
2
2
...

user output
(empty)

Test 6

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
74862
8750
16237
72298
58111
...

user output
(empty)

Test 7

Verdict:

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
(empty)

Test 8

Verdict:

input
200000 200000
1 2 1 2 3 2 1 6 3 1 10 12 13 4...

correct output
1
2
2
2
1
...

user output
(empty)

Test 9

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2796
633
633
151
2690
...

user output
(empty)

Test 10

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
365
73
103
365
216
...

user output
(empty)

Test 11

Verdict: ACCEPTED

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
2

Test 12

Verdict:

input
200000 200000
1 1 2 3 4 5 6 7 8 9 10 11 12 1...

correct output
27468
6353
27468
6353
6353
...

user output
(empty)