Rikka with Cake

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

Problem Description

Rikka’s birthday is on June 12−th12-th12−th. The story of this problem happens on that day.

Today is Rikka′sRikka'sRikka′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, RikkaRikkaRikka is going to cut the cake.

For simplicity, RikkaRikkaRikka firstly builds a Cartesian coordinate system on the cake: the coordinate of the left bottom corner is (0,0)(0,0)(0,0) while that of the right top corner is (n,m)(n,m)(n,m). There are KKK instructions on the grimoire: The i−thi-thi−th cut is a ray starting from (xi,yi)(x_i,y_i)(xi​,yi​) while the direction is DiD_iDi​. There are four possible directions: LLL, passes (xi−1,yi)(x_{i−1},y_i)(xi−1​,yi​); R, passes (xi+1,yi)(x_{i+1},y_i)(xi+1​,yi​); U, passes (xi,yi+1)(x_i,y_{i+1})(xi​,yi+1​); D, passes (xi,yi−1)(x_i,y_{i−1})(xi​,yi−1​).

Take advantage of the infinite power of Tyrant′sTyrant'sTyrant′s Eye, RikkaRikkaRikka 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, RikkaRikkaRikka wants you to finish this task.

Input

The first line of the input contains a single integer T(1≤T≤100)T(1\leq T\leq 100)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)n,m,K(1\leq n,m\leq 10^9,1\leq K\leq 10^5)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 KKK lines follow, the ith line contains two integers xi,yi(1≤xi&lt;n,1≤yi&lt;m)x_i,y_i(1\leq x_i&lt;n,1\leq y_i&lt;m)xi​,yi​(1≤xi​<n,1≤yi​<m) and a char Di∈{′L′,′R′,′U′,′D′}D_i∈\{'L','R','U','D'\}Di​∈{′L′,′R′,′U′,′D′}, which describes the ith cut.

The input guarantees that there are no more than 5 test cases with K&gt;1000K&gt;1000K>1000, and no two cuts share the same xxx coordinate or yyy coordinate, i.e.,∀1≤i&lt;j≤K,xi!=xjandyi!=yj.i.e., \forall1\leq i&lt;j\leq K, x_i\ != x_j and\ y_i\ !=\ y_j.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 333 while the second one is 555.

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

Source

2019 Multi-University Training Contest 9

题意

  • 就是给你一个n×mn\times mn×m的蛋糕,每次从蛋糕中间一个地方向四个方向中的一个方向切一刀【切的方向平行于其中一个蛋糕边缘】,问最后共几块蛋糕,没有切断的算一块

