#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MAX_N 100000
int W, H, N;
struct LINE{
LINE (int idx, int x, int y1, int y2) : idx(idx), x(x), y1(y1), y2(y2) {}
int idx, x;
int y1, y2;
bool operator <(const LINE &a)const{
return x<a.x;
}};
vector<LINE> line;
struct POINT{
POINT (int idx, int x, int y, int z) : idx(idx), x(x), y(y), z(z) {}
int idx, x, y, z;
bool operator <(const POINT &a)const{
if(x==a.x){
return z<a.z;
}
return x<a.x;
}};
vector<POINT> p;
struct SEG{
SEG (int l, int r, int sum) : l(l), r(r), sum(sum) {}
int l, r;
int sum;
};
vector<SEG> seg1;
ll ans = 0;
int sum11(int idx, int s, int e, int x, int y){
if(idx==-1) return 0;
if(x<=s && e<=y) return seg1[idx].sum;
if(x>e || y<s) return 0;
return sum11(seg1[idx].l, s, (s+e)/2, x, y) + sum11(seg1[idx].r, (s+e)/2+1, e, x, y);
}
void update11(int idx, int s, int e, int x, int y){
seg1[idx].sum+=y;
if(s==e) return;
if(x<=(s+e)/2){
if(seg1[idx].l==-1){
seg1[idx].l = seg1.size(); seg1.push_back({-1, -1, 0});
}
update11(seg1[idx].l, s, (s+e)/2, x, y);
}else{
if(seg1[idx].r==-1){
seg1[idx].r = seg1.size(); seg1.push_back({-1, -1, 0});
}
update11(seg1[idx].r, (s+e)/2+1, e, x, y);
}
}
struct SEG2{
SEG2(int l, int r, int group, int sum, int lazy) : l(l), r(r), sum(sum), lazy(lazy) {}
int l, r;
int group, sum, lazy;
};
vector<SEG2> seg2;
int group[MAX_N+50];
int g[MAX_N+50];
int find_g(int x){
if(x==group[x]) return x;
return group[x] = find_g(group[x]);
}
void union_g(int x, int y){
x = find_g(x); y = find_g(y);
group[x] = y;
}
void calc(int idx){
if(idx==-1) return;
if(seg2[idx].sum==0){
seg2[idx].lazy = seg2[idx].group = -1;
return;
}
if(seg2[idx].lazy!=-1){
if(seg2[idx].l!=-1){
if(seg2[seg2[idx].l].sum==0){
seg2[seg2[idx].l].lazy = seg2[seg2[idx].l].group = -1;
}else if(seg2[seg2[idx].l].group==-1){
seg2[seg2[idx].l].group = seg2[idx].lazy;
seg2[seg2[idx].l].lazy = seg2[idx].lazy;
}else{
union_g(seg2[seg2[idx].l].group, seg2[idx].lazy);
seg2[seg2[idx].l].group = seg2[idx].lazy;
seg2[seg2[idx].l].lazy = seg2[idx].lazy;
}
}if(seg2[idx].r!=-1){
if(seg2[seg2[idx].r].sum==0){
seg2[seg2[idx].r].lazy = seg2[seg2[idx].r].group = -1;
}else if(seg2[seg2[idx].r].group==-1){
seg2[seg2[idx].r].group = seg2[idx].lazy;
seg2[seg2[idx].r].lazy = seg2[idx].lazy;
}else{
union_g(seg2[seg2[idx].r].group, seg2[idx].lazy);
seg2[seg2[idx].r].group = seg2[idx].lazy;
seg2[seg2[idx].r].lazy = seg2[idx].lazy;
}
}
seg2[idx].lazy = -1;
}
}
void lazy22(int idx, int s, int e, int x, int y, int z){
if(idx==-1) return;
if(seg2[idx].sum==0) return;
calc(idx);
if(x<=s && e<=y) {
seg2[idx].lazy = z;
if(seg2[idx].group!=-1) union_g(seg2[idx].group, z);
seg2[idx].group = z;
return;
}
if(x>e || y<s) return;
lazy22(seg2[idx].l, s, (s+e)/2, x, y, z);
lazy22(seg2[idx].r, (s+e)/2+1, e, x, y, z);
}
void update22(int idx, int s, int e, int x, int y){
calc(idx);
seg2[idx].sum++;
seg2[idx].group = -1;
if(s==e){
seg2[idx].group = y;
return;
}
if(x<=(s+e)/2){
if(seg2[idx].l==-1){
seg2[idx].l = seg2.size(); seg2.push_back({-1, -1, -1, 0, -1});
}
update22(seg2[idx].l, s, (s+e)/2, x, y);
}else{
if(seg2[idx].r==-1){
seg2[idx].r = seg2.size(); seg2.push_back({-1, -1, -1, 0, -1});
}
update22(seg2[idx].r, (s+e)/2+1, e, x, y);
}
}
int find22(int idx, int s, int e, int k){
calc(idx);
seg2[idx].sum--;
if(s==e){
int ret = seg2[idx].group;
return seg2[idx].group;
}
if(k<=(s+e)/2){
return find22(seg2[idx].l, s, (s+e)/2, k);
}else{
return find22(seg2[idx].r, (s+e)/2+1, e, k);
}
}
int sum1(int x, int y) {return sum11(0, 0, H, x, y);}
void update1(int x, int y) {update11(0, 0, H, x, y);}
void lazy2(int x, int y, int z) {lazy22(0, 0, H, x, y, z);}
void update2(int x, int y) {update22(0, 0, H, x, y);}
int find2(int x) {return find22(0, 0, H, x);}
int main(){
scanf("%d%d%d", &W, &H, &N);
for(int i=0; i<N+4; i++) group[i] = i;
seg1.push_back({-1, -1, 0});
seg2.push_back({-1, -1, -1, 0, -1});
for(int i=0; i<N; i++){
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if(a==c){
line.push_back({i, a, b, d});
}else{
p.push_back({i, a, b, 1}); p.push_back({i, c+1, b, -1});
}
}
ans-=(N+4);
p.push_back({N, 0, 0, 1}); p.push_back({N+1, 0, H, 1});
p.push_back({N, W+1, 0, -1}); p.push_back({N+1, W+1, H, -1});
line.push_back({N+2, 0, 0, H}); line.push_back({N+3, W, 0, H});
sort(p.begin(), p.end()); sort(line.begin(), line.end());
int idx = 0;
for(int i=0; i<line.size(); i++){
while(idx<p.size() && p[idx].x<=line[i].x){
POINT now = p[idx]; idx++;
//cout<<'*'<<now.x<<' '<<now.y<<' '<<now.z<<endl;
update1(now.y, now.z);
if(now.z==1) update2(now.y, now.idx);
else {
g[now.idx] = find2(now.y);
//cout<<now.idx<<' '<<g[now.idx]<<endl;
}
}
int tmp = sum1(line[i].y1, line[i].y2);
lazy2(line[i].y1, line[i].y2, line[i].idx);
//cout<<'&'<<line[i].x<<' '<<line[i].y1<<' '<<line[i].y2<<' '<<tmp<<endl;
ans+=(ll)tmp;
}
while(idx<p.size()){
POINT now = p[idx]; idx++;
//cout<<'*'<<now.x<<' '<<now.y<<' '<<now.z<<endl;
update1(now.y, now.z);
if(now.z==1) update2(now.y, now.idx);
else {
g[now.idx] = find2(now.y);
//cout<<now.idx<<' '<<g[now.idx]<<endl;
}
}
//cout<<ans;
for(int i=0; i<N+4; i++){
//cout<<find_g(i)<<endl;
if(find_g(i)==i){
ans++;
}
}
printf("%lld", ans);
}
Compilation message
2014_ho_t5.cpp: In function 'int find22(int, int, int, int)':
2014_ho_t5.cpp:160:7: warning: unused variable 'ret' [-Wunused-variable]
int ret = seg2[idx].group;
^~~
2014_ho_t5.cpp: In function 'int main()':
2014_ho_t5.cpp:196:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<line.size(); i++){
~^~~~~~~~~~~~
2014_ho_t5.cpp:197:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(idx<p.size() && p[idx].x<=line[i].x){
~~~^~~~~~~~~
2014_ho_t5.cpp:212:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(idx<p.size()){
~~~^~~~~~~~~
2014_ho_t5.cpp:177:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &W, &H, &N);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:183:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
4 ms |
760 KB |
Output is correct |
25 |
Correct |
5 ms |
1032 KB |
Output is correct |
26 |
Correct |
4 ms |
1016 KB |
Output is correct |
27 |
Correct |
5 ms |
1016 KB |
Output is correct |
28 |
Correct |
5 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
13 ms |
1144 KB |
Output is correct |
5 |
Correct |
63 ms |
4668 KB |
Output is correct |
6 |
Correct |
142 ms |
18112 KB |
Output is correct |
7 |
Correct |
303 ms |
36596 KB |
Output is correct |
8 |
Correct |
301 ms |
36332 KB |
Output is correct |
9 |
Correct |
276 ms |
36400 KB |
Output is correct |
10 |
Correct |
239 ms |
36400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
760 KB |
Output is correct |
3 |
Correct |
31 ms |
4668 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
1032 KB |
Output is correct |
6 |
Correct |
576 ms |
34264 KB |
Output is correct |
7 |
Correct |
20 ms |
4532 KB |
Output is correct |
8 |
Correct |
211 ms |
33708 KB |
Output is correct |
9 |
Correct |
304 ms |
33824 KB |
Output is correct |
10 |
Correct |
308 ms |
33808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
4 ms |
760 KB |
Output is correct |
25 |
Correct |
5 ms |
1032 KB |
Output is correct |
26 |
Correct |
4 ms |
1016 KB |
Output is correct |
27 |
Correct |
5 ms |
1016 KB |
Output is correct |
28 |
Correct |
5 ms |
1016 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
13 ms |
1144 KB |
Output is correct |
33 |
Correct |
63 ms |
4668 KB |
Output is correct |
34 |
Correct |
142 ms |
18112 KB |
Output is correct |
35 |
Correct |
303 ms |
36596 KB |
Output is correct |
36 |
Correct |
301 ms |
36332 KB |
Output is correct |
37 |
Correct |
276 ms |
36400 KB |
Output is correct |
38 |
Correct |
239 ms |
36400 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
4 ms |
760 KB |
Output is correct |
41 |
Correct |
31 ms |
4668 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
5 ms |
1032 KB |
Output is correct |
44 |
Correct |
576 ms |
34264 KB |
Output is correct |
45 |
Correct |
20 ms |
4532 KB |
Output is correct |
46 |
Correct |
211 ms |
33708 KB |
Output is correct |
47 |
Correct |
304 ms |
33824 KB |
Output is correct |
48 |
Correct |
308 ms |
33808 KB |
Output is correct |
49 |
Correct |
4 ms |
504 KB |
Output is correct |
50 |
Correct |
3 ms |
376 KB |
Output is correct |
51 |
Correct |
6 ms |
1620 KB |
Output is correct |
52 |
Correct |
216 ms |
18340 KB |
Output is correct |
53 |
Correct |
134 ms |
18108 KB |
Output is correct |
54 |
Correct |
212 ms |
17984 KB |
Output is correct |
55 |
Correct |
507 ms |
36560 KB |
Output is correct |
56 |
Correct |
310 ms |
36272 KB |
Output is correct |
57 |
Correct |
521 ms |
36184 KB |
Output is correct |
58 |
Correct |
628 ms |
36264 KB |
Output is correct |
59 |
Correct |
336 ms |
35240 KB |
Output is correct |
60 |
Correct |
338 ms |
35172 KB |
Output is correct |
61 |
Correct |
377 ms |
35376 KB |
Output is correct |
62 |
Correct |
361 ms |
35388 KB |
Output is correct |
63 |
Correct |
505 ms |
36480 KB |
Output is correct |
64 |
Correct |
261 ms |
36420 KB |
Output is correct |
65 |
Correct |
499 ms |
35968 KB |
Output is correct |
66 |
Correct |
498 ms |
36416 KB |
Output is correct |
67 |
Correct |
387 ms |
36372 KB |
Output is correct |
68 |
Correct |
478 ms |
36548 KB |
Output is correct |
69 |
Correct |
414 ms |
36500 KB |
Output is correct |
70 |
Correct |
452 ms |
36336 KB |
Output is correct |
71 |
Correct |
126 ms |
5220 KB |
Output is correct |
72 |
Correct |
612 ms |
64748 KB |
Output is correct |