This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define rb(x) ((x)&(-(x)))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
pll operator + (const pll &a, const pll &b) { return pll(a.first+b.first, a.second+b.second); }
pll operator - (const pll &a, const pll &b) { return pll(a.first-b.first, a.second-b.second); }
ll operator * (const pll &a, const pll &b) { return a.first*b.second - b.first*a.second; }
ll ccw(const pll &a, const pll &b, const pll &c) { return a*b + b*c + c*a; }
const int MAXN = 30005;
const int SQRN = 180;
const int MAXK = 30005;
const int MAXQ = 100005;
const int MX = 60055;
struct EVT {
EVT(int p, int x, int i) : p(p), x(x), i(i) {}
int p, x, i;
bool operator < (const EVT &t) const {
if(x != t.x) return x < t.x;
return p < t.p;
}
};
struct BIT {
int d[MX];
void init() { memset(d, 0, MX<<2); }
void init(int x) {
for(x += 5; x < MX; x += rb(x))
d[x] = 0;
}
void upd(int x, int r) {
for(x += 5; x < MX; x += rb(x))
d[x] += r;
}
int get(int x) {
int r = 0; for(x += 5; x; x -= rb(x))
r += d[x];
return r;
}
int get(int s, int e) { return s > e ? 0 : get(e)-get(s-1); }
} bit;
pll P[MAXN*2];
int A[MAXN], O[MAXN*2];
vector<int> OutQV[MAXK], InQV[MAXK], JustQV;
vector<int> AV[MAXK];
int L[MAXN], LS[MAXN], LE[MAXN];
int R[MAXN], RS[MAXN], RE[MAXN];
int B[MAXQ], C[MAXQ];
int QI[MAXN];
int Ans[MAXQ];
int N, K, Q;
void calFatA(int a, vector<int> &QV) {
vector<EVT> EV;
for(int v : AV[a]) {
EV.eb(0, LS[v], v);
EV.eb(1, LE[v], v);
}
for(int qi : QV) {
QI[C[qi]] = qi;
for(int v : AV[C[qi]])
EV.eb(2, L[v], v);
}
sorv(EV);
for(auto &ev : EV) {
int t = ev.p, i = ev.i;
if(!t) {
bit.upd(RS[i], 1);
bit.upd(RE[i], -1);
} else if(1 == t) {
bit.upd(RS[i], -1);
bit.upd(RE[i], 1);
} else Ans[QI[A[i]]] += bit.get(1, R[i]);
}
bit.init();
}
void calFatB(int b, vector<int> &QV) {
vector<EVT> EV;
for(int v : AV[b]) EV.eb(0, L[v], v);
for(int qi : QV) {
QI[B[qi]] = qi;
for(int v : AV[B[qi]]) {
EV.eb(1, LS[v]-1, v);
EV.eb(2, LE[v], v);
}
}
sorv(EV);
for(auto &ev : EV) {
int t = ev.p, i = ev.i;
if(!t) bit.upd(R[i], 1);
else if(1 == t) Ans[QI[A[i]]] -= bit.get(RS[i], RE[i]);
else Ans[QI[A[i]]] += bit.get(RS[i], RE[i]);
}
bit.init();
}
void cal(int a, int b, int &ret) {
vector<EVT> EV;
for(int v : AV[b]) EV.eb(0, L[v], v);
for(int v : AV[a]) {
EV.eb(1, LS[v]-1, v);
EV.eb(2, LE[v], v);
}
sorv(EV);
for(auto &ev : EV) {
int t = ev.p, i = ev.i;
if(!t) bit.upd(R[i], 1);
else if(1 == t) ret -= bit.get(RS[i], RE[i]);
else ret += bit.get(RS[i], RE[i]);
}
for(int v : AV[b]) bit.init(R[v]);
}
void precal() {
iota(O, O+(N<<1), 4);
for(int i = 0; i <= N+1; i++)
P[i<<1|1] = P[0] + P[0] - P[i<<1];
sort(O, O+(N<<1), [&](int a, int b) {
bool af = ccw(P[3], P[0], P[a]) < 0;
bool bf = ccw(P[3], P[0], P[b]) < 0;
if(af != bf) return af;
return ccw(P[a], P[0], P[b]) < 0;
});
for(int i = 0, v; i < N; i++) {
v = O[i];
L[(v>>1)-1] = i + ((v&1) ? N : 0);
v >>= 1; v--;
LS[v] = i; LE[v] = i+N;
}
for(int i = 0; i <= N+1; i++)
P[i<<1|1] = P[2] + P[2] - P[i<<1];
sort(O, O+(N<<1), [&](int a, int b) {
bool af = ccw(P[1], P[2], P[a]) < 0;
bool bf = ccw(P[1], P[2], P[b]) < 0;
if(af != bf) return af;
return ccw(P[a], P[2], P[b]) < 0;
});
for(int i = 0, v; i < N; i++) {
v = O[i];
R[(v>>1)-1] = i + ((v&1) ? N : 0);
v >>= 1; v--;
RS[v] = i; RE[v] = i+N;
}
for(int i = 1; i <= N; i++) {
L[i]++; LS[i]++; LE[i]++;
R[i]++; RS[i]++; RE[i]++;
}
}
void splitFat() {
for(int i = 1, a, b; i <= Q; i++) {
a = B[i]; b = C[i];
if(SQRN <= sz(AV[a])) OutQV[a].eb(i);
else if(SQRN <= sz(AV[b])) InQV[b].eb(i);
else JustQV.eb(i);
}
}
void cal() {
for(int i = 1; i <= K; i++) {
auto &V = OutQV[i];
if(V.empty()) continue;
calFatA(i, V);
}
for(int i = 1; i <= K; i++) {
auto &V = InQV[i];
if(V.empty()) continue;
calFatB(i, V);
}
for(int qi : JustQV)
cal(B[qi], C[qi], Ans[qi]);
}
int main() {
ios::sync_with_stdio(false);
cin >> N >> K;
for(int i = 1; i <= N; i++) {
cin >> P[(i+1)<<1].first >> P[(i+1)<<1].second >> A[i];
AV[A[i]].eb(i);
}
cin >> P[0].first >> P[0].second >> P[2].first >> P[2].second;
cin >> Q;
for(int i = 1; i <= Q; i++) cin >> B[i] >> C[i];
precal();
splitFat();
cal();
for(int i = 1; i <= Q; i++) printf("%d\n", Ans[i]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |