HDU 6681 Rikka with Cake(扫描线、动态开点线段树)

http://acm.hdu.edu.cn/showproblem.php?pid=6681

题意

在矩形区域内有k条射线,问这些射线将矩形分成了多少区域

题解

容易发现答案为所有射线交点个数。

按y从排序扫描矩形区域,动态开点线段树维护区间内竖线的个数,由于n,m范围较大,需要离散化处理,但这样比较麻烦且此题空间足够所以建议用动态开点。

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #include <bits/stdc++.h>
 4 #define iter ::iterator
 5 #define pa pair<int,ll>
 6 using namespace  std;
 7 #define ll long long
 8 #define mk make_pair
 9 #define pb push_back
10 #define se second
11 #define fi first
12 ll mod=998244353;
13 const int N=1e5+5;
14 int T;
15 struct node{
16     int x1,x2,y,z;
17     bool operator <(const node &t)const{
18         if(y==t.y)return z<t.z;
19         return y<t.y;
20     }
21 }a[N];
22 int cnt;
23 int sum[N*32],ls[N*32],rs[N*32];
24 void up(int &o,int l,int r,int p,int v){
25     if(!o)o=++cnt;
26     sum[o]+=v;
27     if(l==r)return;
28     int m=(l+r)/2;
29     if(p<=m)up(ls[o],l,m,p,v);
30     else up(rs[o],m+1,r,p,v);
31 }
32 int qu(int o,int l,int r,int ql,int qr){
33     if(l>=ql&&r<=qr){
34         return sum[o];
35     }
36     int res=0;
37     int m=(l+r)/2;
38     if(ql<=m)res+=qu(ls[o],l,m,ql,qr);
39     if(qr>m)res+=qu(rs[o],m+1,r,ql,qr);
40     return res;
41 }
42
43 int main(){
44     scanf("%d",&T);
45     while(T--){
46         int n,m,k;
47         scanf("%d%d%d",&n,&m,&k);
48         int rt=0;
49         cnt=0;
50         for(int i=1;i<=k;i++){
51             int x,y;
52             char s[10];
53             scanf("%d%d%s",&x,&y,s);
54             char op=s[0];
55             a[i].x1=a[i].x2=x;
56             a[i].y=y;
57             if(op==‘U‘)a[i].z=1;
58             else if(op==‘L‘){
59                 a[i].z=2;
60                 a[i].x1=1;
61             }
62             else if(op==‘R‘){
63                 a[i].z=2;
64                 a[i].x2=n;
65             }
66             else{
67                 a[i].z=3;
68                 up(rt,1,n,a[i].x1,1);
69             }
70         }
71         sort(a+1,a+1+k);
72         ll ans=1;
73         for(int i=1;i<=k;i++){
74             if(a[i].z==1)up(rt,1,n,a[i].x1,1);
75             else if(a[i].z==2){
76                 ans+=qu(rt,1,n,a[i].x1,a[i].x2);
77             }
78             else up(rt,1,n,a[i].x1,-1);
79         }
80         printf("%lld\n",ans);
81         for(int i=1;i<=cnt;i++)
82             sum[i]=ls[i]=rs[i]=0;
83     }
84 }

原文地址:https://www.cnblogs.com/ccsu-kid/p/11379483.html

时间: 2024-08-29 01:02:44

HDU 6681 Rikka with Cake(扫描线、动态开点线段树)的相关文章

HDU 6183 Color it(动态开点线段树)

题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1677    Accepted Submission(s): 500 Problem Description 你喜欢画画吗? Little D不喜欢画画,特别

【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化

4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 247  Solved: 113[Submit][Status][Discuss] Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k小的数改为k,他想知 道N次操作后数列中所有元素的和.他还要玩其他游戏,所以这个问题留给你解决. Input 第一

Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 else 区间覆盖 push_up的时候要注意好多细节,, 数组尽量往大开 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const in

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

【bzoj4999】This Problem Is Too Simple! 树链剖分+动态开点线段树

题目描述 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x<2^31) 表示询问i节点到j节点的路径上有多少个值为x的节点. 输入 第一行有两个整数N,Q(1 ≤N≤ 100,000:1 ≤Q≤ 200,000),分别表示节点个数和操作个数. 下面一行N个整数,表示初始时每个节点的初始值. 接下来N-1行,每行两个整数x,y,表示x节点与y节点之间有边直接相连(描述一颗树).

CF1045G AI robots(动态开点线段树)

题意 火星上有$N$个机器人排成一行,第$i$个机器人的位置为$x_{i}$,视野为$r_{i}$,智商为$q_{i}$.我们认为第$i$个机器人可以看到的位置是$[x_{i}-r_{i},x_{i}+r_{i}]$.如果一对机器人相互可以看到,且它们的智商$q_{i}$的差距不大于$K$,那么它们会开始聊天. 为了防止它们吵起来,请计算有多少对机器人可能会聊天. 题解 先膜一下大佬->这里 我们先按视野降序排序,这样一个一个考虑,如果后面的能看到前面,那前面的也肯定能看到后面 这样,就是对于每

CF915E Physical Education Lessons|动态开点线段树

动态开点线段树 题目暗示了区间修改,所以我们自然想到了用线段树来维护非工作日的天数. 然而我们再看一下数据范围,天数n的范围是\(1 \le n \le 10^9\),像普通线段树一样预处理显然会爆空间. 天无绝人之路,我们看一下修改个数,$1\le q \le 3 \cdot 10^5 $, 比天数少很多,这也意味着,我们预先处理好的线段树有些节点并没有用 能否优化呢?答案是肯定的,这就是动态开点线段树,顾名思义,我们只要到用某个节点的时候,才分配一个点给它,这样使得我们使用的空间大大减少.其

动态开点线段树

用途 需要建立多棵独立的线段树 线段树维护的值域较大(1e9),但是操作次数较少(1e5) 特征 类似主席树的原理,动态分配每个树节点的位置(lson[],rson[]),每次只更新一条链,但是主席树是建立一颗新的树,动态开点线段树是在一棵树上不断添加节点(还是一棵树) 类似线段树的原理,push_down区间修改,push_up区间查询 例题 1.维护值域较大,线段树区间修改 cf915e https://codeforces.com/contest/915/problem/E 题意: q(3

Physical Education Lessons CodeForces - 915E (动态开点线段树)

Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical