题目链接

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. 12. 集合类Collection和Map

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. javaweb入门---web服务器与HTTP协议基础

    上文web基础简介了web到底是什么,以及身为Java开发人员需要掌握的地方.本文将解答web服务器是什么,怎么使用?还有关于http协议的基础知识. web服务器 web服务器的大概念很广泛,但是通 ...

  3. Linux学习笔记05之网络基础知识

    一.OSI参考模型:适用于所有网络,现有模型,后有协议 1.应用层:应用程序.用户接口 2.表示层:编码转换.压缩.解压.加密等 3.会话层:建立.维护.拆除会话 4.传输层规定了应用程序的的接口 协 ...

  4. 【Android Studio】提示代码忽略大小写

    在 Preference... 中找到如下,设置 Case sensitive completion 为 None 即可,如下图: PS: 该截图是 Intellij IDEA (Android St ...

  5. 调试过程中发现按f5无法走进jdk源码

    debug 模式 ,在fis=new FileInputStream(file); 行打断点 调试过程中发现按f5无法走进jdk源码 package com.lzl.spring.test; impo ...

  6. Go组件学习——gorm四步带你搞定DB增删改查

    1.简介 ORM Object-Relationl Mapping, 它的作用是映射数据库和对象之间的关系,方便我们在实现数据库操作的时候不用去写复杂的sql语句,把对数据库的操作上升到对于对象的操作 ...

  7. 分布式ID系列(4)——Redis集群实现的分布式ID适合做分布式ID吗

    首先是项目地址: https://github.com/maqiankun/distributed-id-redis-generator 关于Redis集群生成分布式ID,这里要先了解redis使用l ...

  8. 佳木斯集训Day1

    23333第一次写博客 其实在佳木斯集训之前我都已经两三个月没打代码了 在佳木斯的时候前几天真心手生,导致了前几次考试考的很差... D1的考试还是比较良心的,T1是一道大模拟,直接枚举最后几位是00 ...

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

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

  10. Re-Architecting the Video Gatekeeper(一)

    原文 https://medium.com/netflix-techblog/re-architecting-the-video-gatekeeper-f7b0ac2f6b00 本文介绍了了内容配置工 ...