hdu-6681 Rikka with Cake
题目链接
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的更多相关文章
- hdu 6681 Rikka with Cake(扫描线)
题意:给你一个n*m的的矩形框 现在又k条射线 问这个矩形框会被分为多少个区域 思路:之前的想法是枚举边界然后线段树扫一遍计算一下矩形个数 复杂度果断不行 后面发现其实答案就是交点数+1 然后就用线段 ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- hdu 4454 Stealing a Cake(三分之二)
pid=4454" target="_blank" style="">题目链接:hdu 4454 Stealing a Cake 题目大意:给定 ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5
思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...
- HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5
JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...
- HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5
看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...
- hdu多校第九场 1002 (hdu6681) Rikka with Cake 树状数组维护区间和/离散化
题意: 在一块长方形蛋糕上切若干刀,每一刀都是从长方形某条边开始,垂直于这条边,但不切到对边,求把长方形切成了多少块. 题解: 块数=交点数+1 因为对于每个交点,唯一且不重复地对应着一块蛋糕. 就是 ...
- HDU 5828 Rikka with Sequence (线段树)
Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
随机推荐
- PyCharm字体大小调整
1.点击左上角File----settings----keymap----------搜索increase,选中,increase font size--------再选择enter mouse sh ...
- 实战SpringCloud响应式微服务系列教程(第二章)
接上一篇:实战SpringCloud响应式微服务系列教程(第一章) 1.1.2背压 背压是响应式编程的核心概念,这一节也是我们了解响应式编程的重点. 1.背压的机制 在生产者/消费者模型中,我们意识到 ...
- Pyenv虚拟环境的创建(虚拟机)
创建pyenv虚拟环境 sudo yum install openssl* 安装其所需要的库文件 git clone https://github.com/yyuu/pyenv.git ~/.pyen ...
- 以太坊RLPx传输协议
本文主要内容翻译自:The RLPx Transport Protocol,其中添加了一些个人的理解,由于密码学水平有限,不正确之处望指正.另外原文可能已经更新,最新内容请直接阅读原文. 本文档定义了 ...
- 用HTML5的canvas做一个时钟
对于H5来说,canvas可以说是它最有特色的一个地方了,有了它之后我们可以随意的在网页上画各种各样的图形,做一些小游戏啊什么的.canvas这个标签的用法,在网上也有特别多的教程了,这里就不作介绍了 ...
- ThreadLocal为什么会内存泄漏
1.首先看下ThreadLocal的原理图: 在ThreadLocal的生命周期中,都存在这些引用. 其中,实线代表强引用,虚线代表弱引用: 2.ThreadLocal的实现:每个Thread维护一个 ...
- FTP工具-FileZilla安装使用教程
1.首先,百度搜索“FileZilla”,进入官网,下载地址:https://www.filezilla.cn/download/client ,根据自己电脑配置去下载 2.下载本地,双击运行安装程 ...
- MQ服务器端和客户端通信浅谈
MQ服务器端和客户端通信浅谈 1. WebSphere MQ的服务端的安装和配置 (1)创建名为venus.queue.manager的默认队列管理器. 在DOS窗口命令提示符下,输入以下命令: cr ...
- python学习之路(2)---字符编码
二进制编码 bin(300) python计算二进制编码,十进制转2进制 一个二进制位就是1bit 1bit代表了8个字节,00001111 1bit = 1bytes 缩写1b 1kb ...
- vs中的system指令
vs中的system指令 system(“命令语句”);必须要用到头文件include<stdio.h> system里可以加许多指令 取消关机 shutdown -a 关机 sh ...