답안 #24296

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
24296 2017-06-04T06:46:52 Z jiaqiyang Port Facility (JOI17_port_facility) C++14
100 / 100
2383 ms 1040064 KB
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int nextInt() {
  char ch;
  while (!isdigit(ch = getchar())) {}
  int res = ch - '0';
  while (isdigit(ch = getchar())) res = 10 * res + ch - '0';
  return res;
}

const int N = 2000000 + 10, S = 40 * N, MOD = 1000000007;

int n, a[N], b[N], c[N], id[N];

int adj[N], to[S], next[S];

void link(int a, int b) {
  static int cnt = 2;
  to[cnt] = b;
  next[cnt] = adj[a];
  adj[a] = cnt++;
}

int buffer[S], *pool[N];

void update(int p, int v) {
  for (; p <= 2 * n; p += p & -p) pool[p][++pool[p][0]] = v;
}

void query(int p, int v) {
  for (; p; p ^= p & -p) {
    int &y = pool[p][0];
    if (!y) continue;
    int x = pool[p][y];
    if (b[x] < a[v]) continue;
    for (; y; --y) {
      int u = pool[p][y];
      if (b[u] >= a[v]) link(u, v), link(v, u); else break;
    }
    pool[p][++y] = x;
  }
}

int tag[N];

void fail() {
  puts("0");
  exit(0);
}

void dfs(int a) {
  int c = 3 - tag[a];
  for (int i = adj[a]; i; i = next[i]) {
    int b = to[i];
    if (!tag[b]) {
      tag[b] = c;
      dfs(b);
    } else if (tag[b] != c) {
      fail();
    }
  }
}

class {
  int data[N];
  void add(int p, int v) {
    for (; p <= 2 * n; p += p & -p) data[p] += v;
  }
 public:
  void clear() {
    memset(data, 0, sizeof data);
  }
  void add(int l, int r, int v) {
    add(l, v);
    add(r + 1, -v);
  }
  int query(int p) {
    int res = 0;
    for (; p; p ^= p & -p) res += data[p];
    return res;
  }
} bit;

