Submission #594988

# Submission time Handle Problem Language Result Execution time Memory
594988 2022-07-13T08:39:14 Z 박상훈(#8436) Dragon 2 (JOI17_dragon2) C++17
100 / 100
1289 ms 193428 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
struct Query{
    int c, d, i;
    bool inv;
    bool operator<(const Query &Q) const{
        if (d==Q.d) return c<Q.c;
        return d<Q.d;
    }
}Q[100100];
ll ans[100100], it = 0;

struct Point;
ll xo, yo;
int ccw(Point a, Point b, Point c);

struct Point{
    ll x, y, i;
    Point(){}
    Point(ll _x, ll _y, ll _i): x(_x), y(_y), i(_i) {}

    bool operator<(const Point &P) const{
        bool left1 = (x-xo<0 || (x-xo==0 && y-yo>0));
        bool left2 = (P.x-xo<0 || (P.x-xo==0 && P.y-yo>0));
        if (left1!=left2) return left1 > left2;

        return ccw(Point(xo, yo, 0), *this, P) > 0;
    }
}a[60060], b[60060], O1, O2;
int n, ccol, c[30030], nx[30030], ny[30030], opa[30030], opb[30030];
vector<int> T[30030];

int ccw(Point a, Point b, Point c){
    ll ret = (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
    if (ret>0) return 1;
    if (ret<0) return -1;
    return 0;
}

struct Seg{
    vector<int> a, tree;
    int sz;
    void init(){
        sort(a.begin(), a.end());
        sz = a.size();
        tree.resize(sz*2, 0);
    }
    int getidx(int y){
        return lower_bound(a.begin(), a.end(), y) - a.begin();
    }
    void update(int y, int val){
        y = getidx(y) + sz;
        for (tree[y]+=val;y>1;y>>=1) tree[y>>1] = tree[y] + tree[y^1];
    }
    int query(int l, int r){
        int ret = 0;
        l = getidx(l), r = getidx(r+1);
        for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
            if (l&1) ret += tree[l++];
            if (r&1) ret += tree[--r];
        }
        return ret;
    }
}F[30030];

struct Event{
    int x, y1, y2, typ, i;
    Event(){}
    Event(int _x, int _y1, int _y2, int _typ, int _i): x(_x), y1(_y1), y2(_y2), typ(_typ), i(_i) {}
    bool operator<(const Event &E) const{
        if (x==E.x){
            return typ < E.typ;
        }
        return x < E.x;
    }
};
vector<Event> E[30030];

void add_point(int x, int y, int c){
    F[c].a.push_back(y);
    E[c].emplace_back(x, y, y, 0, 0);
}

void add_query(int x1, int x2, int y1, int y2, int c, int idx){
    if (x1>x2 || y1>y2) return;
    //printf("%d %d %d %d %d %d\n", x1, x2, y1, y2, c, idx);
    E[c].emplace_back(x1-1, y1, y2, 2, idx);
    E[c].emplace_back(x2, y1, y2, 1, idx);
}

void process(int c){
    F[c].init();
    sort(E[c].begin(), E[c].end());
    //printf("col %d\n", c);
    for (auto &t:E[c]){
        //printf("%d %d %d %d %d\n", t.x, t.y1, t.y2, t.typ, t.i);
        if (t.typ==0) F[c].update(t.y1, 1);
        else if (t.typ==1) ans[t.i] += F[c].query(t.y1, t.y2);
        else if (t.typ==2) ans[t.i] -= F[c].query(t.y1, t.y2);
    }
}

pair<int, int> get_range(Point a[], int op[], int sx, bool v, Point Z2, Point Z3){
    if (op[sx]==sx+n){
        if ((ccw(Z2, a[sx], Z3)==1) != v) return {sx+1, sx+n-1};
        return {sx+1, sx};
    }
    if ((ccw(Z2, a[sx], Z3)==ccw(Z2, a[sx], a[op[sx]])) == v) return {sx+1, op[sx]-1};
    return {op[sx], sx+n-1};
}

void calc1(int c1, int c2, int qnum){
    for (auto &i:T[c1]){
        it++;
        int sx = nx[i], sy = ny[i];

        auto Rx = get_range(a, opa, sx, 0, O1, O2);
        auto Ry = get_range(b, opb, sy, 0, O2, O1);


        add_query(Rx.first, Rx.second, Ry.first, Ry.second, c2, qnum);


    }
}

void calc2(int c, int c2, int qnum){
    for (auto &i:T[c]){
        it++;
        ///Case #1
        int sx = nx[i], sy = ny[i];
        auto Rx = get_range(a, opa, sx, 1, O1, O2);
        auto Ry = get_range(b, opb, sy, 1, O2, O1);

        add_query(Rx.first, Rx.second, Ry.first, Ry.second, c2, qnum);


        ///Case #2
        pair<int, int> S1, S2;
        S1 = get_range(a, opa, sx, 0, O1, O2);
        S2 = get_range(b, opb, sy, 0, O2, O1);
        //printf(" org: %d %d %d %d\n", sx+1, sx+n-1, sy+1, sy+n-1);
        //printf(" R: %d %d %d %d\n", Rx.first, Rx.second, Ry.first, Ry.second);
        //printf(" S: %d %d %d %d\n", S1.first, S1.second, S2.first, S2.second);
        if (ccw(O1, O2, a[sx])==1){ /// 0 -> 1
            int tmp = nx[n];
            if (!(sx<tmp && tmp<sx+n)) tmp += n;
            S1.second = tmp-1;
        }
        else{ /// 1 -> 0
            int tmp = nx[n];
            if (!(sx<tmp && tmp<sx+n)) tmp += n;
            S1.first = tmp+1;
        }

        if (ccw(O2, O1, a[sx])==1){ ///0 -> 1
            int tmp = ny[n];
            if (!(sy<tmp && tmp<sy+n)) tmp += n;
            S2.second = tmp-1;
        }
        else{ /// 1 -> 0
            int tmp = ny[n];
            if (!(sy<tmp && tmp<sy+n)) tmp += n;
            S2.first = tmp+1;
        }

        add_query(S1.first, S1.second, S2.first, S2.second, c2, qnum);

        //printf(" %lld %lld -> %d + %d\n", a[sx].x, a[sx].y, tree.query(Rx.first, Rx.second, Ry.first, Ry.second), tree.query(S1.first, S1.second, S2.first, S2.second));
        //printf(" S: %d %d %d %d\n\n", S1.first, S1.second, S2.first, S2.second);
    }
}

void init(Point O, Point a[], int nx[], int op[]){
    xo = O.x, yo = O.y;
    sort(a+1, a+n+1);
    for (int i=n+1;i<=n*2;i++) a[i] = a[i-n];

    for (int i=1;i<=n;i++) nx[a[i].i] = i;

    int pt = 2;
    for (int i=1;i<=n;i++){
        while(pt<i+n && ccw(O, a[i], a[pt]) >= 0) pt++;
        op[i] = pt;
    }
}

int main(){
    //freopen("Big.txt", "r", stdin);
    int m;
    scanf("%d %d", &n, &m);
    for (int i=1;i<=n;i++){
        scanf("%lld %lld %d", &a[i].x, &a[i].y, c+i);
        a[i].i = i;
        b[i] = a[i];
        T[c[i]].push_back(i);
    }

    scanf("%lld %lld %lld %lld", &O1.x, &O1.y, &O2.x, &O2.y);
    a[n+1] = O2, b[n+1] = O1;
    a[n+1].i = n+1, b[n+1].i = n+1;
    ++n;
    init(O1, a, nx, opa);
    init(O2, b, ny, opb);

    for (int i=1;i<n;i++){
        add_point(nx[i], ny[i], c[i]);
        add_point(nx[i]+n, ny[i], c[i]);
        add_point(nx[i], ny[i]+n, c[i]);
        add_point(nx[i]+n, ny[i]+n, c[i]);
    }

    //tree.init(n*2+1);

    int q;
    scanf("%d", &q);
    for (int i=1;i<=q;i++){
        scanf("%d %d", &Q[i].c, &Q[i].d);
        Q[i].i = i;

        if (T[Q[i].c].size() > T[Q[i].d].size()){
            Q[i].inv = 1;
            swap(Q[i].c, Q[i].d);
        }
        else if (T[Q[i].c].size()==T[Q[i].d].size() && Q[i].c > Q[i].d){
            Q[i].inv = 1;
            swap(Q[i].c, Q[i].d);
        }
        else Q[i].inv = 0;
    }

    sort(Q+1, Q+q+1);
    ccol = 0;
    for (int i=1;i<=q;i++){
        //if (i%10000==0) printf("ok %d queries\n", i);
        //printf("YES %d\n", i);
        /*if (ccol!=Q[i].d){
            for (auto &j:T[ccol]) update(nx[j], ny[j], -1);
            for (auto &j:T[Q[i].d]) update(nx[j], ny[j], 1);
            ccol = Q[i].d;
        }*/

        //printf("update ok\n");
        /*if (i>1 && Q[i].c==Q[i-1].c && Q[i].d==Q[i-1].d && Q[i].inv==Q[i-1].inv){
            ans[Q[i].i] = ans[Q[i-1].i];
            continue;
        }*/

        if (!Q[i].inv) calc1(Q[i].c, Q[i].d, Q[i].i);
        else calc2(Q[i].c, Q[i].d, Q[i].i);
    }

    for (int i=1;i<=m;i++) process(i);

    //printf("Total iterations: %lld\n", it);
    for (int i=1;i<=q;i++) printf("%lld\n", ans[i]);
    return 0;
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:193:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  193 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
dragon2.cpp:195:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |         scanf("%lld %lld %d", &a[i].x, &a[i].y, c+i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:201:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  201 |     scanf("%lld %lld %lld %lld", &O1.x, &O1.y, &O2.x, &O2.y);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:218:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  218 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
dragon2.cpp:220:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  220 |         scanf("%d %d", &Q[i].c, &Q[i].d);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4468 KB Output is correct
2 Correct 15 ms 5520 KB Output is correct
3 Correct 62 ms 13516 KB Output is correct
4 Correct 109 ms 22320 KB Output is correct
5 Correct 62 ms 11432 KB Output is correct
6 Correct 7 ms 4536 KB Output is correct
7 Correct 6 ms 4564 KB Output is correct
8 Correct 7 ms 4448 KB Output is correct
9 Correct 6 ms 4400 KB Output is correct
10 Correct 6 ms 4532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 12704 KB Output is correct
2 Correct 161 ms 20776 KB Output is correct
3 Correct 71 ms 14924 KB Output is correct
4 Correct 51 ms 12704 KB Output is correct
5 Correct 38 ms 12524 KB Output is correct
6 Correct 61 ms 12084 KB Output is correct
7 Correct 56 ms 12016 KB Output is correct
8 Correct 66 ms 11764 KB Output is correct
9 Correct 39 ms 12640 KB Output is correct
10 Correct 44 ms 12148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4468 KB Output is correct
2 Correct 15 ms 5520 KB Output is correct
3 Correct 62 ms 13516 KB Output is correct
4 Correct 109 ms 22320 KB Output is correct
5 Correct 62 ms 11432 KB Output is correct
6 Correct 7 ms 4536 KB Output is correct
7 Correct 6 ms 4564 KB Output is correct
8 Correct 7 ms 4448 KB Output is correct
9 Correct 6 ms 4400 KB Output is correct
10 Correct 6 ms 4532 KB Output is correct
11 Correct 66 ms 12704 KB Output is correct
12 Correct 161 ms 20776 KB Output is correct
13 Correct 71 ms 14924 KB Output is correct
14 Correct 51 ms 12704 KB Output is correct
15 Correct 38 ms 12524 KB Output is correct
16 Correct 61 ms 12084 KB Output is correct
17 Correct 56 ms 12016 KB Output is correct
18 Correct 66 ms 11764 KB Output is correct
19 Correct 39 ms 12640 KB Output is correct
20 Correct 44 ms 12148 KB Output is correct
21 Correct 67 ms 12396 KB Output is correct
22 Correct 158 ms 20308 KB Output is correct
23 Correct 978 ms 107736 KB Output is correct
24 Correct 1289 ms 193428 KB Output is correct
25 Correct 167 ms 34792 KB Output is correct
26 Correct 111 ms 20948 KB Output is correct
27 Correct 40 ms 15308 KB Output is correct
28 Correct 41 ms 15412 KB Output is correct
29 Correct 135 ms 17980 KB Output is correct
30 Correct 95 ms 17084 KB Output is correct
31 Correct 95 ms 17436 KB Output is correct
32 Correct 115 ms 20280 KB Output is correct
33 Correct 720 ms 110636 KB Output is correct
34 Correct 91 ms 18368 KB Output is correct
35 Correct 94 ms 18780 KB Output is correct
36 Correct 128 ms 20588 KB Output is correct
37 Correct 101 ms 20992 KB Output is correct
38 Correct 744 ms 105360 KB Output is correct
39 Correct 909 ms 115780 KB Output is correct
40 Correct 783 ms 108024 KB Output is correct
41 Correct 139 ms 19060 KB Output is correct
42 Correct 162 ms 22120 KB Output is correct
43 Correct 199 ms 26748 KB Output is correct
44 Correct 66 ms 13756 KB Output is correct
45 Correct 69 ms 13736 KB Output is correct
46 Correct 66 ms 13828 KB Output is correct
47 Correct 65 ms 13248 KB Output is correct
48 Correct 64 ms 13236 KB Output is correct
49 Correct 57 ms 13244 KB Output is correct