## Submission #107340

# Submission time Handle Problem Language Result Execution time Memory
107340 2019-04-23T11:25:41 Z popovicirobert Port Facility (JOI17_port_facility) C++14
100 / 100
5157 ms 209040 KB
```#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const int MAXN = (int) 1e6;

inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}

struct SegTree {

int aint[8 * MAXN + 1];
int n;

inline void init(int _n) {
n = _n;
for(int i = 1; i <= 4 * n; i++) {
aint[i] = -INF;
}
}

void update(int nod, int left, int right, int pos, int cur) {
if(left == right) {
aint[nod] = cur;
}
else {
int mid = (left + right) / 2;

if(pos <= mid) update(2 * nod, left, mid, pos, cur);
else update(2 * nod + 1, mid + 1, right, pos, cur);

aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}
}

int query(int nod, int left, int right, int l, int r) {
if(l <= left && right <= r) {
return aint[nod];
}
else {
int mid = (left + right) / 2;
int ans = -INF;

if(l <= mid) ans = max(ans, query(2 * nod, left, mid, l, r));

if(mid < r) ans = max(ans, query(2 * nod + 1, mid + 1, right, l, r));

return ans;
}
}

void update(int pos, int cur) {
update(1, 1, n, pos, cur);
}

int query(int l, int r) {
return query(1, 1, n, l, r);
}

};

SegTree stl, str;

pair <int, int> arr[MAXN + 1];
int who[2 * MAXN + 1];
bool vis[MAXN + 1], col[MAXN + 1];
int n;

void dfs(int nod, int c) {

col[nod] = c;
vis[nod] = 1;

while(1) {

int cur = str.query(arr[nod].first, arr[nod].second);

if(cur <= arr[nod].second) {
break;
}

int id = who[cur];

str.update(arr[id].first, -INF);

if(vis[id] == 0) {
dfs(id, c ^ 1);
}

}

while(1) {

int cur = -stl.query(arr[nod].first, arr[nod].second);

if(cur >= arr[nod].first) {
break;
}

int id = who[cur];

stl.update(arr[id].second, -INF);

if(vis[id] == 0) {
dfs(id, c ^ 1);
}

}

}

struct Fenwick {

int aib[2 * MAXN + 1];
int n;

inline void init(int _n) {
n = _n;
for(int i = 1; i <= n; i++) {
aib[i] = 0;
}
}

inline void update(int pos) {
for(int i = pos; i <= n; i += lsb(i)) {
aib[i]++;
}
}

inline int query(int pos) {
int ans = 0;
for(int i = pos; i > 0; i -= lsb(i)) {
ans += aib[i];
}
return ans;
}

inline int sum(int l, int r) {
return query(r) - query(l - 1);
}

};

const int MAXBUF = (1 << 17);

char buf[MAXBUF];
int pbuf = MAXBUF;

inline char nextch() {
if(pbuf == MAXBUF) {
pbuf = 0;
}
return buf[pbuf++];
}

inline int getnr() {
char ch = nextch();
while(!isdigit(ch)) {
ch = nextch();
}
int nr = 0;
while(isdigit(ch)) {
nr = nr * 10 + ch - '0';
ch = nextch();
}
return nr;
}

int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i;
//ios::sync_with_stdio(false);
//cin.tie(0), cout.tie(0);

n = getnr();

stl.init(2 * n), str.init(2 * n);

for(i = 1; i <= n; i++) {
arr[i].first = getnr();
arr[i].second = getnr();

str.update(arr[i].first, arr[i].second);
stl.update(arr[i].second, -arr[i].first);

who[arr[i].first] = who[arr[i].second] = i;
}

int ans = 1;

for(i = 1; i <= n; i++) {
if(vis[i] == 0) {
dfs(i, 0);
ans *= 2;
mod(ans);
}
}

Fenwick fen[2];

for(i = 0; i < 2; i++) {
fen[i].init(2 * n);
}

vector <int> ord(n + 1);
iota(ord.begin(), ord.end(), 0);

sort(ord.begin(), ord.end(), [&](const int &a, const int &b) {

return arr[a].first < arr[b].first;

});

for(i = 1; i <= n; i++) {

int id = ord[i];

if(fen[col[id]].sum(arr[id].first, arr[id].second) > 0) {
ans = 0;
break;
}

fen[col[id]].update(arr[id].second);

}

cout << ans;

//cin.close();
//cout.close();
return 0;
}
```

