题目地址http://acm.pku.edu.cn/JudgeOnline/problem?id=3277
首先离散化,然后使用线段树。
提示:题目中说N<=40000,然后RE了几次,看讨论里面说开到70000才可以。
代码
1 #include <iostream>
2 #include <algorithm>
3 #include <cstdio>
4 #include <cstring>
5 using namespace std;
6 const int MAXN = 70000+10;
7 struct rect
8 {
9 int x,y,h;
10 };
11 rect a[MAXN];
12 int hash[MAXN*2];
13 int n;
14 int index;
15 struct node
16 {
17 int l;
18 int r;
19 int mid;
20 int h;
21 };
22 node seg_tree[MAXN*4];
23 void make(int l,int r,int num)
24 {
25 seg_tree[num].l = l;
26 seg_tree[num].r = r;
27 seg_tree[num].mid = (l+r)/2;
28 seg_tree[num].h = 0;
29 if(l+1!=r)
30 {
31 make(l,seg_tree[num].mid,2*num);
32 make(seg_tree[num].mid,r,2*num+1);
33 }
34 }
35 void insert(int l,int r,int num,int h)
36 {
37 if(seg_tree[num].l==l&&seg_tree[num].r==r)
38 {
39 if(h>seg_tree[num].h||!seg_tree[num].h)
40 {
41 seg_tree[num].h = h;
42 }
43 return;
44 }
45 if(r<=seg_tree[num].mid)
46 {
47 insert(l,r,2*num,h);
48 }
49 else if(l>=seg_tree[num].mid)
50 {
51 insert(l,r,2*num+1,h);
52 }
53 else
54 {
55 insert(l,seg_tree[num].mid,2*num,h);
56 insert(seg_tree[num].mid,r,2*num+1,h);
57 }
58 }
59 long long cal(int num,int h)
60 {
61 if(h>seg_tree[num].h)
62 {
63 seg_tree[num].h = h;
64 }
65 if(seg_tree[num].l+1==seg_tree[num].r)
66 {
67 return ((long long)seg_tree[num].h) *
68 (hash[seg_tree[num].r]-hash[seg_tree[num].l]);
69 }
70 return cal(2*num,seg_tree[num].h)+cal(2*num+1,seg_tree[num].h);
71 }
72 int b_search(int val)
73 {
74 int i = 1;
75 int j=index-1;
76 while(i<=j)
77 {
78 int mid = (i+j)>>1;
79 if(val==hash[mid])
80 {
81 return mid;
82 }
83 else if(hash[mid]>val)j=mid-1;
84 else i = mid + 1;
85 }
86 return -1;
87 }
88 int main()
89 {
90 scanf("%d",&n);
91 for(int i=1;i<=n;i++)
92 {
93 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].h);
94 hash[2*i-1] = a[i].x;
95 hash[2*i] = a[i].y;
96 }
97 sort(hash+1,hash+2*n+1);
98 index = 2;
99 for(int i=1;i<2*n;i++)
100 {
101 if(hash[i]!=hash[i+1])
102 {
103 hash[index++] = hash[i+1];
104 }
105 }
106 make(1,index-1,1);
107 for(int i=1;i<=n;i++)
108 {
109 insert(b_search(a[i].x),b_search(a[i].y),1,a[i].h);
110 }
111 long long ans = cal(1,0);
112 printf("%lld\n",ans);
113 return 0;
114 }
115
2 #include <algorithm>
3 #include <cstdio>
4 #include <cstring>
5 using namespace std;
6 const int MAXN = 70000+10;
7 struct rect
8 {
9 int x,y,h;
10 };
11 rect a[MAXN];
12 int hash[MAXN*2];
13 int n;
14 int index;
15 struct node
16 {
17 int l;
18 int r;
19 int mid;
20 int h;
21 };
22 node seg_tree[MAXN*4];
23 void make(int l,int r,int num)
24 {
25 seg_tree[num].l = l;
26 seg_tree[num].r = r;
27 seg_tree[num].mid = (l+r)/2;
28 seg_tree[num].h = 0;
29 if(l+1!=r)
30 {
31 make(l,seg_tree[num].mid,2*num);
32 make(seg_tree[num].mid,r,2*num+1);
33 }
34 }
35 void insert(int l,int r,int num,int h)
36 {
37 if(seg_tree[num].l==l&&seg_tree[num].r==r)
38 {
39 if(h>seg_tree[num].h||!seg_tree[num].h)
40 {
41 seg_tree[num].h = h;
42 }
43 return;
44 }
45 if(r<=seg_tree[num].mid)
46 {
47 insert(l,r,2*num,h);
48 }
49 else if(l>=seg_tree[num].mid)
50 {
51 insert(l,r,2*num+1,h);
52 }
53 else
54 {
55 insert(l,seg_tree[num].mid,2*num,h);
56 insert(seg_tree[num].mid,r,2*num+1,h);
57 }
58 }
59 long long cal(int num,int h)
60 {
61 if(h>seg_tree[num].h)
62 {
63 seg_tree[num].h = h;
64 }
65 if(seg_tree[num].l+1==seg_tree[num].r)
66 {
67 return ((long long)seg_tree[num].h) *
68 (hash[seg_tree[num].r]-hash[seg_tree[num].l]);
69 }
70 return cal(2*num,seg_tree[num].h)+cal(2*num+1,seg_tree[num].h);
71 }
72 int b_search(int val)
73 {
74 int i = 1;
75 int j=index-1;
76 while(i<=j)
77 {
78 int mid = (i+j)>>1;
79 if(val==hash[mid])
80 {
81 return mid;
82 }
83 else if(hash[mid]>val)j=mid-1;
84 else i = mid + 1;
85 }
86 return -1;
87 }
88 int main()
89 {
90 scanf("%d",&n);
91 for(int i=1;i<=n;i++)
92 {
93 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].h);
94 hash[2*i-1] = a[i].x;
95 hash[2*i] = a[i].y;
96 }
97 sort(hash+1,hash+2*n+1);
98 index = 2;
99 for(int i=1;i<2*n;i++)
100 {
101 if(hash[i]!=hash[i+1])
102 {
103 hash[index++] = hash[i+1];
104 }
105 }
106 make(1,index-1,1);
107 for(int i=1;i<=n;i++)
108 {
109 insert(b_search(a[i].x),b_search(a[i].y),1,a[i].h);
110 }
111 long long ans = cal(1,0);
112 printf("%lld\n",ans);
113 return 0;
114 }
115