HDU多校第九場

Rikka with Cake

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1267    Accepted Submission(s): 503

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) while that of the right top corner is (n,m). There are K instructions on the grimoire: The ith cut is a ray starting from (xi,yi) while the direction is Di. There are four possible directions: L, passes (xi−1,yi); R, passes (xi+1,yi); U, passes (xi,yi+1); D, passes (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), 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), 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 Di∈{'L','R','U','D'}, which describes the ith cut.
The input guarantees that there are no more than 5 test cases with K>1000, and no two cuts share the same x coordinate or y coordinate, i.e., ∀1≤i<j≤K, xi≠xj and 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 3while the second one is 5.

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

题意:给出一个n*m的矩形,K次操作,每次从点(x,y)开始向上或下或左或右切割,求最后矩形分成了多少块。

解法之一:可以发现这些射线的交点的个数+1就是最后的答案。所以我们可以离散化y轴,按照x的大小排序,依次从小到大找左右射线和上下射线的交点。

#include <iostream>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=100010;
ll t2[maxn];
int len1,len2;
ll n,m;int K;
struct point
{
    ll x,y;
    char op[3];
}a[maxn];

void init_hash2(int N)
{
    for(int i=1;i<=N;i++)
    {
        t2[i]=a[i].y;
    }
    sort(t2+1,t2+1+N);
    //len2=unique(t2+1,t2+1+N)-t2-1;
}
int hashs2(int x)
{
    return lower_bound(t2+1,t2+1+K,x)-t2;
}
struct node
{
    int l,r;
    ll va;
    ll lazy;
}tree[maxn<<2];
void pushup(int rt)
{
    tree[rt].va=tree[rt<<1].va+tree[rt<<1|1].va;
}
void build(int rt,int l,int r)
{
    tree[rt].l=l;tree[rt].r=r;
    tree[rt].va=0;tree[rt].lazy=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void pushdown(int rt)
{
    if(tree[rt].lazy!=0)
    {
        int mid=(tree[rt].r+tree[rt].l)>>1;
        tree[rt<<1].lazy+=tree[rt].lazy;
        tree[rt<<1|1].lazy+=tree[rt].lazy;
        tree[rt<<1].va+=(mid-tree[rt].l+1)*tree[rt].lazy;
        tree[rt<<1|1].va+=(tree[rt].r-mid)*tree[rt].lazy;
        tree[rt].lazy=0;
    }
}
void update(int rt,int L,int R,ll x)
{
    if(tree[rt].l>=L&&tree[rt].r<=R)
    {
        tree[rt].va+=1ll*(tree[rt].r-tree[rt].l+1)*x;
        tree[rt].lazy+=x;
        return ;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=L) update(rt<<1,L,R,x);
    if(mid<R) update(rt<<1|1,L,R,x);
    pushup(rt);
}
ll query(int rt,int x)
{
    if(tree[rt].l==tree[rt].r)
    {
        return tree[rt].va;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    ll ans=0;
    if(mid>=x)
    {
        ans+=query(rt<<1,x);
    }
    else ans+=query(rt<<1|1,x);
    return ans;
}
bool cmp(point aa,point bb)
{
    return aa.x<bb.x;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld %lld %d",&n,&m,&K);

        for(int i=1;i<=K;i++)
        {
            scanf("%lld %lld %s",&a[i].x,&a[i].y,a[i].op);
        }
        sort(a+1,a+1+K,cmp);
        init_hash2(K);
        build(1,1,K);
        ll ans=1;
        for(int i=1;i<=K;i++)
        {
            if(a[i].op[0]=='U')
            {
                int l=hashs2(a[i].y);
                update(1,l,K,1);
            }
            else if(a[i].op[0]=='D')
            {
                int l=hashs2(a[i].y);
                update(1,1,l,1);
            }
            else if(a[i].op[0]=='L')//统计在这个射线左边的交点
            {
                int x=hashs2(a[i].y);
                ans+=query(1,x);
            }
        }
        build(1,1,K);
        for(int i=K;i>=1;i--)
        {
            if(a[i].op[0]=='U')
            {
                int l=hashs2(a[i].y);
                update(1,l,K,1);
            }
            else if(a[i].op[0]=='D')
            {
                int l=hashs2(a[i].y);
                update(1,1,l,1);
            }
            else if(a[i].op[0]=='R')//统计这个射线右边的交点
            {
                int x=hashs2(a[i].y);
                ans+=query(1,x);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值