Rikka with Cake(线段树+线段树)

ikka's birthday is on June 12th. The story of this problem happens on that day.

Today is Rikka's birthday. Yuta prepares a big cake for her: the shape of this cake is a rectangular of nn centimeters times mm centimeters. With the guidance of a grimoire, Rikka is going to cut the cake.

For simplicity, Rikka firstly builds a Cartesian coordinate system on the cake: the coordinate of the left bottom corner is (0,0)(0,0) while that of the right top corner is (n,m)(n,m). There are KK instructions on the grimoire: The iith cut is a ray starting from (xi,yi)(xi,yi) while the direction is DiDi. There are four possible directions: L, passes (xi−1,yi)(xi−1,yi); R, passes (xi+1,yi)(xi+1,yi); U, passes (xi,yi+1)(xi,yi+1); D, passes (xi,yi−1)(xi,yi−1).

Take advantage of the infinite power of Tyrant's Eye, Rikka finishes all the instructions quickly. Now she wants to count the number of pieces of the cake. However, since a huge number of cuts have been done, the number of pieces can be very large. Therefore, Rikka wants you to finish this task.

Input

The first line of the input contains a single integer T(1≤T≤100)T(1≤T≤100), the number of the test cases.

For each test case, the first line contains three positive integers n,m,K(1≤n,m≤109,1≤K≤105)n,m,K(1≤n,m≤109,1≤K≤105), which represents the shape of the cake and the number of instructions on the grimoire.

Then KK lines follow, the iith line contains two integers xi,yi(1≤xi<n,1≤yi<m)xi,yi(1≤xi<n,1≤yi<m) and a char Di∈{Di∈{'L','R','U','D'}}, which describes the iith cut.

The input guarantees that there are no more than 55 test cases with K>1000K>1000, and no two cuts share the same xx coordinate or yy coordinate, i.e., ∀1≤i<j≤K∀1≤i<j≤K, xi≠xjxi≠xj and yi≠yjyi≠yj.

Output

For each test case, output a single line with a single integer, the number of pieces of the cake.

Hint


The left image and the right image show the results of the first and the second test case in the sample input respectively. Clearly, the answer to the first test case is 33 while the second one is 55.

 

Sample Input

2
4 4 3
1 1 U
2 2 L
3 3 L
5 5 4
1 2 R
3 1 U
4 3 L
2 4 D

Sample Output

3
5

题意

从一个点开始往一个方向切蛋糕,问切完k次后,切了多少块蛋糕。

思路

容易得出,蛋糕块数为交点数加一。可以通过建两次线段树(一次求方向向下的交点数,一次求方向向上的交点数)求解。

介绍一下方向向上的交点数得方法。

先离散化坐标,按纵坐标从大到小更新区间,计算交点数。说的不太明白,讲一下过程,以样例一为例。

图中有三个点(1,1)、(2,2)、(3,3),方向水平得更新区间长度,这样每个点就是占单位一得长度

(3,3)点是方向向左的 那么【1,3】区间内的点值都+1,即sum【1】=1,sum[2]=1,sum[3]=1。

(2,2)点是方向向左的,那么[1,2]区间的点的值都+1,这时候sum[1]=2,sum[2]=2,sum[3]=1

(1,1)点是方向向上的,这时候就计算交点个数,看sum[1]等于几就是这一条向上的线与前面那两条的交点个数,注意sum数组的下标都是横坐标。

方向向下的交点数就是将纵坐标从小到大更新区间,计算出总交点数加一就是答案。

代码

#include<bits/stdc++.h>
#define ll long long
#define N  400010
using namespace std;
int Sum[N],add[N];
vector<int>v[4][N];
struct node{
    int x,y;
    int id;
}eg[N];
void Clear(int n){
    for(int i=0;i<4;i++)
        for(int j=0;j<=n;j++)
            v[i][j].clear();
    memset(Sum,0,sizeof(Sum));
    memset(add,0,sizeof(add));
}
int change(char c){
    if(c=='U')return 0;
    if(c=='R')return 1;
    if(c=='D')return 2;
    if(c=='L')return 3;
}
void pushup(int o){
    Sum[o]=Sum[o<<1]+Sum[o<<1|1];
}
void pushdown(int o,int len){
    if(add[o]){
        add[o<<1]+=add[o];
        add[o<<1|1]+=add[o];
        Sum[o<<1]+=add[o]*(len-(len>>1));
        Sum[o<<1|1]+=add[o]*(len>>1);
        add[o]=0;
    }
}
void update(int x,int y,int l,int r,int o,int c){
    if(l>=x&&r<=y){
        Sum[o]+=(r-l+1)*c;
        add[o]+=c;
        return;
    }
    pushdown(o,r-l+1);
    int mid=(l+r)>>1;
    if(x<=mid)update(x,y,l,mid,o<<1,c);
    if(y>mid)update(x,y,mid+1,r,o<<1|1,c);
    pushup(o);
}
int query(int x,int l,int r,int o){
    if(l==x&&r==x){
        return Sum[o];
    }
    pushdown(o,r-l+1);
    int mid=(l+r)>>1;
    if(x<=mid)return query(x,l,mid,o<<1);
    else return query(x,mid+1,r,o<<1|1);
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        map<int,int>c,r;
        for(int i=1;i<=k;i++){
            char str[2];
            scanf("%d%d%s",&eg[i].x,&eg[i].y,str);
            eg[i].id=change(str[0]);
            c[eg[i].x]=r[eg[i].y]=1;
        }
        int h=0,w=0;
        for(auto it=c.begin();it!=c.end();it++)it->second=++h;
        for(auto it=r.begin();it!=r.end();it++)it->second=++w;
        for(int i=1;i<=k;i++){//离散化
            eg[i].x=c[eg[i].x];eg[i].y=r[eg[i].y];
            v[eg[i].id][eg[i].y].push_back(eg[i].x);
        }
        ll cnt=0;
        for(int i=1;i<=h;i++){ //按纵坐标,从下往上扫,找向下的线段
            int len=v[1][i].size();
            for(int j=0;j<len;j++)update(v[1][i][j],w,1,w,1,1);
            len=v[3][i].size();
            for(int j=0;j<len;j++)update(1,v[3][i][j],1,w,1,1);
            len=v[2][i].size();
            for(int j=0;j<len;j++)cnt+=query(v[2][i][j],1,w,1);
            
        }
       memset(Sum,0,sizeof(Sum));
        memset(add,0,sizeof(add));
        for(int i=h;i>=1;i--){ //按纵坐标,从上往下扫,找向上的线段
            int len=v[1][i].size();
            for(int j=0;j<len;j++)update(v[1][i][j],w,1,w,1,1);
            len=v[3][i].size();
            for(int j=0;j<len;j++)update(1,v[3][i][j],1,w,1,1);
            len=v[0][i].size();
            for(int j=0;j<len;j++)cnt+=query(v[0][i][j],1,w,1);
        }
        printf("%lld\n",cnt+1);
        Clear(h);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩辕青山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值