void check(int y) {
  bit.clear();
  for (int i = 1; i <= 2 * n; ++i) {
    int x = id[i];
    if (tag[x] != y) continue;
    if (c[i] > i) {
      bit.add(1, i, 1);
    } else {
      bit.add(1, c[i], -1);
      if (bit.query(c[i])) fail();
      bit.add(c[i], i, 1);
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1, *top = buffer; i <= 2 * n; ++i) {
    pool[i] = top;
    top += (i & -i) + 1;
  }
  for (int i = 1; i <= n; ++i) a[i] = nextInt(), b[i] = nextInt();
  for (int i = 1; i <= n; ++i) c[a[i]] = b[i], c[b[i]] = a[i], id[a[i]] = id[b[i]] = i;
  for (int i = 1; i <= 2 * n; ++i) {
    if (c[i] > i) continue;
    query(c[i], id[i]);
    update(c[i], id[i]);
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) if (!tag[i]) ++cnt, tag[i] = 1, dfs(i);
  check(1);
  check(2);
  int ans = 1;
  while (cnt--) ans = 2 * ans % MOD;
  printf("%d\n", ans);
  return 0;
}

Compilation message

port_facility.cpp: In function 'int main()':
port_facility.cpp:103:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1008936 KB Output is correct
2 Correct 0 ms 1008936 KB Output is correct
3 Correct 0 ms 1008936 KB Output is correct
4 Correct 3 ms 1008936 KB Output is correct
5 Correct 0 ms 1008936 KB Output is correct
6 Correct 3 ms 1008936 KB Output is correct
7 Correct 3 ms 1008936 KB Output is correct
8 Correct 3 ms 1008936 KB Output is correct
9 Correct 0 ms 1008936 KB Output is correct
10 Correct 3 ms 1008936 KB Output is correct
11 Correct 0 ms 1008936 KB Output is correct
12 Correct 0 ms 1008936 KB Output is correct
13 Correct 0 ms 1008936 KB Output is correct
14 Correct 3 ms 1008936 KB Output is correct
15 Correct 3 ms 1008936 KB Output is correct
16 Correct 0 ms 1008936 KB Output is correct
17 Correct 3 ms 1008936 KB Output is correct
18 Correct 3 ms 1008936 KB Output is correct
19 Correct 3 ms 1008936 KB Output is correct
20 Correct 0 ms 1008936 KB Output is correct
21 Correct 0 ms 1008936 KB Output is correct
22 Correct 0 ms 1008936 KB Output is correct
23 Correct 0 ms 1008936 KB Output is correct
24 Correct 0 ms 1008936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1008936 KB Output is correct
2 Correct 0 ms 1008936 KB Output is correct
3 Correct 0 ms 1008936 KB Output is correct
4 Correct 3 ms 1008936 KB Output is correct
5 Correct 0 ms 1008936 KB Output is correct
6 Correct 3 ms 1008936 KB Output is correct
7 Correct 3 ms 1008936 KB Output is correct
8 Correct 3 ms 1008936 KB Output is correct
9 Correct 0 ms 1008936 KB Output is correct
10 Correct 3 ms 1008936 KB Output is correct
11 Correct 0 ms 1008936 KB Output is correct
12 Correct 0 ms 1008936 KB Output is correct
13 Correct 0 ms 1008936 KB Output is correct
14 Correct 3 ms 1008936 KB Output is correct
15 Correct 3 ms 1008936 KB Output is correct
16 Correct 0 ms 1008936 KB Output is correct
17 Correct 3 ms 1008936 KB Output is correct
18 Correct 3 ms 1008936 KB Output is correct
19 Correct 3 ms 1008936 KB Output is correct
20 Correct 0 ms 1008936 KB Output is correct
21 Correct 0 ms 1008936 KB Output is correct
22 Correct 0 ms 1008936 KB Output is correct
23 Correct 0 ms 1008936 KB Output is correct
24 Correct 0 ms 1008936 KB Output is correct
25 Correct 3 ms 1008936 KB Output is correct
26 Correct 3 ms 1008936 KB Output is correct
27 Correct 0 ms 1008936 KB Output is correct
28 Correct 6 ms 1008936 KB Output is correct
29 Correct 0 ms 1008936 KB Output is correct
30 Correct 3 ms 1008936 KB Output is correct
31 Correct 0 ms 1008936 KB Output is correct
32 Correct 0 ms 1008936 KB Output is correct
33 Correct 0 ms 1008936 KB Output is correct
34 Correct 0 ms 1008936 KB Output is correct
35 Correct 3 ms 1008936 KB Output is correct
36 Correct 6 ms 1008936 KB Output is correct
37 Correct 0 ms 1008936 KB Output is correct
38 Correct 0 ms 1008936 KB Output is correct
39 Correct 0 ms 1008936 KB Output is correct
40 Correct 6 ms 1008936 KB Output is correct
41 Correct 3 ms 1008936 KB Output is correct
42 Correct 0 ms 1008936 KB Output is correct
43 Correct 0 ms 1008936 KB Output is correct
44 Correct 0 ms 1008936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1008936 KB Output is correct
2 Correct 0 ms 1008936 KB Output is correct
3 Correct 0 ms 1008936 KB Output is correct
4 Correct 3 ms 1008936 KB Output is correct
5 Correct 0 ms 1008936 KB Output is correct
6 Correct 3 ms 1008936 KB Output is correct
7 Correct 3 ms 1008936 KB Output is correct
8 Correct 3 ms 1008936 KB Output is correct
9 Correct 0 ms 1008936 KB Output is correct
10 Correct 3 ms 1008936 KB Output is correct
11 Correct 0 ms 1008936 KB Output is correct
12 Correct 0 ms 1008936 KB Output is correct
13 Correct 0 ms 1008936 KB Output is correct
14 Correct 3 ms 1008936 KB Output is correct
15 Correct 3 ms 1008936 KB Output is correct
16 Correct 0 ms 1008936 KB Output is correct
17 Correct 3 ms 1008936 KB Output is correct
18 Correct 3 ms 1008936 KB Output is correct
19 Correct 3 ms 1008936 KB Output is correct
20 Correct 0 ms 1008936 KB Output is correct
21 Correct 0 ms 1008936 KB Output is correct
22 Correct 0 ms 1008936 KB Output is correct
23 Correct 0 ms 1008936 KB Output is correct
24 Correct 0 ms 1008936 KB Output is correct
25 Correct 3 ms 1008936 KB Output is correct
26 Correct 3 ms 1008936 KB Output is correct
27 Correct 0 ms 1008936 KB Output is correct
28 Correct 6 ms 1008936 KB Output is correct
29 Correct 0 ms 1008936 KB Output is correct
30 Correct 3 ms 1008936 KB Output is correct
31 Correct 0 ms 1008936 KB Output is correct
32 Correct 0 ms 1008936 KB Output is correct
33 Correct 0 ms 1008936 KB Output is correct
34 Correct 0 ms 1008936 KB Output is correct
35 Correct 3 ms 1008936 KB Output is correct
36 Correct 6 ms 1008936 KB Output is correct
37 Correct 0 ms 1008936 KB Output is correct
38 Correct 0 ms 1008936 KB Output is correct
39 Correct 0 ms 1008936 KB Output is correct
40 Correct 6 ms 1008936 KB Output is correct
41 Correct 3 ms 1008936 KB Output is correct
42 Correct 0 ms 1008936 KB Output is correct
43 Correct 0 ms 1008936 KB Output is correct
44 Correct 0 ms 1008936 KB Output is correct
45 Correct 66 ms 1009048 KB Output is correct
46 Correct 99 ms 1009280 KB Output is correct
47 Correct 73 ms 1008936 KB Output is correct
48 Correct 93 ms 1009272 KB Output is correct
49 Correct 69 ms 1008936 KB Output is correct
50 Correct 29 ms 1009124 KB Output is correct
51 Correct 43 ms 1008936 KB Output is correct
52 Correct 56 ms 1008936 KB Output is correct
53 Correct 49 ms 1008936 KB Output is correct
54 Correct 39 ms 1008936 KB Output is correct
55 Correct 36 ms 1008936 KB Output is correct
56 Correct 36 ms 1008936 KB Output is correct
57 Correct 63 ms 1008936 KB Output is correct
58 Correct 39 ms 1008936 KB Output is correct
59 Correct 63 ms 1008936 KB Output is correct
60 Correct 66 ms 1008936 KB Output is correct
61 Correct 79 ms 1008936 KB Output is correct
62 Correct 26 ms 1008936 KB Output is correct
63 Correct 36 ms 1008936 KB Output is correct
64 Correct 49 ms 1008936 KB Output is correct
65 Correct 76 ms 1008936 KB Output is correct
66 Correct 79 ms 1008936 KB Output is correct
67 Correct 59 ms 1008936 KB Output is correct
68 Correct 69 ms 1008936 KB Output is correct
69 Correct 59 ms 1008936 KB Output is correct
70 Correct 59 ms 1008936 KB Output is correct
71 Correct 59 ms 1008936 KB Output is correct
72 Correct 63 ms 1008936 KB Output is correct
73 Correct 59 ms 1008936 KB Output is correct
74 Correct 56 ms 1008936 KB Output is correct
75 Correct 53 ms 1010668 KB Output is correct
76 Correct 63 ms 1011936 KB Output is correct
77 Correct 33 ms 1011936 KB Output is correct
78 Correct 83 ms 1009228 KB Output is correct
79 Correct 106 ms 1009072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1008936 KB Output is correct
2 Correct 0 ms 1008936 KB Output is correct
3 Correct 0 ms 1008936 KB Output is correct
4 Correct 3 ms 1008936 KB Output is correct
5 Correct 0 ms 1008936 KB Output is correct
6 Correct 3 ms 1008936 KB Output is correct
7 Correct 3 ms 1008936 KB Output is correct
8 Correct 3 ms 1008936 KB Output is correct
9 Correct 0 ms 1008936 KB Output is correct
10 Correct 3 ms 1008936 KB Output is correct
11 Correct 0 ms 1008936 KB Output is correct
12 Correct 0 ms 1008936 KB Output is correct
13 Correct 0 ms 1008936 KB Output is correct
14 Correct 3 ms 1008936 KB Output is correct
15 Correct 3 ms 1008936 KB Output is correct
16 Correct 0 ms 1008936 KB Output is correct
17 Correct 3 ms 1008936 KB Output is correct
18 Correct 3 ms 1008936 KB Output is correct
19 Correct 3 ms 1008936 KB Output is correct
20 Correct 0 ms 1008936 KB Output is correct
21 Correct 0 ms 1008936 KB Output is correct
22 Correct 0 ms 1008936 KB Output is correct
23 Correct 0 ms 1008936 KB Output is correct
24 Correct 0 ms 1008936 KB Output is correct
25 Correct 3 ms 1008936 KB Output is correct
26 Correct 3 ms 1008936 KB Output is correct
27 Correct 0 ms 1008936 KB Output is correct
28 Correct 6 ms 1008936 KB Output is correct
29 Correct 0 ms 1008936 KB Output is correct
30 Correct 3 ms 1008936 KB Output is correct
31 Correct 0 ms 1008936 KB Output is correct
32 Correct 0 ms 1008936 KB Output is correct
33 Correct 0 ms 1008936 KB Output is correct
34 Correct 0 ms 1008936 KB Output is correct
35 Correct 3 ms 1008936 KB Output is correct
36 Correct 6 ms 1008936 KB Output is correct
37 Correct 0 ms 1008936 KB Output is correct
38 Correct 0 ms 1008936 KB Output is correct
39 Correct 0 ms 1008936 KB Output is correct
40 Correct 6 ms 1008936 KB Output is correct
41 Correct 3 ms 1008936 KB Output is correct
42 Correct 0 ms 1008936 KB Output is correct
43 Correct 0 ms 1008936 KB Output is correct
44 Correct 0 ms 1008936 KB Output is correct
45 Correct 66 ms 1009048 KB Output is correct
46 Correct 99 ms 1009280 KB Output is correct
47 Correct 73 ms 1008936 KB Output is correct
48 Correct 93 ms 1009272 KB Output is correct
49 Correct 69 ms 1008936 KB Output is correct
50 Correct 29 ms 1009124 KB Output is correct
51 Correct 43 ms 1008936 KB Output is correct
52 Correct 56 ms 1008936 KB Output is correct
53 Correct 49 ms 1008936 KB Output is correct
54 Correct 39 ms 1008936 KB Output is correct
55 Correct 36 ms 1008936 KB Output is correct
56 Correct 36 ms 1008936 KB Output is correct
57 Correct 63 ms 1008936 KB Output is correct
58 Correct 39 ms 1008936 KB Output is correct
59 Correct 63 ms 1008936 KB Output is correct
60 Correct 66 ms 1008936 KB Output is correct
61 Correct 79 ms 1008936 KB Output is correct
62 Correct 26 ms 1008936 KB Output is correct
63 Correct 36 ms 1008936 KB Output is correct
64 Correct 49 ms 1008936 KB Output is correct
65 Correct 76 ms 1008936 KB Output is correct
66 Correct 79 ms 1008936 KB Output is correct
67 Correct 59 ms 1008936 KB Output is correct
68 Correct 69 ms 1008936 KB Output is correct
69 Correct 59 ms 1008936 KB Output is correct
70 Correct 59 ms 1008936 KB Output is correct
71 Correct 59 ms 1008936 KB Output is correct
72 Correct 63 ms 1008936 KB Output is correct
73 Correct 59 ms 1008936 KB Output is correct
74 Correct 56 ms 1008936 KB Output is correct
75 Correct 53 ms 1010668 KB Output is correct
76 Correct 63 ms 1011936 KB Output is correct
77 Correct 33 ms 1011936 KB Output is correct
78 Correct 83 ms 1009228 KB Output is correct
79 Correct 106 ms 1009072 KB Output is correct
80 Correct 1449 ms 1009860 KB Output is correct
81 Correct 1406 ms 1009652 KB Output is correct
82 Correct 1303 ms 1009040 KB Output is correct
83 Correct 1376 ms 1009668 KB Output is correct
84 Correct 1283 ms 1009848 KB Output is correct
85 Correct 609 ms 1008948 KB Output is correct
86 Correct 666 ms 1009232 KB Output is correct
87 Correct 989 ms 1008936 KB Output is correct
88 Correct 803 ms 1008936 KB Output is correct
89 Correct 863 ms 1008936 KB Output is correct
90 Correct 779 ms 1008936 KB Output is correct
91 Correct 899 ms 1008936 KB Output is correct
92 Correct 1316 ms 1008936 KB Output is correct
93 Correct 949 ms 1008936 KB Output is correct
94 Correct 1046 ms 1008936 KB Output is correct
95 Correct 1179 ms 1008936 KB Output is correct
96 Correct 1343 ms 1008936 KB Output is correct
97 Correct 513 ms 1008936 KB Output is correct
98 Correct 623 ms 1008936 KB Output is correct
99 Correct 636 ms 1008936 KB Output is correct
100 Correct 2383 ms 1008936 KB Output is correct
101 Correct 2156 ms 1008936 KB Output is correct
102 Correct 1036 ms 1008936 KB Output is correct
103 Correct 1002 ms 1008936 KB Output is correct
104 Correct 1113 ms 1008936 KB Output is correct
105 Correct 1073 ms 1008936 KB Output is correct
106 Correct 859 ms 1008936 KB Output is correct
107 Correct 1033 ms 1008936 KB Output is correct
108 Correct 1106 ms 1008936 KB Output is correct
109 Correct 1053 ms 1008936 KB Output is correct
110 Correct 1309 ms 1039424 KB Output is correct
111 Correct 1433 ms 1040060 KB Output is correct
112 Correct 746 ms 1040064 KB Output is correct
113 Correct 1379 ms 1009012 KB Output is correct
114 Correct 1233 ms 1009616 KB Output is correct