Submission #410504

# Submission time Handle Problem Language Result Execution time Memory
410504 2021-05-22T19:58:32 Z duality Dragon 2 (JOI17_dragon2) C++11
100 / 100
2710 ms 32640 KB
#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#define x first
#define y second
const double PI = acos(-1.0);

pii BB;
struct angle {
    pii p;
    char c;
    angle(double a) {
        assert(a == PI);
        p = BB,c = 1;
    }
    angle(pii a,pii b) {
        if ((LLI) a.x*b.y < (LLI) a.y*b.x) {
            if (b != BB) a.x *= -1,a.y *= -1;
            c = 0,p = a;
        }
        else {
            if (b == BB) a.x *= -1,a.y *= -1;
            c = 1,p = a;
        }
    }
    bool operator<(const angle &o) const {
        if (c == o.c) return (LLI) p.x*o.p.y > (LLI) p.y*o.p.x;
        else return c < o.c;
    }
    angle operator+(double d) {
        angle n = *this;
        d /= PI;
        n.c += round(d);
        return n;
    }
    angle operator-(double d) {
        angle n = *this;
        d /= PI;
        n.c -= round(d);
        return n;
    }
};
vpii pos[30000];
vector<pair<angle,angle> > pos2[30000];
struct event { angle x,l,r; int i; };
bool comp(event a,event b) {
    return a.x < b.x;
}
vpii vv[30000];
int bit[90001];
int ans[100000];
vector<event> events[3];
vector<angle> poss;
int main() {
    int i;
    int N,M,Q;
    int A,B,C,F,G;
    pii a,b;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N; i++) scanf("%d %d %d",&A,&B,&C),pos[C-1].pb(mp(A,B));
    scanf("%d %d %d %d",&a.x,&a.y,&b.x,&b.y);

    int j;
    BB = mp(b.x-a.x,b.y-a.y);
    for (i = 0; i < N; i++) {
        for (j = 0; j < pos[i].size(); j++) {
            angle a1 = angle(mp(pos[i][j].x-a.x,pos[i][j].y-a.y),mp(b.x-a.x,b.y-a.y));
            angle a2 = angle(mp(pos[i][j].x-b.x,pos[i][j].y-b.y),mp(a.x-b.x,a.y-b.y));
            /*double a1 = atan2(pos[i][j].y-a.y,pos[i][j].x-a.x)-atan2(b.y-a.y,b.x-a.x);
            if (a1 < 0) a1 += 2*PI;
            double a2 = atan2(pos[i][j].y-b.y,pos[i][j].x-b.x)-atan2(a.y-b.y,a.x-b.x);
            if (a2 < 0) a2 += 2*PI;
            pos2[i].pb(mp(angle(a1),angle(a2)));*/
            pos2[i].pb(mp(a1,a2));
        }
    }

    scanf("%d",&Q);
    for (i = 0; i < Q; i++) {
        scanf("%d %d",&F,&G),F--,G--;
        if (pos[F].size() < pos[G].size()) vv[G].pb(mp(F,i+1));
        else vv[F].pb(mp(G,-(i+1)));
    }

    int t,k,l;
    for (i = 0; i < N; i++) {
        int sum = 0;
        for (j = 0; j < vv[i].size(); j++) {
            if (vv[i][j].second > 0) {
                int t = i,t2 = j;
                F = vv[i][j].first,G = i,i = vv[i][j].second-1;
                sum += pos2[F].size();
                j = t2,i = t;
            }
            else {
                int t = i,t2 = j;
                G = vv[i][j].first,F = i,i = -vv[i][j].second-1;
                sum += pos2[G].size();
                j = t2,i = t;
            }
        }
        assert(sum <= 2*N);
        for (j = 0; j < vv[i].size(); j++) {
            if (vv[i][j].second > 0) {
                int t = i,t2 = j;
                F = vv[i][j].first,G = i,i = vv[i][j].second-1;
                for (j = 0; j < pos2[F].size(); j++) {
                    if (pos2[F][j].x < angle(PI)) {
                        events[0].pb((event){pos2[F][j].x-PI,pos2[F][j].y,pos2[F][j].y+PI,-(i+1)});
                        events[0].pb((event){pos2[F][j].x,pos2[F][j].y,pos2[F][j].y+PI,i+1});
                    }
                    else {
                        events[0].pb((event){pos2[F][j].x,pos2[F][j].y-PI,pos2[F][j].y,-(i+1)});
                        events[0].pb((event){pos2[F][j].x+PI,pos2[F][j].y-PI,pos2[F][j].y,i+1});
                    }
                }
                j = t2,i = t;
            }
            else {
                int t = i,t2 = j;
                G = vv[i][j].first,F = i,i = -vv[i][j].second-1;
                for (j = 0; j < pos2[G].size(); j++) {
                    if (pos2[G][j].x < angle(PI)) {
                        events[0].pb((event){pos2[G][j].x,pos2[G][j].y-PI,pos2[G][j].y,-(i+1)});
                        events[0].pb((event){pos2[G][j].x+PI,pos2[G][j].y-PI,pos2[G][j].y,i+1});
                        events[2].pb((event){pos2[G][j].x-PI,pos2[G][j].y,pos2[G][j].y+PI,-(i+1)});
                        events[2].pb((event){pos2[G][j].x,pos2[G][j].y,pos2[G][j].y+PI,i+1});
                    }
                    else {
                        events[0].pb((event){pos2[G][j].x-PI,pos2[G][j].y,pos2[G][j].y+PI,-(i+1)});
                        events[0].pb((event){pos2[G][j].x,pos2[G][j].y,pos2[G][j].y+PI,i+1});
                        events[1].pb((event){pos2[G][j].x,pos2[G][j].y-PI,pos2[G][j].y,-(i+1)});
                        events[1].pb((event){pos2[G][j].x+PI,pos2[G][j].y-PI,pos2[G][j].y,i+1});
                    }
                }
                j = t2,i = t;
            }
        }
        for (t = 0; t < 3; t++) {
            vector<event> &ee = events[t];
            for (j = 0; j < pos2[i].size(); j++) {
                if ((t == 1) && (angle(PI) < pos2[i][j].x)) continue;
                if ((t == 2) && (pos2[i][j].x < angle(PI))) continue;
                for (k = -1; k <= 1; k++) {
                    poss.pb(pos2[i][j].y+2*PI*k);
                    for (l = -1; l <= 1; l++) ee.pb((event){pos2[i][j].x+2*PI*k,pos2[i][j].y+2*PI*l,pos2[i][j].y+2*PI*l,0});
                }
            }
            sort(poss.begin(),poss.end());
            sort(ee.begin(),ee.end(),comp);
            for (j = 0; j < ee.size(); j++) {
                if (ee[j].i == 0) {
                    int p = lower_bound(poss.begin(),poss.end(),ee[j].l)-poss.begin();
                    for (k = p+1; k <= poss.size(); k += k & (-k)) bit[k]++;
                }
                else if (ee[j].i > 0) {
                    int l = lower_bound(poss.begin(),poss.end(),ee[j].l)-poss.begin();
                    int r = upper_bound(poss.begin(),poss.end(),ee[j].r)-poss.begin()-1;
                    if (l <= r) {
                        for (k = r+1; k > 0; k -= k & (-k)) ans[ee[j].i-1] += bit[k];
                        for (k = l; k > 0; k -= k & (-k)) ans[ee[j].i-1] -= bit[k];
                    }
                }
                else {
                    int l = lower_bound(poss.begin(),poss.end(),ee[j].l)-poss.begin();
                    int r = upper_bound(poss.begin(),poss.end(),ee[j].r)-poss.begin()-1;
                    if (l <= r) {
                        for (k = r+1; k > 0; k -= k & (-k)) ans[-ee[j].i-1] -= bit[k];
                        for (k = l; k > 0; k -= k & (-k)) ans[-ee[j].i-1] += bit[k];
                    }
                }
            }
            fill(bit,bit+poss.size()+1,0);
            ee.clear(),poss.clear();
        }
    }
    for (i = 0; i < Q; i++) printf("%d\n",ans[i]);

    return 0;
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:119:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for (j = 0; j < pos[i].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~~
dragon2.cpp:141:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (j = 0; j < vv[i].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~
dragon2.cpp:156:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |         for (j = 0; j < vv[i].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~
dragon2.cpp:160:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<angle, angle> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |                 for (j = 0; j < pos2[F].size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
dragon2.cpp:175:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<angle, angle> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |                 for (j = 0; j < pos2[G].size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
dragon2.cpp:194:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<angle, angle> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  194 |             for (j = 0; j < pos2[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~
dragon2.cpp:204:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |             for (j = 0; j < ee.size(); j++) {
      |                         ~~^~~~~~~~~~~
dragon2.cpp:207:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<angle>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |                     for (k = p+1; k <= poss.size(); k += k & (-k)) bit[k]++;
      |                                   ~~^~~~~~~~~~~~~~
dragon2.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
dragon2.cpp:113:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |     for (i = 0; i < N; i++) scanf("%d %d %d",&A,&B,&C),pos[C-1].pb(mp(A,B));
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:114:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |     scanf("%d %d %d %d",&a.x,&a.y,&b.x,&b.y);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:131:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
dragon2.cpp:133:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         scanf("%d %d",&F,&G),F--,G--;
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4708 KB Output is correct
2 Correct 38 ms 3604 KB Output is correct
3 Correct 138 ms 3168 KB Output is correct
4 Correct 200 ms 4532 KB Output is correct
5 Correct 79 ms 4424 KB Output is correct
6 Correct 8 ms 2636 KB Output is correct
7 Correct 8 ms 2656 KB Output is correct
8 Correct 27 ms 5004 KB Output is correct
9 Correct 25 ms 4708 KB Output is correct
10 Correct 25 ms 5144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 22208 KB Output is correct
2 Correct 447 ms 12120 KB Output is correct
3 Correct 216 ms 4760 KB Output is correct
4 Correct 146 ms 3884 KB Output is correct
5 Correct 73 ms 3916 KB Output is correct
6 Correct 288 ms 27432 KB Output is correct
7 Correct 279 ms 27408 KB Output is correct
8 Correct 306 ms 31820 KB Output is correct
9 Correct 245 ms 22440 KB Output is correct
10 Correct 267 ms 32640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4708 KB Output is correct
2 Correct 38 ms 3604 KB Output is correct
3 Correct 138 ms 3168 KB Output is correct
4 Correct 200 ms 4532 KB Output is correct
5 Correct 79 ms 4424 KB Output is correct
6 Correct 8 ms 2636 KB Output is correct
7 Correct 8 ms 2656 KB Output is correct
8 Correct 27 ms 5004 KB Output is correct
9 Correct 25 ms 4708 KB Output is correct
10 Correct 25 ms 5144 KB Output is correct
11 Correct 313 ms 22208 KB Output is correct
12 Correct 447 ms 12120 KB Output is correct
13 Correct 216 ms 4760 KB Output is correct
14 Correct 146 ms 3884 KB Output is correct
15 Correct 73 ms 3916 KB Output is correct
16 Correct 288 ms 27432 KB Output is correct
17 Correct 279 ms 27408 KB Output is correct
18 Correct 306 ms 31820 KB Output is correct
19 Correct 245 ms 22440 KB Output is correct
20 Correct 267 ms 32640 KB Output is correct
21 Correct 308 ms 22384 KB Output is correct
22 Correct 438 ms 11860 KB Output is correct
23 Correct 1847 ms 11860 KB Output is correct
24 Correct 2710 ms 8756 KB Output is correct
25 Correct 299 ms 7556 KB Output is correct
26 Correct 156 ms 7940 KB Output is correct
27 Correct 63 ms 5700 KB Output is correct
28 Correct 67 ms 5740 KB Output is correct
29 Correct 284 ms 25496 KB Output is correct
30 Correct 232 ms 24156 KB Output is correct
31 Correct 232 ms 21584 KB Output is correct
32 Correct 226 ms 14776 KB Output is correct
33 Correct 1594 ms 9776 KB Output is correct
34 Correct 173 ms 8100 KB Output is correct
35 Correct 138 ms 7852 KB Output is correct
36 Correct 157 ms 7768 KB Output is correct
37 Correct 157 ms 7988 KB Output is correct
38 Correct 1630 ms 12284 KB Output is correct
39 Correct 1800 ms 11476 KB Output is correct
40 Correct 1622 ms 11212 KB Output is correct
41 Correct 278 ms 19888 KB Output is correct
42 Correct 326 ms 16300 KB Output is correct
43 Correct 407 ms 14956 KB Output is correct
44 Correct 217 ms 23900 KB Output is correct
45 Correct 205 ms 14508 KB Output is correct
46 Correct 193 ms 12000 KB Output is correct
47 Correct 199 ms 21776 KB Output is correct
48 Correct 185 ms 12676 KB Output is correct
49 Correct 182 ms 10272 KB Output is correct