Rikka with Cake(线段树求线段之间的交点个数)

Rikka'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≠xjand 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

2019杭电多校第九场B题

题意:在一个n*m的矩形内,有k个点,每个点都有一个方向(U,R,D,L)代表从这个点开始根据这个方向切割这个矩形。问最后这个矩形被切割成多少部分?

思路:通过观察可以得出,最后剩余的部分其实是矩形内交点的个数+1.那么此题就变成求平面内,所有线段之间的交点个数。首先我们要将坐标离散化,然后通过线段树来求交点的个数。

求向上方向交点与上图同理。

代码如下:

#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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值