Submission #575185

# Submission time Handle Problem Language Result Execution time Memory
575185 2022-06-09T20:45:17 Z Sam_a17 Stranded Far From Home (BOI22_island) C++14
100 / 100
640 ms 73516 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount

#define pb push_back
#define popf pop_front
#define popb pop_back

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque<T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();

  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 4e5 + 10;
long long n, m, p[N], sz[N], answ[N];
long long value[N];
set<pair<long long, int>> st;
vector<long long> adj[N], comp[N];

long long find(long long a) {
  if(a != p[a]) {
    p[a] = find(p[a]);
  }
  return p[a];
}

long long merge(long long a, long long b) {
  a = find(a), b = find(b);
  if(a == b) {
    return false;
  }
  if(sz(comp[a]) < sz(comp[b])) {
    swap(a, b);
  }

  auto it = st.find({value[b], b});
  if(it != st.end()) {
    st.erase(it);
  }

  it = st.find({value[a], a});
  if(it != st.end()) {
    st.erase(it);
  }

  p[b] = a, sz[a] += sz[b];
  value[a] += value[b];

  st.insert({value[a], a});

  for(auto i: comp[b]) {
    comp[a].push_back(i);
  }
  return true;
}

void solve_() {
  cin >> n >> m;

  vector<pair<long long, int>> A(n + 1);
  vector<long long> B(n + 1);
  for(int i = 1; i <= n; i++) {
    cin >> A[i].first;
    A[i].second = i;
    B[i] = A[i].first;

    p[i] = i, sz[i] = 1;
    value[i] = A[i].first;
    comp[i].push_back(i);

    st.insert({value[i], i});
    answ[i] = 1;
  }

  for(int i = 1; i <= m; i++) {
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  sort(all(A));

  for(int i = 1; i <= n; i++) {
    int j = i;
    while(j <= n && A[j].first == A[i].first) {
      for(auto k: adj[A[j].second]) {
        if(B[k] <= A[j].first) {
          merge(k, A[j].second);
        }
      }
      j++;
    }

    if(j > n) {
      break;
    }

    while(!st.empty() && st.begin()->first < A[j].first) {
      long long c = find(st.begin()->second);
      for(auto k: comp[c]) {
        answ[k] = 0;
      }
      st.erase(st.begin());
    }

    i = j - 1;
  }

  for(int i = 1; i <= n; i++) {
    printf("%lld", answ[i]);
  } printf("\n");
}

int main() {
  setIO("");

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1;
  solve(test_cases);

  return 0;
}

Compilation message

island.cpp: In function 'void setIO(std::string)':
island.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19056 KB Output is correct
3 Correct 12 ms 19108 KB Output is correct
4 Correct 11 ms 19412 KB Output is correct
5 Correct 11 ms 19540 KB Output is correct
6 Correct 11 ms 19360 KB Output is correct
7 Correct 12 ms 19376 KB Output is correct
8 Correct 11 ms 19412 KB Output is correct
9 Correct 13 ms 19476 KB Output is correct
10 Correct 12 ms 19540 KB Output is correct
11 Correct 12 ms 19540 KB Output is correct
12 Correct 12 ms 19668 KB Output is correct
13 Correct 11 ms 19412 KB Output is correct
14 Correct 11 ms 19540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19144 KB Output is correct
2 Correct 9 ms 19028 KB Output is correct
3 Correct 297 ms 58332 KB Output is correct
4 Correct 270 ms 58316 KB Output is correct
5 Correct 459 ms 63700 KB Output is correct
6 Correct 436 ms 65124 KB Output is correct
7 Correct 459 ms 65580 KB Output is correct
8 Correct 422 ms 58304 KB Output is correct
9 Correct 295 ms 73460 KB Output is correct
10 Correct 209 ms 59724 KB Output is correct
11 Correct 393 ms 59888 KB Output is correct
12 Correct 467 ms 59864 KB Output is correct
13 Correct 303 ms 57280 KB Output is correct
14 Correct 256 ms 57264 KB Output is correct
15 Correct 265 ms 57280 KB Output is correct
16 Correct 192 ms 56840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 606 ms 67540 KB Output is correct
3 Correct 613 ms 67676 KB Output is correct
4 Correct 301 ms 57292 KB Output is correct
5 Correct 256 ms 57860 KB Output is correct
6 Correct 598 ms 67720 KB Output is correct
7 Correct 254 ms 57280 KB Output is correct
8 Correct 268 ms 57308 KB Output is correct
9 Correct 200 ms 56896 KB Output is correct
10 Correct 286 ms 57332 KB Output is correct
11 Correct 345 ms 58240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19036 KB Output is correct
2 Correct 554 ms 61076 KB Output is correct
3 Correct 559 ms 62340 KB Output is correct
4 Correct 554 ms 64240 KB Output is correct
5 Correct 638 ms 62700 KB Output is correct
6 Correct 516 ms 58540 KB Output is correct
7 Correct 317 ms 59696 KB Output is correct
8 Correct 213 ms 58824 KB Output is correct
9 Correct 197 ms 42116 KB Output is correct
10 Correct 567 ms 64436 KB Output is correct
11 Correct 353 ms 57720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19056 KB Output is correct
3 Correct 12 ms 19108 KB Output is correct
4 Correct 11 ms 19412 KB Output is correct
5 Correct 11 ms 19540 KB Output is correct
6 Correct 11 ms 19360 KB Output is correct
7 Correct 12 ms 19376 KB Output is correct
8 Correct 11 ms 19412 KB Output is correct
9 Correct 13 ms 19476 KB Output is correct
10 Correct 12 ms 19540 KB Output is correct
11 Correct 12 ms 19540 KB Output is correct
12 Correct 12 ms 19668 KB Output is correct
13 Correct 11 ms 19412 KB Output is correct
14 Correct 11 ms 19540 KB Output is correct
15 Correct 9 ms 19144 KB Output is correct
16 Correct 9 ms 19028 KB Output is correct
17 Correct 297 ms 58332 KB Output is correct
18 Correct 270 ms 58316 KB Output is correct
19 Correct 459 ms 63700 KB Output is correct
20 Correct 436 ms 65124 KB Output is correct
21 Correct 459 ms 65580 KB Output is correct
22 Correct 422 ms 58304 KB Output is correct
23 Correct 295 ms 73460 KB Output is correct
24 Correct 209 ms 59724 KB Output is correct
25 Correct 393 ms 59888 KB Output is correct
26 Correct 467 ms 59864 KB Output is correct
27 Correct 303 ms 57280 KB Output is correct
28 Correct 256 ms 57264 KB Output is correct
29 Correct 265 ms 57280 KB Output is correct
30 Correct 192 ms 56840 KB Output is correct
31 Correct 10 ms 19028 KB Output is correct
32 Correct 606 ms 67540 KB Output is correct
33 Correct 613 ms 67676 KB Output is correct
34 Correct 301 ms 57292 KB Output is correct
35 Correct 256 ms 57860 KB Output is correct
36 Correct 598 ms 67720 KB Output is correct
37 Correct 254 ms 57280 KB Output is correct
38 Correct 268 ms 57308 KB Output is correct
39 Correct 200 ms 56896 KB Output is correct
40 Correct 286 ms 57332 KB Output is correct
41 Correct 345 ms 58240 KB Output is correct
42 Correct 10 ms 19036 KB Output is correct
43 Correct 554 ms 61076 KB Output is correct
44 Correct 559 ms 62340 KB Output is correct
45 Correct 554 ms 64240 KB Output is correct
46 Correct 638 ms 62700 KB Output is correct
47 Correct 516 ms 58540 KB Output is correct
48 Correct 317 ms 59696 KB Output is correct
49 Correct 213 ms 58824 KB Output is correct
50 Correct 197 ms 42116 KB Output is correct
51 Correct 567 ms 64436 KB Output is correct
52 Correct 353 ms 57720 KB Output is correct
53 Correct 10 ms 19028 KB Output is correct
54 Correct 11 ms 19028 KB Output is correct
55 Correct 10 ms 19028 KB Output is correct
56 Correct 12 ms 19464 KB Output is correct
57 Correct 12 ms 19460 KB Output is correct
58 Correct 13 ms 19540 KB Output is correct
59 Correct 12 ms 19504 KB Output is correct
60 Correct 12 ms 19412 KB Output is correct
61 Correct 15 ms 19544 KB Output is correct
62 Correct 13 ms 19560 KB Output is correct
63 Correct 13 ms 19540 KB Output is correct
64 Correct 12 ms 19668 KB Output is correct
65 Correct 12 ms 19448 KB Output is correct
66 Correct 12 ms 19524 KB Output is correct
67 Correct 292 ms 58360 KB Output is correct
68 Correct 281 ms 58324 KB Output is correct
69 Correct 441 ms 63692 KB Output is correct
70 Correct 422 ms 65080 KB Output is correct
71 Correct 483 ms 65620 KB Output is correct
72 Correct 364 ms 58276 KB Output is correct
73 Correct 301 ms 73516 KB Output is correct
74 Correct 204 ms 59732 KB Output is correct
75 Correct 405 ms 59676 KB Output is correct
76 Correct 460 ms 59908 KB Output is correct
77 Correct 267 ms 57404 KB Output is correct
78 Correct 258 ms 57260 KB Output is correct
79 Correct 239 ms 57276 KB Output is correct
80 Correct 206 ms 57004 KB Output is correct
81 Correct 581 ms 67568 KB Output is correct
82 Correct 608 ms 67688 KB Output is correct
83 Correct 289 ms 57280 KB Output is correct
84 Correct 264 ms 57904 KB Output is correct
85 Correct 640 ms 67612 KB Output is correct
86 Correct 260 ms 57352 KB Output is correct
87 Correct 248 ms 57272 KB Output is correct
88 Correct 295 ms 57372 KB Output is correct
89 Correct 334 ms 58220 KB Output is correct
90 Correct 551 ms 61228 KB Output is correct
91 Correct 544 ms 62352 KB Output is correct
92 Correct 558 ms 64172 KB Output is correct
93 Correct 586 ms 62792 KB Output is correct
94 Correct 507 ms 58504 KB Output is correct
95 Correct 300 ms 59792 KB Output is correct
96 Correct 216 ms 58872 KB Output is correct
97 Correct 210 ms 42156 KB Output is correct
98 Correct 548 ms 64348 KB Output is correct
99 Correct 348 ms 57792 KB Output is correct
100 Correct 48 ms 24256 KB Output is correct
101 Correct 608 ms 63740 KB Output is correct
102 Correct 442 ms 52160 KB Output is correct
103 Correct 436 ms 53716 KB Output is correct
104 Correct 585 ms 60036 KB Output is correct