HDU - 6681 Rikka with Cake

Rikka with Cake

Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 524288/524288 K (Java/Others)

Problem Description

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 n centimeters times m 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) (0,0) while that of the right top corner is ( n , m ) (n,m) (n,m). There are K K K instructions on the grimoire: The i i ith cut is a ray starting from ( x i , y i ) (x_i,y_i) (xi,yi) while the direction is D i D_i Di. There are four possible directions: L L L, passes ( x i − 1 , y i ) (x_i−1,y_i) (xi1,yi); R R R, passes ( x i + 1 , y i ) (x_i+1,y_i) (xi+1,yi); U U U, passes ( x i , y i + 1 ) (x_i,y_i+1) (xi,yi+1); D D D, passes ( x i , y i − 1 ) (x_i,y_i−1) (xi,yi1).

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) T(1T100), the number of the test cases.

For each test case, the first line contains three positive integers n , m , K ( 1 ≤ n , m ≤ 1 0 9 , 1 ≤ K ≤ 1 0 5 ) n,m,K(1≤n,m≤10^9,1≤K≤10^5) n,m,K(1n,m109,1K105), which represents the shape of the cake and the number of instructions on the grimoire.

Then K lines follow, the ith line contains two integers %xi,yi(1≤xi<n,1≤yi<m)% and a char D i ∈ D_i∈ Di{‘L’,‘R’,‘U’,‘D’}, which describes the i i ith cut.

The input guarantees that there are no more than 5 test cases with K &gt; 1000 K&gt;1000 K>1000, and no two cuts share the same x x x coordinate or y y y coordinate, i.e., ∀ 1 ≤ i &lt; j ≤ K ∀1≤i&lt;j≤K 1i<jK, x i ≠ x j x_i≠x_j xi̸=xj and y i ≠ y j y_i≠y_j yi̸=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 3 while the second one is 5.

alt

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

Tips

题意

一个 n × m n\times m n×m 的矩形,左下角的坐标为 ( 0 , 0 ) (0,0) (0,0) ,右上角的坐标为 ( n , m ) (n,m) (n,m) 。现给定 K K K 条射线,每条射线的出发点都在矩形内部,且同一个横纵坐标上最多只有一个起点。问这些射线将矩形分成多少个部分。

题解

不难发现,最终的答案为射线的交点数加一。

当然,我们也可以证明。证明需要用到欧拉公式 V − E + F = 2 V-E+F=2 VE+F=2

设射线的交点共 c c c 个。则在这个图中, V = K + 4 + K + c = 2 K + c + 4 V=K+4+K+c=2K+c+4 V=K+4+K+c=2K+c+4 E = 2 ∑ ( c i + 1 ) + K + 4 = 2 K + 2 c + 4 E=2\sum (c_i+1)+K+4=2K+2c+4 E=2(ci+1)+K+4=2K+2c+4 。因此 F = 2 − V + E = c + 2 F=2-V+E=c+2 F=2V+E=c+2 。减去外面的无穷区域,得出答案为 c + 1 c+1 c+1

下面只需将坐标离散化然后用线段数求解即可。

首先从左向右考虑,我们用线段数存储每一个纵坐标上已经有多少条上下方向的射线经过,然后每遇到一条向左的边,则交点数加上这个纵坐标对应的射线数。然后从右向左考虑,存储每个纵坐标经过上下方向射线的数量,每遇到一条向右的射线,则交点数加上对应值。

最终,输出交点数加一即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
struct Line{
    int x,y;
    char d;
}line[MAXN];
ll tree[MAXN<<2],lazy[MAXN<<2];
bool cmpx(const Line &a,const Line &b){return a.x<b.x;}
bool cmpy(const Line &a,const Line &b){return a.y<b.y;}
int K;
void build(int rt=1,int l=1,int r=K){
    tree[rt]=lazy[rt]=0;
    if (l!=r){
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build(rt<<1|1,mid+1,r);
    }
}
void update(int a,int b,int rt=1,int l=1,int r=K){
    if (a>r||b<l) return;
    else if (a<=l&&b>=r){
        ++lazy[rt];
        tree[rt]+=r-l+1;
    }
    else{
        int mid=(l+r)>>1;
        update(a,b,rt<<1,l,mid);
        update(a,b,rt<<1|1,mid+1,r);
    }
}
inline void push_down(int rt,int l,int r){
    int mid=(l+r)>>1;
    lazy[rt<<1]+=lazy[rt];
    tree[rt<<1]+=1LL*(mid-l+1)*lazy[rt];
    lazy[rt<<1|1]+=lazy[rt];
    tree[rt<<1|1]+=1LL*(r-mid)*lazy[rt];
    lazy[rt]=0;
}
ll query(int y,int rt=1,int l=1,int r=K){
    if (l==y&&r==y) return tree[rt];
    else if (l<=y&&r>=y){
        push_down(rt,l,r);
        int mid=(l+r)>>1;
        if (mid>=y) return query(y,rt<<1,l,mid);
        else return query(y,rt<<1|1,mid+1,r);
    }
    return 0;
}
int t,n,m;
int main(){
    scanf("%d",&t);
    while (t--){
        scanf("%d%d%d",&n,&m,&K);
        for (int i=0;i<K;++i)
            scanf("%d%d %c",&line[i].x,&line[i].y,&line[i].d);
        sort(line,line+K,cmpy);
        for (int i=0;i<K;++i)
            line[i].y=i+1;
        sort(line,line+K,cmpx);
        ll res(0);
        build();
        for (int i=0;i<K;++i){
            if (line[i].d=='U') update(line[i].y,K);
            else if (line[i].d=='D') update(1,line[i].y);
            else if (line[i].d=='L') res+=query(line[i].y);
        }
        build();
        for (int i=K-1;i>=0;--i){
            if (line[i].d=='U') update(line[i].y,K);
            else if (line[i].d=='D') update(1,line[i].y);
            else if (line[i].d=='R') res+=query(line[i].y);
        }
        printf("%lld\n",++res);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值