hdu - 6681 Rikka with Cake 线段树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6681

题意:给定一个左下顶点为\left (0\right,0),右上顶点为\left ( n, \right m)的矩形,然后给你k条射线,每条射线的起点及方向(上下左右)都已知,射线的起点都位于矩形内,不包括矩形边界,且每条射线间不会有重叠,求矩形被分割成多少块。

思路:由于是射线,因此容易知道每一次射线的相交都会使答案+1,考虑用线段树求解。按照起始点的x坐标对射线排序,对y轴坐标离散化,从左往右更新,遇到向上或者向下的射线,对线段树上的对应区间+1,遇到向左的射线,对线段树进行单点查询,答案加上查询结果。初始化线段树后,从右往左更新,处理向右的射线,其余不变。处理完毕后即可得到答案。由于只需要线段树中叶节点的值,因此可以不用pushUp函数。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int N = 1e5+10;
 
struct line
{
    int x, y;
    char oper;
    bool operator <(const line &A) const
    {
        return x < A.x;
    }
}Line[N];
 
int x[N], y[N];
int sum[N<<2], lazy[N<<2];

void pushDown(int rt)
{
    if(!lazy[rt]) return ;
    sum[rt<<1] += lazy[rt];
    sum[rt<<1|1] += lazy[rt];
    lazy[rt<<1] += lazy[rt];
    lazy[rt<<1|1] += lazy[rt];
    lazy[rt] = 0;
}
 
void update(int L,int R,int l,int r,int rt)
{
    if(L <= l && r <= R){
        lazy[rt] += 1;
        sum[rt] += 1;
        return ;
    }
    int m = (l+r)>>1;
    pushDown(rt);
    if(L <= m) update(L,R,l,m,rt<<1);
    if(m <  R) update(L,R,m+1,r,rt<<1|1);
}
 
int query(int L,int l,int r,int rt)
{
    if(l == r)
        return sum[rt];
    int m = (l+r)>>1, ans = 0;
    pushDown(rt);
    if(L <= m) ans += query(L,l,m,rt<<1);
    else        ans += query(L,m+1,r,rt<<1|1);
    return ans;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T, n, m, M;
    long long ans = 0;
    cin>>T;
    while(T--){
        ans = 0;
        cin>>n>>m>>M;
        memset(sum, 0, sizeof sum);
        memset(lazy, 0, sizeof lazy);
        for(int i = 1; i <= M; ++i){
            cin>>Line[i].x>>Line[i].y>>Line[i].oper;
            x[i] = Line[i].x, y[i] = Line[i].y;
        }
        sort(y+1,y+M+1);
        int posy = unique(y+1,y+M+1)-y-1;
        sort(Line+1,Line+M+1);
        for(int i = 1; i <= M; ++i){
            int t = lower_bound(y+1,y+posy+1,Line[i].y)-y;
            if(Line[i].oper == 'U'){
                update(t,posy,1,posy,1);
            }
            else if(Line[i].oper == 'D'){
                update(1,t,1,posy,1);
            }
            else if(Line[i].oper == 'L'){
                ans += query(t,1,posy,1);
            }
        }
        memset(sum, 0, sizeof sum);
        memset(lazy, 0, sizeof lazy);
        for(int i = M; i >= 1; --i){
        	int t = lower_bound(y+1,y+posy+1,Line[i].y)-y;
            if(Line[i].oper == 'U'){
                update(t,posy,1,posy,1);
            }
            else if(Line[i].oper == 'D'){
                update(1,t,1,posy,1);
            }
            else if(Line[i].oper == 'R'){
                ans += query(t,1,posy,1);
            }
        }
        cout<<ans+1<<'\n';
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值