题目链接

hdu-6681

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

题意

矩形平面上有一些水平或垂直的射线,问这些射线把这个矩形分成了几块

题解

有一个神仙公式叫欧拉公式:\(V-E+F=2\),V是点数,E是边数,F是平面数

V = 矩形四个点 + 射线端点n + 射线到矩形交点n + 射线间焦点c = 2n + 4 + c

E = 矩形四条边 + 矩形四条边被射线分割的边n + 射线n条 + 射线与射线分割的边(每个交点都会产生2条新边) 2c

所以F = 2 + c,再扣掉矩形外的平面答案就是1 + c

求交点个数只要从左往右遍历垂直射线,遍历过程中每遇到水平射线左端点则在树状数组上+1,遇到水平射线右端点就在树状数组上-1

代码

#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5+5;
int C[mx];
int lowbit(int x) {
return x & -x;
}
vector <int> vy;
struct Seg {
int x, y1, y2;
bool operator < (Seg other) const {
return x < other.x;
}
}seg[mx]; struct Point {
int x, y, val;
bool operator < (Point other) const {
return x < other.x;
}
}point[mx]; int getid(int y) {
return lower_bound(vy.begin(), vy.end(), y) - vy.begin() + 1;
} void update(int x, int val) {
for (int i = x; i < mx; i+=lowbit(i))
C[i] += val;
} int query(int l, int r) {
int sumr = 0, suml = 0;
for (int i = r; i > 0; i-=lowbit(i)) sumr += C[i];
for (int i = l-1; i > 0; i-=lowbit(i)) suml += C[i];
return sumr-suml;
} int main() {
int T;
scanf("%d", &T); while (T--) {
memset(C, 0, sizeof(C));
vy.clear(); int n, m, k, x, y;
int cnts = 0, cntp = 0;
char dir[2];
scanf("%d%d%d", &n, &m, &k);
vy.push_back(0); vy.push_back(m);
for (int i = 1; i <= k; i++) {
scanf("%d%d%s", &x, &y, dir);
vy.push_back(y);
if (dir[0] == 'L') {
point[cntp++] = Point{0, y, 1};
point[cntp++] = Point{x, y, -1};
} else if (dir[0] == 'R') {
point[cntp++] = Point{x, y, 1};
point[cntp++] = Point{n, y, -1};
} else if (dir[0] == 'U') {
seg[cnts++] = Seg{x, y, m};
} else {
seg[cnts++] = Seg{x, 0, y};
}
}
sort(vy.begin(), vy.end());
sort(seg, seg+cnts);
sort(point, point+cntp);
for (int i = 0; i < cnts; i++) {
seg[i].y1 = getid(seg[i].y1);
seg[i].y2 = getid(seg[i].y2);
}
for (int i = 0; i < cntp; i++) point[i].y = getid(point[i].y);
int pos = 0;
int sum = 0;
for (int i = 0; i < cnts; i++) {
while (point[pos].x < seg[i].x) {
update(point[pos].y, point[pos].val);
pos++;
}
sum += query(seg[i].y1, seg[i].y2);
}
printf("%d\n", sum+1);
}
return 0;
}

hdu-6681 Rikka with Cake的更多相关文章

  1. hdu 6681 Rikka with Cake(扫描线)

    题意:给你一个n*m的的矩形框 现在又k条射线 问这个矩形框会被分为多少个区域 思路:之前的想法是枚举边界然后线段树扫一遍计算一下矩形个数 复杂度果断不行 后面发现其实答案就是交点数+1 然后就用线段 ...

  2. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  3. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  4. hdu 4454 Stealing a Cake(三分之二)

    pid=4454" target="_blank" style="">题目链接:hdu 4454 Stealing a Cake 题目大意:给定 ...

  5. HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5

    思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...

  6. HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5

    思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...

  7. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  8. HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5

    看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...

  9. hdu多校第九场 1002 (hdu6681) Rikka with Cake 树状数组维护区间和/离散化

    题意: 在一块长方形蛋糕上切若干刀,每一刀都是从长方形某条边开始,垂直于这条边,但不切到对边,求把长方形切成了多少块. 题解: 块数=交点数+1 因为对于每个交点,唯一且不重复地对应着一块蛋糕. 就是 ...

  10. HDU 5828 Rikka with Sequence (线段树)

    Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

随机推荐

  1. 游戏开发3D基础知识

    概念学习: 向量 向量简介 我们将所有彼此平行的向量进行平移,使其起点与坐标原点重合,当某一向量的起始端与坐标原点重合,我们成该向量处于标准位置.这样,我们就可用向量的终点坐标来描述一个处于标准位置的 ...

  2. docker的基本安装和命令详解

    docker的安装 yum install docker-io docker的启动 /bin/systemctl start docker.service docker查找镜像 docker sear ...

  3. poj 1455 Crazy tea party

    这道题第一眼看去很难,其实不然,短短几行代码就搞定了. 说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律 ...

  4. Shiro权限框架与SpringMVC集成

    1.Shiro整合SpringMVC 我们学习Shiro框架肯定是要应用到Web项目上的,所以我们需要整合Shiro和SpringMVC 整合步骤: 第一步:SpringMVC框架的配置 spring ...

  5. Python学习系列(三)Python 入门语法规则1

    一.注释 ''' 多行注释 ''' #单行注释 '''    #example1.1 测试程序  时间:4/17/2017 i1=input("请输入用户名:") i2=input ...

  6. AWS Aurora数据库 Multi-Master 小测

    AWS Aurora Mysql终于推出了Multi-Master,直面硬刚Oracle RAC.在多一份数据库产品选择的小兴奋之余,我们也看看新推出的Multi-Master的特点(包括优缺点). ...

  7. LeetCode 85. 冗余连接 II

    题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...

  8. exe4j打包--jar打包exe

    本文重点介绍如何将我们写的java代码打包成在电脑上可以运行的exe文件.这里只介绍直接打包成exe的方法,至于打包成exe安装包下节介绍 test 软件准备 exe4j集合包下载地址(下节内容也在这 ...

  9. 实验:keepalived双主抢占模式和非抢占模式和IPVS

    内容: 一:概念.原理   二:实验过程 一.概念 一.keepalived原理及配置解析 keepalived:vrrp协议的实现 vrrp协议:virtual router redundancy ...

  10. 自己实现spring核心功能 三

    前言 前两篇已经基本实现了spring的核心功能,下面讲到的参数绑定是属于springMvc的范畴了.本篇主要将请求到servlet后怎么去做映射和处理.首先来看一看dispatherServlet的 ...