-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd.cpp
125 lines (117 loc) · 2.98 KB
/
d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// template.cpp : Defines the entry point for the console application.
//
#include <climits>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <list>
#include <tuple>
#include <ctime>
#include <cassert>
#include <cstring>
using namespace std;
//type shortcuts
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef tuple<ll,ll,ll> L3;
//constants
const ll INF=0x1fffffffffffffff;
const double INFD=1e20;
const double EPS=1e-9;
const double PI = atan(1) * 4;
const ll M = 1000000007;
//for-loop shortcut
#define rng(i,begin,end) for (ll i=begin;i<end;++i)
//scanf shortcuts
ll scan() {ll r;scanf("%lld",&r);return r;}
void scanstr(char *buf){scanf("%s",buf);}
void dump(VI &arr){
for(auto item:arr){
cerr<<item<<' ';
}
cerr<<endl;
}
ll getSubAns(map<ll,ll> &b2c){
ll ans=0;
ll presum=0;
for (auto const&bc:b2c){
ans+=bc.second*presum;
presum+=bc.second;
}
return ans;
}
ll calc(vector<L3> &people, VI &xsidLeft, VI &xsidRight, VI &ysidDown, VI &ysidUp){
ll k=people.size();
map<ll,ll> b2c;
ll ans=0;
rng(i,0,k){
ll x,y,pid;
ll x2,y2,pid2;
ll x0,y0,pid0;
tie(x,y,pid)=people[i];
tie(x2,y2,pid2)=(i<k-1)?people[i+1]:make_tuple(0ll,0ll,0ll);
tie(x0,y0,pid0)=(i>0)?people[i-1]:make_tuple(0ll,0ll,0ll);
if (xsidLeft[pid]<xsidRight[pid]&&ysidDown[pid]==ysidUp[pid]){
if (i==0||xsidRight[pid0]<xsidRight[pid]){
}
b2c[ysidDown[pid]]++;
if (i==k-1||xsidLeft[pid]<xsidLeft[pid2]) {
ans+=getSubAns(b2c);
b2c.clear();
}
}
}
return ans;
}
int main()
{
ll tn=scan();
rng(ti,0,tn){
ll n=scan();
ll m=scan();
ll k=scan();
VI x(n);
rng(i,0,n){
x[i]=scan();
}
VI y(m);
rng(i,0,m){
y[i]=scan();
}
vector<L3> pxys(k);
vector<L3> pyxs(k);
VI xsidLeft(k);
VI xsidRight(k);
VI ysidDown(k);
VI ysidUp(k);
rng(i,0,k){
ll px=scan();
ll py=scan();
xsidRight[i]=lower_bound(x.begin(),x.end(),px)-x.begin();
xsidLeft[i]=(x[xsidRight[i]]==px)?xsidRight[i]:(xsidRight[i]-1);
ysidUp[i]=lower_bound(y.begin(),y.end(),py)-y.begin();
ysidDown[i]=(y[ysidUp[i]]==py)?ysidUp[i]:(ysidUp[i]-1);
pxys[i]=make_tuple(px,py,i);
pyxs[i]=make_tuple(py,px,i);
}
sort(pxys.begin(),pxys.end());
sort(pyxs.begin(),pyxs.end());
ll ans=calc(pxys,xsidLeft,xsidRight,ysidDown,ysidUp)+calc(pyxs,ysidDown,ysidUp,xsidLeft,xsidRight);
cout<<ans<<endl;
}
return 0;
}