#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=4001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
inline ll readll() {
ll x = 0;
int neg=1;
char ch = getchar_unlocked();
while (ch < '0' || ch > '9'){
if(ch=='-')neg=-1;
ch = getchar_unlocked();
}
while (ch >= '0' && ch <= '9'){
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar_unlocked();
}
return x*neg;
}
struct Point{
ll x,y;
Point(){}
Point(ll a,ll b):x(a),y(b){}
bool ccw(Point a,Point b){
ll dx1=x-a.x,dy1=y-a.y;
ll dx2=b.x-a.x,dy2=b.y-a.y;
return dx1*dy2-dx2*dy1>0;
}
};
class BIT{
vi bit;ll n;
ll sum(ll i){
ll res=0;
for(;i;i-=i&-i)res+=bit[i];
return res;
}
public:
BIT(ll n_):n(n_){bit=vi(n+1);}
void add(ll i,ll x){
for(i++;i<=n;i+=i&-i)bit[i]+=x;
}
ll get(ll a,ll b){
if(b<=a)return 0ll;
return sum(b)-sum(a);
}
ll solve(ll x){ //sum([0,r])>=x となる最小のr
if(x<=0)return -1;
ll l=0,len=1;while(len<n)len<<=1;
for(;len;len>>=1)if(l+len<n&&bit[l+len]<x){
x-=bit[l+len];l+=len;
}
return l;
}
};
const ll B=300;
int main(){
cout<<fixed<<setprecision(20);
ll n,m;cin>>m>>n;
vector<vector<Point>> v(n);
rep(i,m){
ll a,b,c;cin>>a>>b>>c;c--;
v[c].pb(Point(a,b));
}
Point a,b;
cin>>a.x>>a.y>>b.x>>b.y;
auto cmpy=[&](Point s,Point t){
Point x(b.x-a.x,b.y-a.y),y(t.x-s.x,t.y-s.y);
if(x.x*y.y==x.y*y.x){
if(a.y==t.y)return a.x<t.x;
return a.y<t.y;
}
return x.x*y.y>x.y*y.x;
};
ll q;cin>>q;
rep(i,n)sort(all(v[i]),cmpy);
vvp L(n),S(n);
rep(i,q){
ll a,b;cin>>a>>b;a--;b--;
if(v[a].size()==0||v[b].size()==0)continue;
if(v[a].size()<=B||v[b].size()>B)S[a].pb(b,i);
else L[b].pb(a,i);
}
vi ans(q);
ll cnt_id=0;
vvi id(n);
rep(i,n)rep(j,v[i].size())id[i].pb(cnt_id++);
vector<pair<Point,P>> srt;
rep(i,n)rep(j,v[i].size())srt.pb(v[i][j],P(i,j));
// rep(i,n)rep(j,v[i].size())out(v[i][j].Angle(a,b));
{
auto comp=[&](pair<Point,P> s,pair<Point,P> t){
Point x=s.fi,y=t.fi;
if(x.ccw(a,b)){
x.x=a.x*2-x.x;
x.y=a.y*2-x.y;
}
if(y.ccw(a,b)){
y.x=a.x*2-y.x;
y.y=a.y*2-y.y;
}
return !y.ccw(a,x);
};
sort(all(srt),comp);
BIT bit(m);
rep(i,n)rep(j,v[i].size())if(!v[i][j].ccw(a,b))bit.add(id[i][j],1);
for(auto x:srt){
auto p=x.fi;
// cout<<p.x<<' '<<p.y<<endl;
ll i=x.se.fi,j=x.se.se;
if(!p.ccw(a,b))bit.add(id[i][j],-1);
for(auto query:S[i]){
ll k=query.fi;
if(p.ccw(a,b)){
ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]-=bit.get(l,id[k].back()+1);
}
else{
ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]-=bit.get(id[k][0],r);
}
}
for(auto query:L[i]){
ll k=query.fi;
if(p.ccw(a,b)){
ll l=lower_bound(all(v[k]),a,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]-=bit.get(l,id[k].back()+1);
ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]+=bit.get(id[k][0],r);
}
else{
ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]+=bit.get(l,id[k].back()+1);
ll r=lower_bound(all(v[k]),a,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]-=bit.get(id[k][0],r);
}
}
if(p.ccw(a,b))bit.add(id[i][j],1);
}
}
{
auto comp=[&](pair<Point,P> s,pair<Point,P> t){
Point x=s.fi,y=t.fi;
if(x.ccw(a,b)){
x.x=b.x*2-x.x;
x.y=b.y*2-x.y;
}
if(y.ccw(a,b)){
y.x=b.x*2-y.x;
y.y=b.y*2-y.y;
}
return !y.ccw(b,x);
};
sort(all(srt),comp);
BIT bit(m);
rep(i,n)rep(j,v[i].size())if(!v[i][j].ccw(a,b))bit.add(id[i][j],1);
for(auto x:srt){
Point p=x.fi;
// cout<<p.x<<' '<<p.y<<endl;
ll i=x.se.fi,j=x.se.se;
if(!p.ccw(a,b))bit.add(id[i][j],-1);
for(auto query:S[i]){
ll k=query.fi;
if(p.ccw(a,b)){
ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]+=bit.get(l,id[k].back()+1);
}
else{
ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]+=bit.get(id[k][0],r);
}
}
for(auto query:L[i]){
ll k=query.fi;
if(p.ccw(a,b)){
ll l=lower_bound(all(v[k]),b,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]+=bit.get(l,id[k].back()+1);
ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]-=bit.get(id[k][0],r);
}
else{
ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin();
if(l<v[k].size())l=id[k][l];
else l=id[k].back()+1;
ans[query.se]-=bit.get(l,id[k].back()+1);
ll r=lower_bound(all(v[k]),b,cmpy)-v[k].begin();
if(r<v[k].size())r=id[k][r];
else r=id[k].back()+1;
ans[query.se]+=bit.get(id[k][0],r);
}
}
if(p.ccw(a,b))bit.add(id[i][j],1);
// outv(ans);
}
}
for(ll x:ans)out(x);
}
Compilation message
dragon2.cpp: In function 'int main()':
dragon2.cpp:160:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:166:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
dragon2.cpp:175:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:179:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
179 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
dragon2.cpp:185:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
185 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:189:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
189 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
dragon2.cpp:222:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
222 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:228:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
228 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
dragon2.cpp:237:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
237 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:241:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
241 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
dragon2.cpp:247:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
247 | if(l<v[k].size())l=id[k][l];
| ~^~~~~~~~~~~~
dragon2.cpp:251:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
251 | if(r<v[k].size())r=id[k][r];
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
592 KB |
Output is correct |
2 |
Correct |
7 ms |
720 KB |
Output is correct |
3 |
Correct |
26 ms |
848 KB |
Output is correct |
4 |
Correct |
85 ms |
4620 KB |
Output is correct |
5 |
Correct |
62 ms |
3784 KB |
Output is correct |
6 |
Correct |
6 ms |
1104 KB |
Output is correct |
7 |
Correct |
7 ms |
1104 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
568 KB |
Output is correct |
10 |
Correct |
5 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
2916 KB |
Output is correct |
2 |
Correct |
82 ms |
3036 KB |
Output is correct |
3 |
Correct |
51 ms |
3372 KB |
Output is correct |
4 |
Correct |
50 ms |
3312 KB |
Output is correct |
5 |
Correct |
50 ms |
6488 KB |
Output is correct |
6 |
Correct |
44 ms |
2928 KB |
Output is correct |
7 |
Correct |
45 ms |
2988 KB |
Output is correct |
8 |
Correct |
43 ms |
2880 KB |
Output is correct |
9 |
Correct |
34 ms |
2684 KB |
Output is correct |
10 |
Correct |
33 ms |
2720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
592 KB |
Output is correct |
2 |
Correct |
7 ms |
720 KB |
Output is correct |
3 |
Correct |
26 ms |
848 KB |
Output is correct |
4 |
Correct |
85 ms |
4620 KB |
Output is correct |
5 |
Correct |
62 ms |
3784 KB |
Output is correct |
6 |
Correct |
6 ms |
1104 KB |
Output is correct |
7 |
Correct |
7 ms |
1104 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
568 KB |
Output is correct |
10 |
Correct |
5 ms |
592 KB |
Output is correct |
11 |
Correct |
47 ms |
2916 KB |
Output is correct |
12 |
Correct |
82 ms |
3036 KB |
Output is correct |
13 |
Correct |
51 ms |
3372 KB |
Output is correct |
14 |
Correct |
50 ms |
3312 KB |
Output is correct |
15 |
Correct |
50 ms |
6488 KB |
Output is correct |
16 |
Correct |
44 ms |
2928 KB |
Output is correct |
17 |
Correct |
45 ms |
2988 KB |
Output is correct |
18 |
Correct |
43 ms |
2880 KB |
Output is correct |
19 |
Correct |
34 ms |
2684 KB |
Output is correct |
20 |
Correct |
33 ms |
2720 KB |
Output is correct |
21 |
Correct |
47 ms |
2880 KB |
Output is correct |
22 |
Correct |
82 ms |
2948 KB |
Output is correct |
23 |
Correct |
366 ms |
3696 KB |
Output is correct |
24 |
Correct |
631 ms |
7304 KB |
Output is correct |
25 |
Correct |
147 ms |
8296 KB |
Output is correct |
26 |
Correct |
131 ms |
9556 KB |
Output is correct |
27 |
Correct |
58 ms |
7624 KB |
Output is correct |
28 |
Correct |
57 ms |
7684 KB |
Output is correct |
29 |
Correct |
142 ms |
8872 KB |
Output is correct |
30 |
Correct |
102 ms |
8384 KB |
Output is correct |
31 |
Correct |
114 ms |
8588 KB |
Output is correct |
32 |
Correct |
203 ms |
8888 KB |
Output is correct |
33 |
Correct |
491 ms |
9548 KB |
Output is correct |
34 |
Correct |
125 ms |
8940 KB |
Output is correct |
35 |
Correct |
108 ms |
8912 KB |
Output is correct |
36 |
Correct |
121 ms |
9436 KB |
Output is correct |
37 |
Correct |
140 ms |
9464 KB |
Output is correct |
38 |
Correct |
581 ms |
9088 KB |
Output is correct |
39 |
Correct |
537 ms |
9248 KB |
Output is correct |
40 |
Correct |
482 ms |
9664 KB |
Output is correct |
41 |
Correct |
122 ms |
8988 KB |
Output is correct |
42 |
Correct |
130 ms |
8932 KB |
Output is correct |
43 |
Correct |
145 ms |
8896 KB |
Output is correct |
44 |
Correct |
60 ms |
5184 KB |
Output is correct |
45 |
Correct |
60 ms |
5184 KB |
Output is correct |
46 |
Correct |
64 ms |
5220 KB |
Output is correct |
47 |
Correct |
59 ms |
5184 KB |
Output is correct |
48 |
Correct |
62 ms |
5140 KB |
Output is correct |
49 |
Correct |
61 ms |
5224 KB |
Output is correct |