CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Intersections
Sender:Rasse
Submission time:2024-11-08 09:59:10 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.03 sdetails
#3ACCEPTED0.03 sdetails
#4ACCEPTED0.03 sdetails
#5ACCEPTED0.03 sdetails
#60.03 sdetails
#7ACCEPTED0.03 sdetails
#80.03 sdetails
#9ACCEPTED0.03 sdetails
#100.03 sdetails
#110.03 sdetails
#120.03 sdetails
#130.03 sdetails
#14ACCEPTED0.03 sdetails
#150.03 sdetails
#160.03 sdetails
#170.03 sdetails
#180.03 sdetails
#190.03 sdetails
#200.03 sdetails
#210.03 sdetails
#220.03 sdetails
#230.03 sdetails
#240.03 sdetails
#250.03 sdetails
#260.03 sdetails
#270.03 sdetails
#280.03 sdetails
#290.03 sdetails
#300.03 sdetails
#310.03 sdetails
#320.03 sdetails
#330.03 sdetails
#340.03 sdetails
#350.03 sdetails
#360.03 sdetails
#370.04 sdetails
#380.04 sdetails
#390.04 sdetails
#400.04 sdetails

Compiler report

input/code.cpp: In function 'void solve()':
input/code.cpp:96:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < events.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <climits>
#include <cmath>
#include <functional>
#include <type_traits>
#include <fstream>
#include <bitset>
#include <complex>
 
#define int long long
using namespace std;

int ST_GetRange(vector<int> &tree, int lowerInclusive, int upperInclusive) {
    lowerInclusive += tree.size() / 2; upperInclusive += tree.size() / 2;
    int s = 0; // 0 for SumTree and INT_MAX for MinTree
    while (lowerInclusive <= upperInclusive) {
        // Sum tree
        if (lowerInclusive%2 == 1) s += tree[lowerInclusive++];
        if (upperInclusive%2 == 0) s += tree[upperInclusive--];
        // Min tree
        //if (lowerInclusive%2 == 1) s = min(tree[lowerInclusive++], s);
        //if (upperInclusive%2 == 0) s = min(tree[upperInclusive--], s);

        lowerInclusive /= 2; upperInclusive /= 2;
    }
    return s;
}

void ST_Set(vector<int> &tree, int index, int newVal) {
    index += tree.size() / 2;
    
    tree[index] += newVal; // For adding
    //tree[index] = newVal; // For setting value

    for (index /= 2; index >= 1; index /= 2) {
        tree[index] = tree[2*index]+tree[2*index+1]; // Sum tree
        //tree[index] = min(tree[2*index], tree[2*index+1]); // Min tree
    }
}

// Create segment tree
vector<int> ST_Create(int valueCount) {
    int res = 1;
    while (res < valueCount) res <<= 1;
    vector<int> tree(2 * res, 0);

    // start from the end, from the second last element -> -2
    for (int i = res * 2 - 2; i > 0; i -= 2) {
        //tree[i/2] = min(tree[i], tree[i + 1]); // Min tree
        tree[i/2] = tree[i] + tree[i + 1]; // Sum tree
    }
    return tree;
}

 
#define cross(x, y) (((x)*conj(y)).imag())
#define sign(v) ((0 < (v)) - ((v) < 0))

void solve()
{
    int n;
    cin >> n;

    vector<int> segment_tree = ST_Create(1000001*2);

    vector<tuple<int, int, int>> events;
    events.reserve(2*n);

    for (int i = 0; i < n; i++)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if (y1 == y2)
        {
            events.push_back({x1, INT_MAX, y1});
            events.push_back({x2, INT_MIN, y1});
        }
        else
        {
            if (y1 > y2)
                swap(y1, y2);
            events.push_back({x1, y1, y2});
        }
    }
    sort(events.begin(), events.end());

    int res = 0;
    for (int i = 0; i < events.size(); i++)
    {
        auto [p1, p2, p3] = events[i];
        if (p2 == INT_MAX)
            ST_Set(segment_tree, p3+1000001, 1);
        else if (p2 == INT_MIN)
            ST_Set(segment_tree, p3+1000001, -1);
        else
        {
            res += ST_GetRange(segment_tree, p2+1000001+1, p3+1000001-1);
        }
    }

    cout << res;

}
 
 
signed main()
{
    ios::sync_with_stdio(0);
    //cin.tie(0);
 
    int t = 1;
 
    //cin >> t;
    //ifstream f("testcase.txt");
    //cin.rdbuf(f.rdbuf());
 
    for (int i = 0; i < t; i++)
        solve();
}

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:

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

correct output
2

user output
-1

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:

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

correct output
3

user output
0

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:

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

correct output
6

user output
1

Test 11

Verdict:

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

correct output
6

user output
0

Test 12

Verdict:

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

correct output
5

user output
-2

Test 13

Verdict:

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

correct output
8

user output
0

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:

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

correct output
10

user output
1

Test 16

Verdict:

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

correct output
9

user output
0

Test 17

Verdict:

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

correct output
24

user output
-2

Test 18

Verdict:

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

correct output
24

user output
2

Test 19

Verdict:

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

correct output
24

user output
-1

Test 20

Verdict:

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

correct output
24

user output
2

Test 21

Verdict:

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

correct output
21

user output
-1

Test 22

Verdict:

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

correct output
24

user output
-2

Test 23

Verdict:

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

correct output
576

user output
11

Test 24

Verdict:

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

correct output
621

user output
1

Test 25

Verdict:

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

correct output
616

user output
30

Test 26

Verdict:

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

correct output
624

user output
-24

Test 27

Verdict:

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

correct output
576

user output
26

Test 28

Verdict:

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

correct output
2464

user output
39

Test 29

Verdict:

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

correct output
2475

user output
37

Test 30

Verdict:

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

correct output
2500

user output
100

Test 31

Verdict:

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

correct output
2464

user output
27

Test 32

Verdict:

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

correct output
2491

user output
43

Test 33

Verdict:

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

correct output
9984

user output
124

Test 34

Verdict:

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

correct output
9919

user output
-85

Test 35

Verdict:

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

correct output
9991

user output
-50

Test 36

Verdict:

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

correct output
9999

user output
22

Test 37

Verdict:

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

correct output
24992431

user output
-42435

Test 38

Verdict:

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

correct output
24999775

user output
22045

Test 39

Verdict:

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

correct output
24999856

user output
12147

Test 40

Verdict:

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

correct output
24999996

user output
-37269