洛谷P2070 刷墙 离散化

洛谷P2070 刷墙 离散化
题意 区间覆盖 求覆盖了2次以上的线段有多少长

离散化一下,一条线段两个点,2n个点排序一下就行了

 

 1 #include <bits/stdc++.h>
 2 #define For(i, j, k) for(int i=j; i<=k; i++)
 3 #define Dow(i, j, k) for(int i=j; i>=k; i--)
 4 #define LL long long
 5 using namespace std;
 6 inline int read() {
 7     int x = 0, f = 1;
 8     char ch = getchar();
 9     while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
10     while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
11     return x * f;
12 }
13 
14 const int N = 100011; 
15 struct node{
16     int val, pos; 
17 }point[2*N];
18 int n,sum,L,R,num,ans; 
19 
20 inline bool cmp_pos(node a, node b) {
21     return a.pos < b.pos; 
22 }
23 
24 int main() {
25     n = read(); 
26     int now = 0, y;
27     For(i, 1, n) {
28         char ch[10]; 
29         int len = read(); scanf("%s",ch+1); 
30         int x = now; 
31         if(ch[1]=='L') y = x-len; else y = x+len; 
32         now = y; 
33         if(x>y) swap(x, y); 
34         point[++num].pos = x; point[num].val = 1; 
35         point[++num].pos = y; point[num].val = -1; 
36     }
37     sort(point+1, point+num+1, cmp_pos); 
38 
39     For(i, 1, num) {
40         int old = sum; 
41         sum += point[i].val; 
42         if(old==1 && sum==2) L = point[i].pos; 
43         if(old==2 && sum==1) ans +=point[i].pos-L; 
44     }
45     printf("%d\n", ans); 
46     return 0; 
47 }

 

转载于:https://www.cnblogs.com/third2333/p/8443228.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值