hdu6681 Rikka with Cake

题目大意:

给出$k$条射线 求能使$n*m$的矩形分成多少个块

题解:

由于是射线 所以所以每个一个交点都能产生贡献 最后的答案就是总交点数+1

那如何去维护呢 我们可以把$k$条射线按横纵来考虑 对于竖的射线 假如为$U$ 那么$[y,m]$便被覆盖 假如为$D$ 那么$[0,y]$便被覆盖 对于这样的线段可以考虑用树状数组去维护 我们考虑一个扫描线从下到上的扫过去 假如当前扫到了$y$ 那么便执行操作$add(x,val)$ 对于一个向下的射线 我们可以当扫描线在$0$时$add(x,val)$ 当扫描线在$y+1$时 $add(x,-val)$ 其中$val=1$ 假如我们扫描线当前遇到了横线的话 就直接查询从$L-R$覆盖的线段即可 由于$x$范围很大但是$k$很小 所以我们可以对$x$坐标离散化

AC代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=200050;
int T;
int n,m,k;
int c[maxn],h[maxn],num,tot;
struct node{
	int p,l,r,pos,val;
	bool operator<(const node&t) const{
		if(pos==t.pos) return p>t.p; return pos<t.pos;
	}
}a[maxn];
inline int read()
{
	int x=0,t=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')t=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*t;
}
int ha(int x){return lower_bound(h+1,h+tot+1,x)-h;}
int lowbit(int x){return x&-x;}
void add(int x,int p)
{
	while(x<=tot)
	{
		c[x]+=p;
		x+=lowbit(x);
	}
}
int query(int x)
{
	int res=0;
	while(x)
	{
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
int main()
{
	T=read();
	while(T--)
	{
		memset(c,0,sizeof(c));
		num=0,tot=0;
		n=read(),m=read(),k=read();
		for(int i=1;i<=k;i++)
		{
			int x,y;x=read(),y=read();
			char opt[2];cin>>opt;
			if(opt[0]=='U')
			{
				a[++num]={1,x,y,y,1};
				h[++tot]=x;
			}
			else if(opt[0]=='D')
			{
				a[++num]={1,x,0,0,1};
				a[++num]={1,x,y+1,y+1,-1};
				h[++tot]=x;
			}
			else if(opt[0]=='L')
			{
				a[++num]={0,0,x,y,0};
				h[++tot]=x;
			}
			else
			{
				a[++num]={0,x,n,y,0};
				h[++tot]=x;
			}
			//cout<<x<<" "<<y<<endl;
		}
		h[++tot]=0;h[++tot]=n;
		sort(h+1,h+tot+1);
		tot=unique(h+1,h+tot+1)-(h+1);
		sort(a+1,a+num+1);
		int ans=0;
		for(int i=1;i<=num;i++)
		{
			if(a[i].p==1)
			{
				add(ha(a[i].l),a[i].val);
			}
			else ans+=query(ha(a[i].r))-query(ha(a[i].l)-1);
		}
		cout<<ans+1<<endl;
	}
	return 0;
}