Submission #818407

# Submission time Handle Problem Language Result Execution time Memory
818407 2023-08-10T04:42:34 Z 반딧불(#10131) Dragon 2 (JOI17_dragon2) C++17
60 / 100
4000 ms 12892 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Frac{
    ll a, b;
    Frac(){}
    Frac(ll a, ll b): a(a), b(b){
        ll g = __gcd(a, b);
        a/=g, b/=g;
        if(b<0) a=-a, b=-b;
    }

    bool operator<(const Frac &r)const{
        return a*r.b < b*r.a;
    }
};

struct vector2{
    ll x, y;
    vector2(){}
    vector2(ll x, ll y): x(x), y(y){}

    vector2 operator+(const vector2 &r)const{
        return vector2(x+r.x, y+r.y);
    }

    vector2 operator-(const vector2 &r)const{
        return vector2(x-r.x, y-r.y);
    }

    ll cross(vector2 r)const{
        return x*r.y - y*r.x;
    }

    bool operator<(const vector2 &r)const{
        return cross(r) > 0;
    }
};

struct Query{
    int x, y, idx;
    Query(){}
    Query(int x, int y, int idx): x(x), y(y), idx(idx){}
    bool operator<(const Query &r)const{
        return x==r.x ? y<r.y : x<r.x;
    }
};

ll ccw(vector2 a, vector2 b){
    return a.cross(b);
}

ll ccw(vector2 a, vector2 b, vector2 c){
    return (b-a).cross(c-a);
}

inline ll sign(ll x){
    return x>0?1:-1;
}

int n, k, q;
vector2 arr[30002];
int group[30002];
ll calc[30002];
vector<int> groupList[30002][2];
vector2 ps, pe;

int ans[100002];
Query query[100002];

void input();
void operate();

int main(){
    input();
    operate();
}

void input(){
    scanf("%d %d", &n, &k);
    for(int i=1; i<=n; i++){
        scanf("%lld %lld %d", &arr[i].x, &arr[i].y, &group[i]);
    }
    scanf("%lld %lld %lld %lld %d", &ps.x, &ps.y, &pe.x, &pe.y, &q);
    for(int i=1; i<=q; i++){
        scanf("%d %d", &query[i].x, &query[i].y);
        query[i].idx = i;
    }
}

void makePositive(){ /// ps�� 0����, pe�� ������ 0 �̻� 90�� �̸����� ����
    for(int i=1; i<=n; i++) arr[i] = arr[i] - ps;
    pe = pe - ps, ps = ps - ps;
    bool cx = (pe.x < 0), cy = (pe.y < 0);
    for(int i=1; i<=n; i++){
        if(cx) arr[i].x = -arr[i].x;
        if(cy) arr[i].y = -arr[i].y;
    }
    if(cx) pe.x = -pe.x;
    if(cy) pe.y = -pe.y;
    if(pe.x==0){
        for(int i=1; i<=n; i++) swap(arr[i].x, arr[i].y);
        swap(pe.x, pe.y);
    }
}

void putIntoGroup(){ /// groupList�� ����ֱ�
    for(int i=1; i<=n; i++){
        if(ccw(ps, pe, arr[i]) > 0) groupList[group[i]][0].push_back(i);
        else groupList[group[i]][1].push_back(i);
    }
}

struct dat{
    int x, y, w;
    dat(){}
    dat(int x, int y, int w): x(x), y(y), w(w){}
    bool operator<(const dat &r)const{
        return x<r.x;
    }
};

struct Fenwick{
    int n;
    int tree[40002];

    void init(int _n){
        n = _n;
        for(int i=0; i<=n; i++) tree[i] = 0;
    }

    void add(int x, int y){
        while(x<=n){
            tree[x] += y;
            x += x&-x;
        }
    }

    int sum(int x){
        int ret = 0;
        while(x){
            ret += tree[x];
            x -= x&-x;
        }
        return ret;
    }

    int sum(int l, int r){
        return sum(r) - sum(l-1);
    }
} tree;

map<pair<int, int>, int> mp;
int processQuery(int gx, int gy){
    if(mp.find(make_pair(gx, gy)) != mp.end()) return mp[make_pair(gx, gy)];
    int ret = 0;
    { /// 00 ó��
        vector<vector2> va, vb;
        for(int x: groupList[gx][0]) va.push_back(arr[x] - ps), vb.push_back(arr[x] - pe);
        for(int x: groupList[gy][0]) va.push_back(arr[x] - ps), vb.push_back(arr[x] - pe);
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        vector<dat> vec;
        for(int x: groupList[gx][0]){
            vec.push_back(dat(lower_bound(va.begin(), va.end(), arr[x]-ps) - va.begin() + 1,
                              lower_bound(vb.begin(), vb.end(), arr[x]-pe) - vb.begin() + 1, 0));
        }
        for(int x: groupList[gy][0]){
            vec.push_back(dat(lower_bound(va.begin(), va.end(), arr[x]-ps) - va.begin() + 1,
                              lower_bound(vb.begin(), vb.end(), arr[x]-pe) - vb.begin() + 1, 1));
        }
        sort(vec.begin(), vec.end());
        tree.init((int)vec.size());
        for(dat tmp: vec){
            if(!tmp.w) ret += tree.sum(tmp.y, (int)vec.size());
            else tree.add(tmp.y, 1);
        }
    }

    { /// 11 ó��
        vector<vector2> va, vb;
        for(int x: groupList[gx][1]) va.push_back(arr[x] - ps), vb.push_back(arr[x] - pe);
        for(int x: groupList[gy][1]) va.push_back(arr[x] - ps), vb.push_back(arr[x] - pe);
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        vector<dat> vec;
        for(int x: groupList[gx][1]){
            vec.push_back(dat(lower_bound(va.begin(), va.end(), arr[x]-ps) - va.begin() + 1,
                              lower_bound(vb.begin(), vb.end(), arr[x]-pe) - vb.begin() + 1, 0));
        }
        for(int x: groupList[gy][1]){
            vec.push_back(dat(lower_bound(va.begin(), va.end(), arr[x]-ps) - va.begin() + 1,
                              lower_bound(vb.begin(), vb.end(), arr[x]-pe) - vb.begin() + 1, 1));
        }
        for(dat &p: vec) swap(p.x, p.y);
        sort(vec.begin(), vec.end());
        tree.init((int)vec.size());
        for(dat tmp: vec){
            if(!tmp.w) ret += tree.sum(tmp.y, (int)vec.size());
            else tree.add(tmp.y, 1);
        }
    }

    { /// 01 ó��
        vector<vector2> psv, pev;
        for(int x: groupList[gy][1]) psv.push_back(arr[x] - ps), pev.push_back(arr[x] - pe);
        sort(psv.begin(), psv.end()), sort(pev.begin(), pev.end());
        for(int x: groupList[gx][0]){
            int A = lower_bound(psv.begin(), psv.end(), ps - arr[x]) - psv.begin();
            int B = pev.end() - lower_bound(pev.begin(), pev.end(), pe - arr[x]);
            ret += (int)psv.size() - A - B;
        }
    }

    { /// 10 ó��
        vector<vector2> psv, pev;
        for(int x: groupList[gy][0]) psv.push_back(arr[x] - ps), pev.push_back(arr[x] - pe);
        sort(psv.begin(), psv.end()), sort(pev.begin(), pev.end());
        for(int x: groupList[gx][1]){
            int A = psv.end() - lower_bound(psv.begin(), psv.end(), ps - arr[x]);
            int B = lower_bound(pev.begin(), pev.end(), pe - arr[x]) - pev.begin();
            ret += (int)psv.size() - A - B;
        }
    }

    return mp[make_pair(gx, gy)] = ret;
}

void processQueries(){
    for(int i=1; i<=q; i++){
        ans[i] = processQuery(query[i].x, query[i].y);
    }
}

void operate(){
    makePositive();
    putIntoGroup();
    processQueries();
    for(int i=1; i<=q; i++) printf("%d\n", ans[i]);
}

Compilation message

dragon2.cpp: In function 'void input()':
dragon2.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
dragon2.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         scanf("%lld %lld %d", &arr[i].x, &arr[i].y, &group[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     scanf("%lld %lld %lld %lld %d", &ps.x, &ps.y, &pe.x, &pe.y, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         scanf("%d %d", &query[i].x, &query[i].y);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2004 KB Output is correct
2 Correct 12 ms 1948 KB Output is correct
3 Correct 74 ms 2240 KB Output is correct
4 Correct 178 ms 10640 KB Output is correct
5 Correct 108 ms 10816 KB Output is correct
6 Correct 3 ms 2004 KB Output is correct
7 Correct 3 ms 1996 KB Output is correct
8 Correct 3 ms 2000 KB Output is correct
9 Correct 2 ms 2004 KB Output is correct
10 Correct 2 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4604 KB Output is correct
2 Correct 151 ms 3624 KB Output is correct
3 Correct 31 ms 3208 KB Output is correct
4 Correct 11 ms 3208 KB Output is correct
5 Correct 11 ms 3796 KB Output is correct
6 Correct 27 ms 4828 KB Output is correct
7 Correct 26 ms 4924 KB Output is correct
8 Correct 25 ms 4104 KB Output is correct
9 Correct 17 ms 4020 KB Output is correct
10 Correct 14 ms 3876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2004 KB Output is correct
2 Correct 12 ms 1948 KB Output is correct
3 Correct 74 ms 2240 KB Output is correct
4 Correct 178 ms 10640 KB Output is correct
5 Correct 108 ms 10816 KB Output is correct
6 Correct 3 ms 2004 KB Output is correct
7 Correct 3 ms 1996 KB Output is correct
8 Correct 3 ms 2000 KB Output is correct
9 Correct 2 ms 2004 KB Output is correct
10 Correct 2 ms 1876 KB Output is correct
11 Correct 27 ms 4604 KB Output is correct
12 Correct 151 ms 3624 KB Output is correct
13 Correct 31 ms 3208 KB Output is correct
14 Correct 11 ms 3208 KB Output is correct
15 Correct 11 ms 3796 KB Output is correct
16 Correct 27 ms 4828 KB Output is correct
17 Correct 26 ms 4924 KB Output is correct
18 Correct 25 ms 4104 KB Output is correct
19 Correct 17 ms 4020 KB Output is correct
20 Correct 14 ms 3876 KB Output is correct
21 Correct 28 ms 4612 KB Output is correct
22 Correct 147 ms 3644 KB Output is correct
23 Correct 1032 ms 3708 KB Output is correct
24 Correct 1456 ms 12448 KB Output is correct
25 Correct 199 ms 12528 KB Output is correct
26 Correct 121 ms 12892 KB Output is correct
27 Correct 20 ms 5332 KB Output is correct
28 Correct 20 ms 5344 KB Output is correct
29 Execution timed out 4059 ms 6756 KB Time limit exceeded
30 Halted 0 ms 0 KB -