Submission #575186

# Submission time Handle Problem Language Result Execution time Memory
575186 2022-06-09T20:47:47 Z Sam_a17 Stranded Far From Home (BOI22_island) C++14
100 / 100
673 ms 62788 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], 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, value[a] += value[b];

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

  for(auto i: comp[b]) {
    comp[a].push_back(i);
  } comp[b].clear();
  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, 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;
      }
      comp[c].clear();
      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 10 ms 19028 KB Output is correct
2 Correct 10 ms 19044 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 15 ms 19360 KB Output is correct
5 Correct 12 ms 19412 KB Output is correct
6 Correct 15 ms 19432 KB Output is correct
7 Correct 13 ms 19352 KB Output is correct
8 Correct 12 ms 19284 KB Output is correct
9 Correct 11 ms 19412 KB Output is correct
10 Correct 12 ms 19540 KB Output is correct
11 Correct 13 ms 19424 KB Output is correct
12 Correct 11 ms 19524 KB Output is correct
13 Correct 11 ms 19448 KB Output is correct
14 Correct 11 ms 19512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19060 KB Output is correct
2 Correct 13 ms 19144 KB Output is correct
3 Correct 335 ms 56732 KB Output is correct
4 Correct 266 ms 56736 KB Output is correct
5 Correct 423 ms 58172 KB Output is correct
6 Correct 392 ms 59500 KB Output is correct
7 Correct 480 ms 59580 KB Output is correct
8 Correct 361 ms 56760 KB Output is correct
9 Correct 298 ms 62756 KB Output is correct
10 Correct 179 ms 55228 KB Output is correct
11 Correct 385 ms 58172 KB Output is correct
12 Correct 492 ms 56720 KB Output is correct
13 Correct 262 ms 55656 KB Output is correct
14 Correct 267 ms 55836 KB Output is correct
15 Correct 241 ms 55700 KB Output is correct
16 Correct 197 ms 55276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 593 ms 59552 KB Output is correct
3 Correct 572 ms 59992 KB Output is correct
4 Correct 286 ms 55728 KB Output is correct
5 Correct 268 ms 55848 KB Output is correct
6 Correct 634 ms 59756 KB Output is correct
7 Correct 261 ms 55716 KB Output is correct
8 Correct 276 ms 55668 KB Output is correct
9 Correct 200 ms 55344 KB Output is correct
10 Correct 296 ms 54812 KB Output is correct
11 Correct 342 ms 55140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 498 ms 56976 KB Output is correct
3 Correct 497 ms 57776 KB Output is correct
4 Correct 562 ms 59856 KB Output is correct
5 Correct 655 ms 61228 KB Output is correct
6 Correct 561 ms 56996 KB Output is correct
7 Correct 329 ms 58128 KB Output is correct
8 Correct 235 ms 57464 KB Output is correct
9 Correct 245 ms 40456 KB Output is correct
10 Correct 612 ms 62784 KB Output is correct
11 Correct 327 ms 55052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 10 ms 19044 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 15 ms 19360 KB Output is correct
5 Correct 12 ms 19412 KB Output is correct
6 Correct 15 ms 19432 KB Output is correct
7 Correct 13 ms 19352 KB Output is correct
8 Correct 12 ms 19284 KB Output is correct
9 Correct 11 ms 19412 KB Output is correct
10 Correct 12 ms 19540 KB Output is correct
11 Correct 13 ms 19424 KB Output is correct
12 Correct 11 ms 19524 KB Output is correct
13 Correct 11 ms 19448 KB Output is correct
14 Correct 11 ms 19512 KB Output is correct
15 Correct 10 ms 19060 KB Output is correct
16 Correct 13 ms 19144 KB Output is correct
17 Correct 335 ms 56732 KB Output is correct
18 Correct 266 ms 56736 KB Output is correct
19 Correct 423 ms 58172 KB Output is correct
20 Correct 392 ms 59500 KB Output is correct
21 Correct 480 ms 59580 KB Output is correct
22 Correct 361 ms 56760 KB Output is correct
23 Correct 298 ms 62756 KB Output is correct
24 Correct 179 ms 55228 KB Output is correct
25 Correct 385 ms 58172 KB Output is correct
26 Correct 492 ms 56720 KB Output is correct
27 Correct 262 ms 55656 KB Output is correct
28 Correct 267 ms 55836 KB Output is correct
29 Correct 241 ms 55700 KB Output is correct
30 Correct 197 ms 55276 KB Output is correct
31 Correct 11 ms 19028 KB Output is correct
32 Correct 593 ms 59552 KB Output is correct
33 Correct 572 ms 59992 KB Output is correct
34 Correct 286 ms 55728 KB Output is correct
35 Correct 268 ms 55848 KB Output is correct
36 Correct 634 ms 59756 KB Output is correct
37 Correct 261 ms 55716 KB Output is correct
38 Correct 276 ms 55668 KB Output is correct
39 Correct 200 ms 55344 KB Output is correct
40 Correct 296 ms 54812 KB Output is correct
41 Correct 342 ms 55140 KB Output is correct
42 Correct 10 ms 19028 KB Output is correct
43 Correct 498 ms 56976 KB Output is correct
44 Correct 497 ms 57776 KB Output is correct
45 Correct 562 ms 59856 KB Output is correct
46 Correct 655 ms 61228 KB Output is correct
47 Correct 561 ms 56996 KB Output is correct
48 Correct 329 ms 58128 KB Output is correct
49 Correct 235 ms 57464 KB Output is correct
50 Correct 245 ms 40456 KB Output is correct
51 Correct 612 ms 62784 KB Output is correct
52 Correct 327 ms 55052 KB Output is correct
53 Correct 10 ms 19028 KB Output is correct
54 Correct 10 ms 19028 KB Output is correct
55 Correct 12 ms 19136 KB Output is correct
56 Correct 12 ms 19412 KB Output is correct
57 Correct 13 ms 19492 KB Output is correct
58 Correct 12 ms 19468 KB Output is correct
59 Correct 12 ms 19412 KB Output is correct
60 Correct 12 ms 19284 KB Output is correct
61 Correct 12 ms 19516 KB Output is correct
62 Correct 16 ms 19540 KB Output is correct
63 Correct 13 ms 19420 KB Output is correct
64 Correct 15 ms 19540 KB Output is correct
65 Correct 12 ms 19436 KB Output is correct
66 Correct 12 ms 19412 KB Output is correct
67 Correct 286 ms 56760 KB Output is correct
68 Correct 303 ms 56680 KB Output is correct
69 Correct 400 ms 58100 KB Output is correct
70 Correct 398 ms 59464 KB Output is correct
71 Correct 427 ms 59512 KB Output is correct
72 Correct 387 ms 56728 KB Output is correct
73 Correct 312 ms 62764 KB Output is correct
74 Correct 195 ms 55284 KB Output is correct
75 Correct 388 ms 58340 KB Output is correct
76 Correct 493 ms 56648 KB Output is correct
77 Correct 288 ms 55728 KB Output is correct
78 Correct 255 ms 55788 KB Output is correct
79 Correct 249 ms 55744 KB Output is correct
80 Correct 255 ms 55372 KB Output is correct
81 Correct 558 ms 59572 KB Output is correct
82 Correct 606 ms 59988 KB Output is correct
83 Correct 308 ms 55704 KB Output is correct
84 Correct 266 ms 55860 KB Output is correct
85 Correct 571 ms 59708 KB Output is correct
86 Correct 273 ms 55692 KB Output is correct
87 Correct 263 ms 55700 KB Output is correct
88 Correct 274 ms 54724 KB Output is correct
89 Correct 364 ms 55032 KB Output is correct
90 Correct 510 ms 57084 KB Output is correct
91 Correct 516 ms 57800 KB Output is correct
92 Correct 585 ms 59888 KB Output is correct
93 Correct 559 ms 61204 KB Output is correct
94 Correct 528 ms 57188 KB Output is correct
95 Correct 334 ms 58384 KB Output is correct
96 Correct 215 ms 57412 KB Output is correct
97 Correct 187 ms 40460 KB Output is correct
98 Correct 569 ms 62788 KB Output is correct
99 Correct 352 ms 55016 KB Output is correct
100 Correct 48 ms 24132 KB Output is correct
101 Correct 673 ms 58480 KB Output is correct
102 Correct 395 ms 50964 KB Output is correct
103 Correct 430 ms 51012 KB Output is correct
104 Correct 546 ms 56108 KB Output is correct