1. 程式人生 > >洛谷P2070 刷墻 離散化

洛谷P2070 刷墻 離散化

() c++ nbsp lin 題意 nod ons main val

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

洛谷P2070 刷墻 離散化