题解

  • 比赛的时候一眼看出了答案是交点个数+1+1+1,和队友说了一下队友同意了,然后我这个伪几何选手直接套了一个线段交点个数的板子,这个板子是直接从网上搬过来的,表面看上去复杂度是nlog⁡nn\log nnlogn,但是交上去TTT掉了,然后还是老老实实写线段树吧:(
  • 先考虑向上切的与其他横切的交点个数,从上至下遍历所有横切的,在线段树中区间加值,然后对于一个向上切,单点查询值就是交点个数了,向下切在从下至上遍历一次就行了

代码

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>using namespace std;
const int maxn=2e5+10;namespace IO{ #define BUF_SIZE 100000 #define OUT_SIZE 100000 bool IOerror=0; inline char nc(){ static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if (p1==pend){ p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if (pend==p1){IOerror=1;return -1;} } return *p1++; } inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} inline bool read(double &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror) return false; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (ch=='.'){ double tmp=1; ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0'); } if (sign)x=-x; return true;} template<typename T>inline bool read(T &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror) return false; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; return true;} inline bool read(char *s){ char ch=nc(); for (;blank(ch);ch=nc()); if (IOerror) return false; for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; *s=0; return true;}
};
using namespace IO;int sum[maxn<<2],mark[maxn<<2];void down(int id)
{mark[id<<1]+=mark[id];mark[id<<1|1]+=mark[id];mark[id]=0;
}void build(int id,int l,int r)
{mark[id]=0;if(l==r) return;int mid=(l+r)>>1;build(id<<1,l,mid);build(id<<1|1,mid+1,r);
}void update(int id,int L,int R,int l,int r,long long add)
{if(l<=L&&R<=r) {mark[id]+=add;return;}if(mark[id]!=0) down(id);int mid=(L+R)>>1;if(l<=mid) update(id<<1,L,mid,l,r,add);if(r>mid) update(id<<1|1,mid+1,R,l,r,add);
}int query(int id,int L,int R,int pos)
{if(L==R) return mark[id];int mid=(L+R)>>1;if(mark[id]) down(id);if(pos<=mid) return query(id<<1,L,mid,pos);else return query(id<<1|1,mid+1,R,pos);
}struct line{int x,y,dir;line(int a=0,int b=0,int d=0) {x=a;y=b;dir=d;}
}a[maxn];char s[10];
int b[maxn],c[maxn],w,h,n,m,k;
vector<int> vec[4][maxn];void clear_()
{for(int i=0;i<=3;i++) for(int j=0;j<=h;j++) vec[i][j].clear();
}int main()
{int t;read(t);while(t--) {read(n);read(m);read(k);for(int i=1,x,y;i<=k;i++) {read(a[i].x);read(a[i].y);read(s+1);b[i]=a[i].x;c[i]=a[i].y;if(s[1]=='L') a[i].dir=3;else if(s[1]=='R') a[i].dir=1;else if(s[1]=='U') a[i].dir=0;else a[i].dir=2;}sort(b+1,b+k+1);sort(c+1,c+k+1);w=unique(b+1,b+k+1)-b-1;h=unique(c+1,c+k+1)-c-1;for(int i=1;i<=k;i++) {a[i].x=lower_bound(b+1,b+k+1,a[i].x)-b;a[i].y=lower_bound(c+1,c+k+1,a[i].y)-c;vec[a[i].dir][a[i].y].push_back(a[i].x);}build(1,1,w);long long ans=0;for(int i=1;i<=h;i++) {for(auto j:vec[1][i]) update(1,1,w,j,w,1);for(auto j:vec[3][i]) update(1,1,w,1,j,1);for(auto j:vec[2][i]) ans+=query(1,1,w,j);}build(1,1,w);for(int i=h;i>=1;i--) {for(auto j:vec[1][i]) update(1,1,w,j,w,1);for(auto j:vec[3][i]) update(1,1,w,1,j,1);for(auto j:vec[0][i]) ans+=query(1,1,w,j);}printf("%lld\n",ans+1);clear_();}
}

「hdu6681」Rikka with Cake【线段树】相关推荐

  1. loj2537 「PKUWC2018」Minimax 【概率 + 线段树合并】

    题目链接 loj2537 题解 观察题目的式子似乎没有什么意义,我们考虑计算出每一种权值的概率 先离散化一下权值 显然可以设一个\(dp\),设\(f[i][j]\)表示\(i\)节点权值为\(j\) ...

  2. hdu - 6681 Rikka with Cake 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6681 题意:给定一个左下顶点为,右上顶点为的矩形,然后给你k条射线,每条射线的起点及方向(上下左右)都 ...

  3. 「JOISC 2020 Day4」治疗计划(线段树+dijkstra最短路)

    「JOISC 2020 Day4」治疗计划 description solution 设dpi:1−Ridp_i:1-R_idpi​:1−Ri​ 都能被救治成功的最小花费 两个治疗方案[Li,Ri], ...

  4. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  5. HDU 6089 Rikka with Terrorist (线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...

  6. hdu6681 Rikka with Cake(主席树)

    题目链接 大意:给你一个矩形区域,((0,0)−(n,m))((0,0)-(n,m))((0,0)−(n,m)),现在有k条直线,每条直线都是从一个点出发到上下左右四个方向之一. 问你这个区域被分成了 ...

  7. 「2017 山东一轮集训 Day2」Pair (霍尔定理+线段树)

    题目描述 给出一个长度为  的数列  和一个长度为  的数列 ,求  有多少个长度为  的连续子数列能与  匹配. 两个数列可以匹配,当且仅当存在一种方案,使两个数列中的数可以两两配对,两个数可以配对 ...

  8. 「 Luogu P2574 」 XOR的艺术——线段树

    # 解题思路 这题不难,但是原谅我一开始的傻逼想法,一会儿再给大家透露透露. 先说怎么做这题. 显然对于 $0$ 和 $1$ 来说,异或无非也就只有两种变化 异或了奇数次,$0$ 就会变成 $1$,$ ...

  9. LOJ 2339 「WC2018」通道——边分治+虚树

    题目:https://loj.ac/problem/2339 两棵树的话,可以用 CTSC2018 暴力写挂的方法,边分治+虚树.O(nlogn). 考虑怎么在这个方法上再加一棵树.发现很难弄. 看了 ...

最新文章

  1. 管理系统中的计算机应用信息可靠性,计算机信息处理系统的可靠性研究
  2. Python+selenium自动化测试:报错:TypeError: 'WebElement' object is not iterable
  3. php调用txt接口,PHP 如何更优雅地调用 API 接口
  4. tkinter笔记:scale 尺度 (莫烦python笔记)
  5. ab压力测试工具linux,【Linux】ApacheBench(ab)压力测试工具
  6. Ctrl+Alt+F1~F6
  7. inputstreamreader未关闭会导致oom_Linux内核OOM机制分析和防止进程被OOM杀死的方法...
  8. 常用机器学习算法汇总(中)
  9. AndroidManifest.xml文件的作用和简单使用
  10. 在测试中发现错误,不要着急去改,静下心来,想一想错误的关联性( 错误展开确认 )。
  11. BZOJ3678: wangxz与OJ
  12. RDD持久化(缓存)
  13. 单幅RGB图像+Depth深度图得到点云模型示例
  14. 马斯克震撼演讲:世界上最可怕的事情,是没有内驱力
  15. python pycharm 无法import win32api、win32con、win32com、win32gui 问题一次解决!方法合集
  16. 苹果CoreFoundation源代码
  17. 海外抖音推荐算法,玩转tiktok短视频内容运营
  18. spring cloud-熔断(六)
  19. java半角英数check_java - 关于全角半角介绍以及处理方式
  20. 怎么在一张图片中隐藏文件?

热门文章

  1. 搭建流媒体推流/拉流服务(RTMP/RTSP/HLS/HTTP-FLV)
  2. 如何挑选性价比较高的固态硬盘SSD?
  3. 蓝牙Mesh物联系统开发一 项目简介
  4. 通信电源更新改造新技术在淄博联通投入应用
  5. java integer 相加_Java-整数相加求和
  6. 毕业设计——基于STM32单片机的绿植养护系统(物联网、智能家居、手机APP控制、自动监测土壤湿度)
  7. 电源电路中电感为什么会啸叫?
  8. 05-shell命令解释器基本操作--不会你能说自己是程序员?
  9. Spring与Web环境集成
  10. 一张图介绍PRS的计算步骤