#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
int bit[30005];
vector<pii> v[30005];
int Q(pii x)
{
if(x.F<=0&&x.S<0) return 1;
if(x.F>0&&x.S<=0) return 2;
if(x.F>=0&&x.S>0) return 3;
return 4;
}
bool yee(pii a,pii b)
{
if(Q(a)!=Q(b)) return Q(a)<Q(b);
return (ll)a.S*b.F<(ll)b.S*a.F;
}
bool yee2(pair<pii,pii> a,pair<pii,pii> b)
{
return yee(a.F,b.F);
}
bool yee3(pii a,pii b)
{
return !yee(a,b)&&!yee(b,a);
}
void modify(int x,int val,int t)
{
for(;x<=v[t].size();x+=x&-x) bit[x]+=val;
}
int query(int x)
{
int re=0;
for(;x>0;x-=x&-x) re+=bit[x];
return re;
}
struct mode
{
int p,l,r,type;//count the number of l<=value<=r in [1,p]
ll *i;
//type 1:+ type -1:-
bool operator<(const mode &a)const{
return p<a.p;
}
};
vector<pii> tb[30005];
vector<pair<pii,pii>> seq[30005],org[30005];
vector<int> pl[30005];
vector<mode> qs[30005];
pii operator-(const pii &a,const pii &b){return MP(a.F-b.F,a.S-b.S);}
pii angle(pii c,pii x){
x=x-c;
return x;
}
ll ans[100005],m;
void add_q(pii _l,pii _r,pii _x,pii _y,int p,int i)
{
int l=lower_bound(ALL(seq[p]),MP(_l,MP(0,0)),yee2)-seq[p].begin();
int r=upper_bound(ALL(seq[p]),MP(_r,MP(0,0)),yee2)-seq[p].begin();
int x=lower_bound(ALL(v[p]),_x,yee)-v[p].begin();
int y=upper_bound(ALL(v[p]),_y,yee)-v[p].begin();
qs[p].pb(mode{r,x,y,1,&ans[i]}),qs[p].pb(mode{l,x,y,-1,&ans[i]});
}
void solve()
{
for(int i=1;i<=m;++i)
{
sort(ALL(qs[i]));
int nw=0;
for(auto j:qs[i])
{
while(nw<j.p)
++nw,modify(pl[i][nw-1],1,i);
int sum=query(j.r)-query(j.l);
*j.i+=j.type*sum;
}
for(int j=1;j<=nw;++j)
modify(pl[i][j-1],-1,i);
qs[i].clear();
}
}
pii rg(pii x)
{
return MP(-x.F,-x.S);
}
vector<pair<pii,pii>> interval(pii x)//x clockwise 180.
{
if(Q(x)>=3) return vector<pair<pii,pii>>{MP(rg(x),x)};
return vector<pair<pii,pii>>{MP(rg(x),MP(-1,0)),MP(MP(-1000000001,-1),x)};
}
int main()
{jizz
int n,q,x,y,c;
pii a,b,ag;
cin >> n >> m;
for(int i=0;i<n;++i)
cin >> x >> y >> c,tb[c].pb(MP(x,y));
cin >> a.F >> a.S >> b.F >> b.S >> q;
if(a>b) swap(a,b);
for(int i=1;i<=m;++i)
{
for(auto j:tb[i])
{
pii s=angle(a,j),t=angle(b,j);
org[i].pb(MP(s,t)),v[i].pb(t);
}
seq[i]=org[i],sort(ALL(seq[i]),yee2);
sort(ALL(v[i]),yee),v[i].resize(unique(ALL(v[i]),yee3)-v[i].begin());
for(auto j:seq[i])
pl[i].pb(upper_bound(ALL(v[i]),j.S,yee)-v[i].begin());
}
ag=angle(a,b);
auto inter=interval(ag);
for(int i=0;i<q;++i)
{
cin >> x >> y;
if(tb[x].size()>tb[y].size()) swap(x,y);
for(int j=0;j<tb[x].size();++j)
{
int flag=0;
for(auto k:inter)
if((yee(k.F,org[x][j].F)||yee3(k.F,org[x][j].F))&&(yee(org[x][j].F,k.S)||yee3(org[x][j].F,k.S)))
flag=1;
vector<pair<pii,pii>> L,R;
if(flag)
L=interval(rg(org[x][j].F)),R=interval(org[x][j].S);
else
L=interval(org[x][j].F),R=interval(rg(org[x][j].S));
for(auto l:L)
for(auto r:R)
add_q(l.F,l.S,r.F,r.S,y,i);
}
}
solve();
for(int i=0;i<q;++i)
cout << ans[i] << "\n";
}
/*
4 2
0 1 1
0 -1 1
1 2 2
-6 1 2
-2 0 2 0
2
1 2
2 1
3 2
-1000000000 -1 1
-999999998 -1 1
0 0 2
999999997 1 999999999 1
1
1 2
6 3
2 -1 1
1 0 1
0 3 2
2 4 2
5 4 3
3 9 3
0 0 3 3
6
1 2
1 3
2 1
2 3
3 1
3 2
*/
Compilation message
dragon2.cpp: In function 'void modify(int, int, int)':
dragon2.cpp:47:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(;x<=v[t].size();x+=x&-x) bit[x]+=val;
~^~~~~~~~~~~~~
dragon2.cpp: In function 'int main()':
dragon2.cpp:144:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<tb[x].size();++j)
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
5116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
8092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
5116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |