CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Intersections
Sender:eyong002
Submission time:2024-11-11 01:20:59 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int hv_intersection()':
input/code.cpp:30:18: error: 'typeof' was not declared in this scope
   30 |             for (typeof(s.begin()) it=s.lower_bound(make_pair(c.p1.y,-1));it!=s.end() && it->y<=c.p2.y; it++) count++;
      |                  ^~~~~~
input/code.cpp:30:75: error: 'it' was not declared in this scope; did you mean 'i'?
   30 |             for (typeof(s.begin()) it=s.lower_bound(make_pair(c.p1.y,-1));it!=s.end() && it->y<=c.p2.y; it++) count++;
      |                                                                           ^~
      |                                                                           i

Code

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

#define x second
#define y first
const int MAX = 1e5;
typedef pair<int,int >point;

struct event
{
    point p1,p2;
    int type;
    event() {};
    event(point p1,point p2, int type) : p1(p1), p2(p2),type(type) {};
};
int n,e;
event events[MAX];
bool compare(event a, event b){
    return a.p1.x<b.p1.x;
}
set<point >s;

int hv_intersection(){
    int count = 0;
    for (int i=0;i<e;++i){
        event c = events[i];
        if (c.type==0) s.insert(c.p1);
        else if (c.type==1) s.erase(c.p2);
        else{
            for (typeof(s.begin()) it=s.lower_bound(make_pair(c.p1.y,-1));it!=s.end() && it->y<=c.p2.y; it++) count++;
        }
    }
    return count;
}

int main (){
    cin >> n;
    int p1x,p1y,p2x,p2y;

    for (int i=0;i<n;++i) {
        cin >> p1x >> p1y >> p2x >> p2y;
        if (p1x==p2x){
            events[e++] = event(make_pair(p1y,p1x), make_pair(p2y,p2x), 2);
        }
        else{
            events[e++] = event(make_pair(p1y,p1x), make_pair(p2y,p2x), 0);
            events[e++] = event(make_pair(p2y,p2x), make_pair(p1y,p1x), 1);
        }
    }
    sort(events, events+e, compare);
    cout << hv_intersection();
    return 0;
}