### Compilation message

```port_facility.cpp: In function 'char nextch()':
port_facility.cpp:162:14: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
~~~~~^~~~~~~~~~~~~~~~~~~~~~~```

#### Subtask #1 10.0 / 10.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 412 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct

#### Subtask #2 12.0 / 12.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 412 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 5 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 5 ms 640 KB Output is correct
31 Correct 5 ms 768 KB Output is correct
32 Correct 4 ms 640 KB Output is correct
33 Correct 4 ms 640 KB Output is correct
34 Correct 5 ms 768 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 4 ms 640 KB Output is correct
37 Correct 5 ms 768 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 5 ms 768 KB Output is correct
43 Correct 4 ms 768 KB Output is correct
44 Correct 4 ms 768 KB Output is correct

#### Subtask #3 56.0 / 56.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 412 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 5 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 5 ms 640 KB Output is correct
31 Correct 5 ms 768 KB Output is correct
32 Correct 4 ms 640 KB Output is correct
33 Correct 4 ms 640 KB Output is correct
34 Correct 5 ms 768 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 4 ms 640 KB Output is correct
37 Correct 5 ms 768 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 5 ms 768 KB Output is correct
43 Correct 4 ms 768 KB Output is correct
44 Correct 4 ms 768 KB Output is correct
45 Correct 239 ms 11848 KB Output is correct
46 Correct 228 ms 13252 KB Output is correct
47 Correct 198 ms 11000 KB Output is correct
48 Correct 208 ms 13048 KB Output is correct
49 Correct 230 ms 11256 KB Output is correct
50 Correct 190 ms 12792 KB Output is correct
51 Correct 236 ms 12816 KB Output is correct
52 Correct 142 ms 10460 KB Output is correct
53 Correct 197 ms 10460 KB Output is correct
54 Correct 244 ms 19960 KB Output is correct
55 Correct 246 ms 15396 KB Output is correct
56 Correct 237 ms 15228 KB Output is correct
57 Correct 205 ms 10616 KB Output is correct
58 Correct 148 ms 10592 KB Output is correct
59 Correct 176 ms 10524 KB Output is correct
60 Correct 212 ms 10616 KB Output is correct
61 Correct 206 ms 10540 KB Output is correct
62 Correct 201 ms 10488 KB Output is correct
63 Correct 215 ms 10616 KB Output is correct
64 Correct 222 ms 10616 KB Output is correct
65 Correct 334 ms 19960 KB Output is correct
66 Correct 346 ms 19960 KB Output is correct
67 Correct 210 ms 15224 KB Output is correct
68 Correct 224 ms 15224 KB Output is correct
69 Correct 220 ms 14712 KB Output is correct
70 Correct 246 ms 14748 KB Output is correct
71 Correct 223 ms 19932 KB Output is correct
72 Correct 217 ms 20004 KB Output is correct
73 Correct 235 ms 16760 KB Output is correct
74 Correct 248 ms 16760 KB Output is correct
75 Correct 197 ms 16164 KB Output is correct
76 Correct 206 ms 19960 KB Output is correct
77 Correct 219 ms 20048 KB Output is correct
78 Correct 210 ms 12792 KB Output is correct
79 Correct 222 ms 11896 KB Output is correct

