洛谷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 }

 

posted @ 2018-02-11 21:14  third2333  阅读(195)  评论(0编辑  收藏  举报