题目链接、
压缩y轴,维护x的树状数组
/*author:revolIA*/
#include<bits/stdc++.h>
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
using namespace std;
typedef long long ll;
template <class T> T max(T &a,const T &b){return a>b?a:b;}
template <class T> T min(T &a,const T &b){return a<b?a:b;}
const int maxn = 1e5+7,inf = 1e9+7;
int Tree[maxn],n,m,k,T;
void add(int x,int val){while(x<=n)Tree[x] += val,x+=x&-x;}
int query(int x,int ans = 0){while(x)ans += Tree[x],x-=x&-x;return ans;}
vector<int> vex,vey;
vector<pair<int,int> > Add[maxn],Query[maxn];
struct node{
int x,y;
char opt[2];
}rel[maxn];
int main(){
scanf("%d",&T);
while(T--){
vex.clear(),vey.clear();
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++){
scanf("%d%d%s",&rel[i].x,&rel[i].y,rel[i].opt);
vex.push_back(rel[i].x);
vey.push_back(rel[i].y);
}
sort(vex.begin(),vex.end()),sort(vey.begin(),vey.end());
vex.erase(unique(vex.begin(),vex.end()),vex.end());
vey.erase(unique(vey.begin(),vey.end()),vey.end());
n = vex.size(),m = vey.size();
for(int i=1;i<=k;i++){
rel[i].x = lower_bound(vex.begin(),vex.end(),rel[i].x)-vex.begin()+1;
rel[i].y = lower_bound(vey.begin(),vey.end(),rel[i].y)-vey.begin()+1;
}
for(int i=1;i<=m;i++)Add[i].clear(),Query[i].clear();
for(int i=1;i<=n;i++)Tree[i] = 0;
for(int i=1;i<=k;i++){
int x = rel[i].x,y = rel[i].y;
if(rel[i].opt[0] == 'U'){
Add[y].push_back({x,1});
}else if(rel[i].opt[0] == 'D'){
Add[1].push_back({x,1});
Add[y+1].push_back({x,-1});
}else if(rel[i].opt[0] == 'L'){
Query[y].push_back({1,x});
}else{
Query[y].push_back({x,n});
}
}
int ans = 0;
for(int i=1;i<=m;i++){
for(auto j:Add[i])add(j.first,j.second);
for(auto j:Query[i])ans += query(j.second)-query(j.first-1);
}
printf("%d\n",ans+1);
}
return 0;
}