#### Subtask #4 22.0 / 22.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 412 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 5 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 5 ms 640 KB Output is correct
31 Correct 5 ms 768 KB Output is correct
32 Correct 4 ms 640 KB Output is correct
33 Correct 4 ms 640 KB Output is correct
34 Correct 5 ms 768 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 4 ms 640 KB Output is correct
37 Correct 5 ms 768 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 5 ms 768 KB Output is correct
43 Correct 4 ms 768 KB Output is correct
44 Correct 4 ms 768 KB Output is correct
45 Correct 239 ms 11848 KB Output is correct
46 Correct 228 ms 13252 KB Output is correct
47 Correct 198 ms 11000 KB Output is correct
48 Correct 208 ms 13048 KB Output is correct
49 Correct 230 ms 11256 KB Output is correct
50 Correct 190 ms 12792 KB Output is correct
51 Correct 236 ms 12816 KB Output is correct
52 Correct 142 ms 10460 KB Output is correct
53 Correct 197 ms 10460 KB Output is correct
54 Correct 244 ms 19960 KB Output is correct
55 Correct 246 ms 15396 KB Output is correct
56 Correct 237 ms 15228 KB Output is correct
57 Correct 205 ms 10616 KB Output is correct
58 Correct 148 ms 10592 KB Output is correct
59 Correct 176 ms 10524 KB Output is correct
60 Correct 212 ms 10616 KB Output is correct
61 Correct 206 ms 10540 KB Output is correct
62 Correct 201 ms 10488 KB Output is correct
63 Correct 215 ms 10616 KB Output is correct
64 Correct 222 ms 10616 KB Output is correct
65 Correct 334 ms 19960 KB Output is correct
66 Correct 346 ms 19960 KB Output is correct
67 Correct 210 ms 15224 KB Output is correct
68 Correct 224 ms 15224 KB Output is correct
69 Correct 220 ms 14712 KB Output is correct
70 Correct 246 ms 14748 KB Output is correct
71 Correct 223 ms 19932 KB Output is correct
72 Correct 217 ms 20004 KB Output is correct
73 Correct 235 ms 16760 KB Output is correct
74 Correct 248 ms 16760 KB Output is correct
75 Correct 197 ms 16164 KB Output is correct
76 Correct 206 ms 19960 KB Output is correct
77 Correct 219 ms 20048 KB Output is correct
78 Correct 210 ms 12792 KB Output is correct
79 Correct 222 ms 11896 KB Output is correct
80 Correct 3164 ms 106152 KB Output is correct
81 Correct 3095 ms 105080 KB Output is correct
82 Correct 3021 ms 101272 KB Output is correct
83 Correct 3137 ms 105088 KB Output is correct
84 Correct 2964 ms 106232 KB Output is correct
85 Correct 2820 ms 102060 KB Output is correct
86 Correct 3001 ms 105336 KB Output is correct
87 Correct 2086 ms 100600 KB Output is correct
88 Correct 3165 ms 100472 KB Output is correct
89 Correct 3469 ms 194316 KB Output is correct
90 Correct 3452 ms 147212 KB Output is correct
91 Correct 3368 ms 147412 KB Output is correct
92 Correct 2845 ms 100316 KB Output is correct
93 Correct 2044 ms 100296 KB Output is correct
94 Correct 2773 ms 100420 KB Output is correct
95 Correct 2904 ms 100316 KB Output is correct
96 Correct 2870 ms 100336 KB Output is correct
97 Correct 2688 ms 100396 KB Output is correct
98 Correct 2752 ms 100472 KB Output is correct
99 Correct 2687 ms 100344 KB Output is correct
100 Correct 5028 ms 194424 KB Output is correct
101 Correct 5157 ms 208764 KB Output is correct
102 Correct 3119 ms 162240 KB Output is correct
103 Correct 3131 ms 162040 KB Output is correct
104 Correct 3021 ms 156760 KB Output is correct
105 Correct 2849 ms 156648 KB Output is correct
106 Correct 3025 ms 208904 KB Output is correct
107 Correct 3037 ms 208824 KB Output is correct
108 Correct 2926 ms 177528 KB Output is correct
109 Correct 3134 ms 177668 KB Output is correct
110 Correct 2886 ms 207232 KB Output is correct
111 Correct 2865 ms 209004 KB Output is correct
112 Correct 2856 ms 209040 KB Output is correct
113 Correct 2995 ms 115784 KB Output is correct
114 Correct 2906 ms 119456 KB Output is correct