CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Intersections
Sender:fabiank
Submission time:2024-11-14 16:26:24 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.02 sdetails
#38ACCEPTED0.02 sdetails
#39ACCEPTED0.01 sdetails
#40ACCEPTED0.02 sdetails

Compiler report

input/code.cpp: In function 'long long int ford_fulkerson(std::vector<std::vector<long long int> >&, int, int)':
input/code.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (int i = 1; i < path_reversed.size(); i++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = a; i < b; i++)

// Type Aliases for 1D and 2D vectors with initialization
#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val
#define ll long long
#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val))              // 2D vector of ints (n x m), initialized to val
#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val

using namespace std;

void print_vector(vector<int> &x)
{
    for (int v : x)
    {
        cout << v << " ";
    }
    cout << "\n";
}

void print_matrix(vector<vector<int>> &matrix)
{
    cout << "\n"
         << "----------------" << "\n";
    for (vector<int> row : matrix)
    {
        print_vector(row);
    }
    cout << "\n"
         << "----------------" << "\n";
}

int calc_max_digit(int n)
{
    int max_digit = 0;
    while (n > 0 && max_digit < 9)
    {
        int digit = n % 10;
        if (digit > max_digit)
        {
            max_digit = digit;
        }
        n /= 10;
    }
    return max_digit;
}

// edges as edge list for outgoing node as pairs (end, cost)
vector<ll> dijkstras(int start_point, vector<vector<pair<int, int>>> edges)
{
    int n = edges.size();
    vector<bool> processed(n, false);
    vector<ll> distances(n, LLONG_MAX);
    distances[start_point] = 0;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, start_point});
    while (!pq.empty())
    {
        int curr = pq.top().second;
        pq.pop();
        if (processed[curr])
        {
            continue;
        }
        processed[curr] = true;
        ll distance = distances[curr];

        for (pair<int, int> edge : edges[curr])
        {

            if (distance + edge.second < distances[edge.first])
            {
                distances[edge.first] = distance + edge.second;
                pq.push({-distances[edge.first], edge.first});
            }
        }
    }
    return distances;
}

int bfs_edmondson_karp(const vector<vector<ll>> &connections,
                       const int source, const int target, vector<int> &path_reversed)
{
    int n = connections.size();

    queue<pair<int, ll>> queue;
    queue.push({source, LLONG_MAX});
    vector<int> predecessor(n, -2);
    predecessor[source] = -1;

    while (!queue.empty())
    {
        int current = queue.front().first;
        ll current_bottleneck = queue.front().second;
        queue.pop();

        if (current == target)
        {
            while (current != -1)
            {
                path_reversed.push_back(current);
                current = predecessor[current];
            }
            return current_bottleneck;
        }

        for (int edge_end = 0; edge_end < n; edge_end++)
        {
            ll edge_cap = connections[current][edge_end];
            if (edge_cap > 0 && predecessor[edge_end] == -2)
            {
                predecessor[edge_end] = current;
                queue.push({edge_end, min(current_bottleneck, edge_cap)});
            }
        }
    }

    return -1;
}

ll ford_fulkerson(vector<vector<ll>> &residual_graph, const int source, const int target)
{
    ll flow = 0;

    while (true)
    {
        vector<int> path_reversed;
        int path_capacity = bfs_edmondson_karp(residual_graph, source, target, path_reversed);

        if (path_capacity < 0)
        {
            break;
        }

        flow += path_capacity;
        for (int i = 1; i < path_reversed.size(); i++)
        {
            int edge_end = path_reversed[i - 1];
            int edge_start = path_reversed[i];
            // reduce forwards edge
            residual_graph[edge_start][edge_end] -= path_capacity;
            assert(residual_graph[edge_start][edge_end] >= 0);
            // add to backwards edge
            residual_graph[edge_end][edge_start] += path_capacity;
            assert(residual_graph[edge_end][edge_start] >= 0);
        }
    }
    return flow;
}

bool dfs(int n, const vector<vector<int>> snakes, vector<bool> &visited, vector<int> path, int start, int target)
{
    if (start == target)
    {
        path.push_back(target);
        return true;
    }
    for (int i = n; n >= 1; n--)
    {
        if (!visited[i] && !snakes[start][i])
        {
            if (dfs(n, snakes, visited, path, i, target))
            {
                path.push_back(start);
                return true;
            }
        }
    }
    return false;
}

vector<int> z(const string &s)
{
    int n = s.size();
    vector<int> z(n);
    z[0] = n;
    int x = 0, y = 0;
    for (int k = 1; k < n; k++)
    {
        z[k] = max(0, min(z[k - x], y - k + 1));
        while (k + z[k] < n && s[z[k]] == s[k + z[k]])
        {
            // while there is a potential longer match and characters coincide
            x = k;
            y = k + z[k];
            z[k]++;
        }
    }
    return z;
}

typedef long long C;
typedef complex<C> P;

C cross(P a, P b)
{
    return (conj(a) * b).imag();
}

bool is_between(C a, C b, C c)
{
    return min(a, b) <= c && c <= max(a, b);
}

bool check_intersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
    P p1 = P(x1, y1);
    P p2 = P(x2, y2);
    P p3 = P(x3, y3);
    P p4 = P(x4, y4);

    if (p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4)
    {
        return true;
    }

    if (cross(p2 - p1, p3 - p1) == 0 && cross(p2 - p1, p4 - p1) == 0)
    {
        return (is_between(p1.real(), p2.real(), p3.real()) && is_between(p1.imag(), p2.imag(), p3.imag())) ||
               (is_between(p1.real(), p2.real(), p4.real()) && is_between(p1.imag(), p2.imag(), p4.imag())) ||
               (is_between(p3.real(), p4.real(), p1.real()) && is_between(p3.imag(), p4.imag(), p1.imag())) ||
               (is_between(p3.real(), p4.real(), p2.real()) && is_between(p3.imag(), p4.imag(), p2.imag()));
    }

    C cross1 = cross(p2 - p1, p3 - p1);
    C cross2 = cross(p2 - p1, p4 - p1);
    C cross3 = cross(p4 - p3, p1 - p3);
    C cross4 = cross(p4 - p3, p2 - p3);

    return (cross1 * cross2 < 0) && (cross3 * cross4 < 0);
}

enum EventType
{
    HStart,
    Vertical,
    HStop
};

struct Event
{
    EventType type;
    int x;
    int y1;
    int y2;
};

bool compare_Events(const Event &event1, const Event &event2)
{
    if (event1.x == event2.x)
    {
        return event1.type < event2.type; // ensures that start before vertical before stop
    }
    return event1.x < event2.x;
}

// // Binary Indexed Tree (Fenwick Tree) implementation
// class BIT
// {
// public:
//     vector<int> tree;
//     int n;

//     BIT(int size) : n(size)
//     {
//         tree.resize(n + 2, 0);
//     }

//     void update(int idx, int delta)
//     {
//         while (idx <= n)
//         {
//             tree[idx] += delta;
//             idx += idx & -idx;
//         }
//     }

//     int query(int idx)
//     {
//         int res = 0;
//         while (idx > 0)
//         {
//             res += tree[idx];
//             idx -= idx & -idx;
//         }
//         return res;
//     }

//     int range_query(int l, int r)
//     {
//         return query(r) - query(l - 1);
//     }
// };

int main()
{
    int n;
    cin >> n;

    // vector<Event> events;

    int vertical = 0;
    int horizontal = 0;
    for (int i = 0; i < n; i++)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        if (x1 == x2)
        {
            // events.push_back({Vertical, x1, y1, y2});
            // cout << "vertical\n";
            vertical++;
        }
        else
        {
            //     if (x1 > x2)
            //     {
            //         swap(x1, x2);
            //         swap(y1, y2);
            //     }
            //     events.push_back({HStart, x1, y1, -1});
            //     events.push_back({HStop, x2, y2, -1});
            // cout << "horizontal\n";
            horizontal++;
        }
    }
    // sort(events.begin(), events.end(), compare_Events);
    cout << (ll)horizontal * vertical << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
1
1 1 -1 1

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
1
-1 1 1 1

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
2
2 1 -2 1
-1 2 -2 2

correct output
0

user output
0

Test 4

Verdict: ACCEPTED

input
2
-2 2 2 2
-1 0 -2 0

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

input
3
3 2 -3 2
-1 3 -2 3
-3 -1 -2 -1

correct output
0

user output
0

Test 6

Verdict: ACCEPTED

input
3
-3 3 2 3
-1 0 -3 0
-1 2 -1 -1

correct output
2

user output
2

Test 7

Verdict: ACCEPTED

input
4
4 2 -4 2
-2 4 -3 4
-4 -1 -3 -1
-1 0 3 0

correct output
0

user output
0

Test 8

Verdict: ACCEPTED

input
4
-4 3 3 3
-1 4 4 4
-4 1 -1 1
-1 0 -1 -2

correct output
3

user output
3

Test 9

Verdict: ACCEPTED

input
5
5 2 -5 2
-2 5 -4 5
-4 -1 -3 -1
-2 0 4 0
...

correct output
0

user output
0

Test 10

Verdict: ACCEPTED

input
5
-5 4 4 4
-1 5 5 5
-5 1 -1 1
-2 0 -2 -2
...

correct output
6

user output
6

Test 11

Verdict: ACCEPTED

input
5
5 -2 5 -3
-5 -5 -5 -1
5 1 -4 1
1 -1 1 0
...

correct output
6

user output
6

Test 12

Verdict: ACCEPTED

input
6
6 3 -6 3
-3 6 -5 6
-5 -1 -4 -1
-2 2 -1 2
...

correct output
5

user output
5

Test 13

Verdict: ACCEPTED

input
6
-6 5 4 5
-2 6 0 6
1 -1 3 -1
0 0 -3 0
...

correct output
8

user output
8

Test 14

Verdict: ACCEPTED

input
7
7 3 6 3
-6 -3 7 -3
-4 -6 -2 -6
-2 -2 3 -2
...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
7
-7 6 5 6
-2 7 0 7
2 -2 4 -2
0 0 -3 0
...

correct output
10

user output
10

Test 16

Verdict: ACCEPTED

input
10
10 5 9 5
-8 -4 10 -4
-6 -9 -2 -9
-2 -3 4 -3
...

correct output
9

user output
9

Test 17

Verdict: ACCEPTED

input
10
-7 -10 9 -10
9 -1 9 0
-4 -4 -7 -4
4 3 -8 3
...

correct output
24

user output
24

Test 18

Verdict: ACCEPTED

input
10
-9 4 -9 7
-8 0 1 0
-1 8 -1 -10
-10 -6 -5 -6
...

correct output
24

user output
24

Test 19

Verdict: ACCEPTED

input
10
8 1 8 -7
7 5 7 2
2 -6 2 -8
-6 -10 -6 4
...

correct output
24

user output
24

Test 20

Verdict: ACCEPTED

input
10
-9 8 7 8
-3 9 10 9
-9 2 -2 2
-3 0 -3 -4
...

correct output
24

user output
24

Test 21

Verdict: ACCEPTED

input
10
9 -4 9 -6
-9 -10 -9 -3
10 2 -8 2
2 -2 2 1
...

correct output
21

user output
21

Test 22

Verdict: ACCEPTED

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

correct output
24

user output
24

Test 23

Verdict: ACCEPTED

input
50
50 22 44 22
-38 -20 50 -20
-27 -41 -10 -41
-11 -16 17 -16
...

correct output
576

user output
576

Test 24

Verdict: ACCEPTED

input
50
-32 -48 44 -48
45 -7 45 -2
-18 -17 -35 -17
20 12 -38 12
...

correct output
621

user output
621

Test 25

Verdict: ACCEPTED

input
50
-43 21 -43 34
-38 1 7 1
-6 40 -6 -49
-46 -30 -25 -30
...

correct output
616

user output
616

Test 26

Verdict: ACCEPTED

input
50
40 5 40 -33
36 22 36 11
10 -29 10 -36
-28 -50 -28 20
...

correct output
624

user output
624

Test 27

Verdict: ACCEPTED

input
50
-45 37 33 37
-14 42 48 42
-41 11 -10 11
-15 2 -15 -1
...

correct output
576

user output
576

Test 28

Verdict: ACCEPTED

input
100
100 44 87 44
-75 -40 100 -40
-53 -82 -21 -82
-23 -31 34 -31
...

correct output
2464

user output
2464

Test 29

Verdict: ACCEPTED

input
100
-63 -95 87 -95
90 -13 90 -3
-36 -34 -69 -34
40 24 -76 24
...

correct output
2475

user output
2475

Test 30

Verdict: ACCEPTED

input
100
-86 42 -86 68
-76 2 14 2
-13 80 -13 -97
-92 -59 -51 -59
...

correct output
2500

user output
2500

Test 31

Verdict: ACCEPTED

input
100
81 9 81 -66
71 43 71 22
20 -57 20 -72
-55 -99 -55 40
...

correct output
2464

user output
2464

Test 32

Verdict: ACCEPTED

input
100
-89 75 67 75
-27 84 96 84
-82 22 -21 22
-29 4 -29 -3
...

correct output
2491

user output
2491

Test 33

Verdict: ACCEPTED

input
200
199 88 173 88
-149 -79 200 -79
-106 -163 -41 -163
-45 -62 68 -62
...

correct output
9984

user output
9984

Test 34

Verdict: ACCEPTED

input
200
-126 -190 173 -190
180 -26 180 -6
-72 -68 -139 -68
80 48 -152 48
...

correct output
9919

user output
9919

Test 35

Verdict: ACCEPTED

input
200
-172 83 -172 136
-152 4 28 4
-25 159 -25 -193
-184 -117 -101 -117
...

correct output
9991

user output
9991

Test 36

Verdict: ACCEPTED

input
200
161 19 161 -131
143 86 143 44
39 -114 39 -144
-110 -198 -110 80
...

correct output
9999

user output
9999

Test 37

Verdict: ACCEPTED

input
10000
9944 4407 8652 4407
-7438 -3954 9981 -3954
-5278 -8154 -2068 -8154
-2242 -3089 3395 -3089
...

correct output
24992431

user output
24992431

Test 38

Verdict: ACCEPTED

input
10000
-6299 -9482 8631 -9482
8955 -1294 8955 -305
-3589 -3393 -6912 -3393
3977 2386 -7601 2386
...

correct output
24999775

user output
24999775

Test 39

Verdict: ACCEPTED

input
10000
-8586 4163 -8586 6799
-7574 217 1386 217
-1259 7926 -1259 -9626
-9188 -5855 -5042 -5855
...

correct output
24999856

user output
24999856

Test 40

Verdict: ACCEPTED

input
10000
8013 945 8013 -6546
7113 4297 7113 2181
1951 -5678 1951 -7171
-5510 -9876 -5510 3969
...

correct output
24999996

user output
24999996