This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |