#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3192 KB |
Output is correct |
2 |
Correct |
11 ms |
3192 KB |
Output is correct |
3 |
Correct |
55 ms |
2980 KB |
Output is correct |
4 |
Correct |
133 ms |
4972 KB |
Output is correct |
5 |
Correct |
73 ms |
4976 KB |
Output is correct |
6 |
Correct |
8 ms |
2936 KB |
Output is correct |
7 |
Correct |
10 ms |
2936 KB |
Output is correct |
8 |
Correct |
10 ms |
3192 KB |
Output is correct |
9 |
Correct |
8 ms |
3192 KB |
Output is correct |
10 |
Correct |
8 ms |
3192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
5808 KB |
Output is correct |
2 |
Correct |
87 ms |
5880 KB |
Output is correct |
3 |
Correct |
61 ms |
5240 KB |
Output is correct |
4 |
Correct |
53 ms |
5112 KB |
Output is correct |
5 |
Correct |
56 ms |
5496 KB |
Output is correct |
6 |
Correct |
48 ms |
5920 KB |
Output is correct |
7 |
Correct |
48 ms |
5928 KB |
Output is correct |
8 |
Correct |
57 ms |
5796 KB |
Output is correct |
9 |
Correct |
42 ms |
5796 KB |
Output is correct |
10 |
Correct |
44 ms |
5920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3192 KB |
Output is correct |
2 |
Correct |
11 ms |
3192 KB |
Output is correct |
3 |
Correct |
55 ms |
2980 KB |
Output is correct |
4 |
Correct |
133 ms |
4972 KB |
Output is correct |
5 |
Correct |
73 ms |
4976 KB |
Output is correct |
6 |
Correct |
8 ms |
2936 KB |
Output is correct |
7 |
Correct |
10 ms |
2936 KB |
Output is correct |
8 |
Correct |
10 ms |
3192 KB |
Output is correct |
9 |
Correct |
8 ms |
3192 KB |
Output is correct |
10 |
Correct |
8 ms |
3192 KB |
Output is correct |
11 |
Correct |
59 ms |
5808 KB |
Output is correct |
12 |
Correct |
87 ms |
5880 KB |
Output is correct |
13 |
Correct |
61 ms |
5240 KB |
Output is correct |
14 |
Correct |
53 ms |
5112 KB |
Output is correct |
15 |
Correct |
56 ms |
5496 KB |
Output is correct |
16 |
Correct |
48 ms |
5920 KB |
Output is correct |
17 |
Correct |
48 ms |
5928 KB |
Output is correct |
18 |
Correct |
57 ms |
5796 KB |
Output is correct |
19 |
Correct |
42 ms |
5796 KB |
Output is correct |
20 |
Correct |
44 ms |
5920 KB |
Output is correct |
21 |
Correct |
60 ms |
5796 KB |
Output is correct |
22 |
Correct |
88 ms |
5844 KB |
Output is correct |
23 |
Correct |
294 ms |
5992 KB |
Output is correct |
24 |
Correct |
1160 ms |
7280 KB |
Output is correct |
25 |
Correct |
206 ms |
7332 KB |
Output is correct |
26 |
Correct |
134 ms |
7408 KB |
Output is correct |
27 |
Correct |
55 ms |
6140 KB |
Output is correct |
28 |
Correct |
53 ms |
6136 KB |
Output is correct |
29 |
Correct |
120 ms |
8400 KB |
Output is correct |
30 |
Correct |
117 ms |
9096 KB |
Output is correct |
31 |
Correct |
120 ms |
9104 KB |
Output is correct |
32 |
Correct |
583 ms |
8780 KB |
Output is correct |
33 |
Correct |
1421 ms |
8988 KB |
Output is correct |
34 |
Correct |
131 ms |
8944 KB |
Output is correct |
35 |
Correct |
122 ms |
8912 KB |
Output is correct |
36 |
Correct |
135 ms |
8908 KB |
Output is correct |
37 |
Correct |
148 ms |
8996 KB |
Output is correct |
38 |
Correct |
2254 ms |
9008 KB |
Output is correct |
39 |
Correct |
1697 ms |
8916 KB |
Output is correct |
40 |
Correct |
1387 ms |
8816 KB |
Output is correct |
41 |
Correct |
119 ms |
9288 KB |
Output is correct |
42 |
Correct |
125 ms |
9172 KB |
Output is correct |
43 |
Correct |
138 ms |
9156 KB |
Output is correct |
44 |
Correct |
67 ms |
6944 KB |
Output is correct |
45 |
Correct |
65 ms |
6924 KB |
Output is correct |
46 |
Correct |
67 ms |
6620 KB |
Output is correct |
47 |
Correct |
68 ms |
7072 KB |
Output is correct |
48 |
Correct |
69 ms |
7028 KB |
Output is correct |
49 |
Correct |
67 ms |
6580 KB |
Output is correct |