CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Intersections
Sender:minghao
Submission time:2024-11-13 20:24:02 +0200
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.14 sdetails
#2ACCEPTED0.14 sdetails
#3ACCEPTED0.14 sdetails
#4ACCEPTED0.14 sdetails
#5ACCEPTED0.14 sdetails
#60.14 sdetails
#7ACCEPTED0.14 sdetails
#80.14 sdetails
#9ACCEPTED0.14 sdetails
#100.14 sdetails
#110.14 sdetails
#120.14 sdetails
#130.14 sdetails
#14ACCEPTED0.14 sdetails
#150.14 sdetails
#160.14 sdetails
#170.14 sdetails
#180.14 sdetails
#190.14 sdetails
#200.14 sdetails
#210.14 sdetails
#220.14 sdetails
#230.14 sdetails
#240.14 sdetails
#250.14 sdetails
#260.14 sdetails
#270.14 sdetails
#280.14 sdetails
#290.14 sdetails
#300.14 sdetails
#310.14 sdetails
#320.14 sdetails
#330.14 sdetails
#340.14 sdetails
#350.14 sdetails
#360.14 sdetails
#370.16 sdetails
#380.16 sdetails
#390.16 sdetails
#400.16 sdetails

Compiler report

input/code.cpp: In function 'void Test()':
input/code.cpp:146:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |     freopen("temp\\in.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

const int N = 100005, M = 1e6+1;

/*----------------------------------------------------------------------------*/
template<typename Value, typename Tag>
struct Node
{
    int id, l, r;
    Value val;
    Tag tag;

    inline int length()const
    {
        return r-l+1;
    }
    inline bool disjoint(const int& l_bor, const int& r_bor)const
    {
        return l_bor>r || r_bor<l;
    }// (l, r<l_bor)/(r_bor>l, r)
    inline bool subset(const int& l_bor, const int& r_bor)const
    {
        return l_bor<=l && r_bor>=r;
    }// [l_bor, l, r, r_bor]
};

template<typename Value, typename Tag>
class SegmentTree
{
  public:
    typedef Value (*FucMer)(Value, Value);
    typedef void (*FucCov)(Node<Value, Tag>*, Tag);
    SegmentTree(const FucMer f1, const FucCov f2, Value val, Tag tag)
        : Merge(f1), Cover(f2), val_zero_(val), tag_zero_(tag) {}
    void Init(const int& size, const vector<Value>& v)
    {
        l_range_=1, r_range_=size;
        nodes_.resize(size<<2);
        Build(1, l_range_, r_range_, v);
    }

    inline Node<Value, Tag>* identify(const int& p)
    {
        return &nodes_[p];
    }
    inline Node<Value, Tag>* l_child(const Node<Value, Tag>* p)
    {
        return &nodes_[p->id<<1];
    }// 2*id for the left child
    inline Node<Value, Tag>* r_child(const Node<Value, Tag>* p)
    {
        return &nodes_[p->id<<1|1];
    }// 2*id+1 for the right child

    void Modify(const int& pos, const Tag& tag)
    {
        Modify(identify(1), pos, pos, tag);
    }// point change
    void Modify(const int& l_bor, const int& r_bor, const Tag& tag)
    {
        Modify(identify(1), l_bor, r_bor, tag);
    }// section change
    Value Query(const int& pos)
    {
        return Query(identify(1), pos, pos);
    }// point query
    Value Query(const int& l_bor, const int& r_bor)
    {
        return Query(identify(1), l_bor, r_bor);
    }// section query

  private:
    const FucMer Merge;
    const FucCov Cover;
    const Value val_zero_;
    const Tag tag_zero_;
    int l_range_, r_range_;
    vector<Node<Value, Tag>> nodes_;

    inline void PushUp(Node<Value, Tag>* p)
    {
        p->val=Merge(l_child(p)->val, r_child(p)->val);
    }
    inline void PushDown(Node<Value, Tag>* p)
    {
        Cover(l_child(p), p->tag);
        Cover(r_child(p), p->tag);
        p->tag=tag_zero_;
    }

    void Build(int cur, int l_cur, int r_cur, const vector<Value>& v)
    {
        Node<Value, Tag> *p=identify(cur);
        p->id=cur, p->l=l_cur, p->r=r_cur;
        p->tag=tag_zero_;
        if(l_cur>r_cur) return;
        if(l_cur==r_cur) { p->val=v[l_cur]; return; }
        int mid=(l_cur+r_cur)>>1;
        Build(cur<<1, l_cur, mid, v);
        Build(cur<<1|1, mid+1, r_cur, v);
        PushUp(p);
    }

    void Modify(Node<Value, Tag> *p, const int& l_bor, const int& r_bor,
                const Tag& tag)
    {
        if(p->disjoint(l_bor, r_bor)) return;
        if(p->subset(l_bor, r_bor)) { Cover(p, tag); return; }
        if(p->tag!=tag_zero_) PushDown(p);
        Modify(l_child(p), l_bor, r_bor, tag);
        Modify(r_child(p), l_bor, r_bor, tag);
        PushUp(p);
    }

    Value Query(Node<Value, Tag>* p, const int& l_bor, const int& r_bor)
    {
        if(p->disjoint(l_bor, r_bor)) return val_zero_;
        if(p->subset(l_bor, r_bor))   return p->val;
        if(p->tag!=tag_zero_) PushDown(p);
        return Merge(Query(l_child(p), l_bor, r_bor), 
                     Query(r_child(p), l_bor, r_bor));
    } 
};
/*----------------------------------------------------------------------------*/

auto h1=[](int a, int b) -> int { return a + b; };
auto h2=[](Node<int, int>* p, int x) { p->val += x*p->length(), p->tag += x; };
SegmentTree<int, int> h_sum(h1, h2, 0, 0);

typedef pair<pair<int, int>, int> P;
vector<P> vertical, horizon;

bool CompareLast(const P& a, const P& b) {
    return a.second < b.second;
}
struct CompareMiddle {
    bool operator()(const P& a, const P& b) const {
        return a.first.second > b.first.second; 
    }
};

void Test()
{
    freopen("temp\\in.txt", "r", stdin);
}
int main()
{
    // Test();
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a1, b1, a2, b2;
        cin >> a1 >> b1 >> a2 >> b2;
        a1 += M, a2 += M, b1 += M, b2 += M;
        if(a1 == a2) {
            if(b1 > b2) swap(b1, b2);
            vertical.push_back(make_pair(make_pair(b1, b2), a1));
        } else {
            if(a1 > a2) swap(a1, a2);
            horizon.push_back(make_pair(make_pair(a1, a2), b1));
        }
    }
    vector<int> zeros(2*M, 0);
    h_sum.Init(2*M, zeros);

    sort(horizon.begin(), horizon.end());
    sort(vertical.begin(), vertical.end(), CompareLast);

    priority_queue<P, vector<P>, CompareMiddle> list;
    auto it = horizon.begin();
    P now_p, now_q;
    int now = 0, sum = 0;
    pair<int, int> temp;
    for(auto p : vertical)
    {
        temp = p.first;
        now = p.second;
        // Delete old ones
        while(!list.empty() && list.top().first.second < now)
        {
            now_p = list.top(); list.pop();
            h_sum.Modify(now_p.second, -1);
        }
        // Enter new ones
        while(it != horizon.end())
        {
            now_q = *it;
            if(now_q.first.first > now) break;
            h_sum.Modify(now_q.second, 1);
            list.push(now_q);
            it++;
        }
        sum += h_sum.Query(temp.first, temp.second);
    }
    cout << sum;

    return 0;
}

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
4

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
2

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
3

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
3

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
5

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
2

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
4

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
4

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
6

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
38

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
49

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
91

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
79

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
65

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
339

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
277

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
256

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
276

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
264

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
1167

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
950

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
1095

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
1026

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
2765302

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
2769822

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
2802799

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
2748552