Submission #1017711

# Submission time Handle Problem Language Result Execution time Memory
1017711 2024-07-09T09:36:46 Z cadmiumsky Radio Towers (IOI22_towers) C++17
23 / 100
4000 ms 11984 KB
#include "towers.h"

#include <vector>
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 1e5 + 5;
int v[nmax];

namespace DSU {
   int dsu[nmax], nxt[nmax];
   int mxR[nmax];

   void init(int l, int r) {
      for(int i = l; i <= r; i++) dsu[i] = i, nxt[i] = i + 1;
      return;
   }
   int f(int x) { return x == dsu[x]? x : dsu[x] = f(dsu[x]); }
   void unite(int x, int y) {
      x = f(x);
      y = f(y);
      if(x == y) return;
      if(x > y) swap(x, y);
      dsu[y] = x;
      mxR[x] = max(mxR[x], mxR[y]);
      nxt[x] = nxt[y];
      return;
   }
   int coef(int i, const vector<int>& p) {
      i = f(i);
      return mxR[i] - max(v[p[i]], v[p[nxt[i]]]);
   }
}

int timeout[nmax];
int dir[nmax];

void initglobals(signed L, signed R) {
   
   ++L;
   ++R;
   
   vector<int> p;
   for(int i = L; i <= R; i++) {
      if((i == L  || v[i] < v[i - 1]) && (i == R || v[i] < v[i + 1])) p.emplace_back(i), timeout[i] = 1e9 + 5;
      else timeout[i] = 0;
   }
   
   DSU::init(0, sz(p));
   
   set<pii> heap;
   
   set<int> indemnizatii;
   for(int i = 0; i < sz(p) - 1; i++) {
      indemnizatii.emplace(i);
      indemnizatii.emplace(i + 1);
      int mx = v[p[i] + 1];
      for(int j = p[i] + 2; j < p[i + 1]; j++)
         mx = max(mx, v[j]);
      DSU::mxR[i] = mx;
      heap.emplace(mx - max(v[p[i]], v[p[i + 1]]), i);
   }
   
   auto get = [&](int p_) { return pii{DSU::coef(p_, p), p_}; };
      
   while(!heap.empty()) {
      auto [C, i] = *heap.begin();
      heap.erase(heap.begin());
      
      i = DSU::f(i);
      int urm = DSU::nxt[i];
      if(i != *indemnizatii.begin()) {
         int ant = DSU::f(i - 1);
         heap.erase(heap.find(get(ant)));
      }
      if(urm != *indemnizatii.rbegin()) 
         heap.erase(heap.find(get(urm)));
      
      if(v[p[i]] < v[p[urm]]) {
         timeout[p[urm]] = C;
         dir[p[urm]] = p[i];
         DSU::unite(i, urm);
         indemnizatii.erase(urm);
         if(i != *indemnizatii.begin()) {
            int ant = DSU::f(i - 1);
            heap.insert(get(ant));
         }
         if(i != *indemnizatii.rbegin())
            heap.insert(get(i));
         
      }
      else {
         timeout[p[i]] = C;
         dir[p[i]] = p[urm];
         if(i != *indemnizatii.begin()) {
            int ant = DSU::f(i - 1);
            DSU::unite(ant, i);
            heap.insert(get(ant));
         }
         if(urm != *indemnizatii.rbegin())
            heap.insert(get(urm));
         indemnizatii.erase(i);
      }  
   }
}

int occ[nmax];

void fill(signed L, signed R, signed D) {
   
   ++L;
   ++R;
   
   vector<int> p;
   for(int i = L; i <= R; i++) {
      occ[i] = 0;
      if((i == L  || v[i] < v[i - 1]) && (i == R || v[i] < v[i + 1])) p.emplace_back(i), occ[i] = 1;
   }
   
   DSU::init(0, sz(p));
   
   set<pii> heap;
   
   set<int> indemnizatii;
   for(int i = 0; i < sz(p) - 1; i++) {
      indemnizatii.emplace(i);
      indemnizatii.emplace(i + 1);
      int mx = v[p[i] + 1];
      for(int j = p[i] + 2; j < p[i + 1]; j++)
         mx = max(mx, v[j]);
      DSU::mxR[i] = mx;
      heap.emplace(mx - max(v[p[i]], v[p[i + 1]]), i);
   }
   
   auto get = [&](int p_) { return pii{DSU::coef(p_, p), p_}; };
      
   while(!heap.empty() && heap.begin() -> first < D) {
      auto [C, i] = *heap.begin();
      heap.erase(heap.begin());
      
      i = DSU::f(i);
      int urm = DSU::nxt[i];
      if(i != *indemnizatii.begin()) {
         int ant = DSU::f(i - 1);
         heap.erase(heap.find(get(ant)));
      }
      if(urm != *indemnizatii.rbegin()) 
         heap.erase(heap.find(get(urm)));
      
      if(v[p[i]] < v[p[urm]]) {
         occ[p[urm]] = 0;
         DSU::unite(i, urm);
         indemnizatii.erase(urm);
         if(i != *indemnizatii.begin()) {
            int ant = DSU::f(i - 1);
            heap.insert(get(ant));
         }
         if(i != *indemnizatii.rbegin())
            heap.insert(get(i));
         
      }
      else {
         occ[p[i]] = 0;
         if(i != *indemnizatii.begin()) {
            int ant = DSU::f(i - 1);
            DSU::unite(ant, i);
            heap.insert(get(ant));
         }
         if(urm != *indemnizatii.rbegin())
            heap.insert(get(urm));
         indemnizatii.erase(i);
      }  
   }
}


void init(signed N, std::vector<signed> H) {
   for(int i = 0; i < N; i++) v[i + 1] = H[i]; 
   initglobals(0, N - 1);
}


signed max_towers(signed L, signed R, signed D) {
   fill(L, R, D);
   ++L, ++R;
   
   
   if([&]() {
      for(int i = L; i <= R; i++) 
         if(timeout[i] != 0) return 0;
      return 1;
   }()) {
      int mx = 0;
      for(int i = L; i <= R; i++) mx = max(mx, v[i]);
      return mx - max(v[L], v[R]) >= D? 2 : 1;
   }
   
   int l = L, r = R;
   while (l <= r && timeout[l] < D) l++;
   if(l > r) return 1;
   
   int rez = 0;
   if(L < l) {
      
      int mn = L;
      for(int i = L + 1; i < l; i++) if(dir[i] < L && timeout[i] != 0 && v[mn] > v[i]) mn = i;
      int mx = 0; 
      for(int i = mn + 1; i < l; i++) mx = max(v[i], mx);
      
      
      if(mx - max(v[l], v[mn]) >= D) rez++;
   }
   
   while(timeout[r] < D) r--;
   
   if(r < R) {
      int mn = R;
      for(int i = r + 1; i < R; i++) if(dir[i] > R && timeout[i] != 0 && v[mn] > v[i]) mn = i;
      int mx = 0;
      for(int i = r + 1; i < mn; i++) mx = max(v[i], mx);
      if(mx - max(v[r], v[mn]) >= D) rez++;
   }
   
   for(int i = l; i <= r; i++)
      if(timeout[i] >= D) rez++;
   return rez;
}
#undef int
# Verdict Execution time Memory Grader output
1 Correct 2397 ms 5464 KB Output is correct
2 Execution timed out 4024 ms 5976 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4696 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Correct 3 ms 4696 KB Output is correct
7 Correct 2 ms 4696 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4540 KB Output is correct
17 Correct 2 ms 4696 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 2 ms 4696 KB Output is correct
22 Correct 2 ms 4696 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 1 ms 4440 KB Output is correct
26 Correct 2 ms 4440 KB Output is correct
27 Correct 2 ms 4440 KB Output is correct
28 Correct 2 ms 4696 KB Output is correct
29 Correct 2 ms 4696 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4696 KB Output is correct
32 Correct 1 ms 4440 KB Output is correct
33 Correct 1 ms 4440 KB Output is correct
34 Correct 1 ms 4440 KB Output is correct
35 Correct 1 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4696 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Correct 3 ms 4696 KB Output is correct
7 Correct 2 ms 4696 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4540 KB Output is correct
17 Correct 2 ms 4696 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 2 ms 4696 KB Output is correct
22 Correct 2 ms 4696 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 1 ms 4440 KB Output is correct
26 Correct 2 ms 4440 KB Output is correct
27 Correct 2 ms 4440 KB Output is correct
28 Correct 2 ms 4696 KB Output is correct
29 Correct 2 ms 4696 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4696 KB Output is correct
32 Correct 1 ms 4440 KB Output is correct
33 Correct 1 ms 4440 KB Output is correct
34 Correct 1 ms 4440 KB Output is correct
35 Correct 1 ms 4440 KB Output is correct
36 Correct 45 ms 7968 KB Output is correct
37 Correct 85 ms 9988 KB Output is correct
38 Correct 65 ms 9936 KB Output is correct
39 Correct 108 ms 11976 KB Output is correct
40 Correct 95 ms 11976 KB Output is correct
41 Correct 139 ms 11956 KB Output is correct
42 Correct 106 ms 11976 KB Output is correct
43 Correct 10 ms 5976 KB Output is correct
44 Correct 9 ms 5976 KB Output is correct
45 Correct 9 ms 6072 KB Output is correct
46 Correct 9 ms 5976 KB Output is correct
47 Correct 67 ms 9928 KB Output is correct
48 Correct 92 ms 11984 KB Output is correct
49 Correct 99 ms 11976 KB Output is correct
50 Correct 9 ms 5972 KB Output is correct
51 Correct 9 ms 5976 KB Output is correct
52 Correct 74 ms 9892 KB Output is correct
53 Correct 115 ms 11976 KB Output is correct
54 Correct 116 ms 11976 KB Output is correct
55 Correct 10 ms 6112 KB Output is correct
56 Correct 9 ms 5976 KB Output is correct
57 Correct 98 ms 9676 KB Output is correct
58 Correct 106 ms 10036 KB Output is correct
59 Correct 85 ms 10040 KB Output is correct
60 Correct 112 ms 11972 KB Output is correct
61 Correct 121 ms 11984 KB Output is correct
62 Correct 152 ms 11976 KB Output is correct
63 Correct 125 ms 11984 KB Output is correct
64 Correct 9 ms 5976 KB Output is correct
65 Correct 9 ms 5988 KB Output is correct
66 Correct 13 ms 6120 KB Output is correct
67 Correct 9 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4021 ms 9936 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4006 ms 5976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 2 ms 4696 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Correct 3 ms 4696 KB Output is correct
7 Correct 2 ms 4696 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4540 KB Output is correct
17 Correct 2 ms 4696 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 2 ms 4696 KB Output is correct
22 Correct 2 ms 4696 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 1 ms 4440 KB Output is correct
26 Correct 2 ms 4440 KB Output is correct
27 Correct 2 ms 4440 KB Output is correct
28 Correct 2 ms 4696 KB Output is correct
29 Correct 2 ms 4696 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4696 KB Output is correct
32 Correct 1 ms 4440 KB Output is correct
33 Correct 1 ms 4440 KB Output is correct
34 Correct 1 ms 4440 KB Output is correct
35 Correct 1 ms 4440 KB Output is correct
36 Correct 45 ms 7968 KB Output is correct
37 Correct 85 ms 9988 KB Output is correct
38 Correct 65 ms 9936 KB Output is correct
39 Correct 108 ms 11976 KB Output is correct
40 Correct 95 ms 11976 KB Output is correct
41 Correct 139 ms 11956 KB Output is correct
42 Correct 106 ms 11976 KB Output is correct
43 Correct 10 ms 5976 KB Output is correct
44 Correct 9 ms 5976 KB Output is correct
45 Correct 9 ms 6072 KB Output is correct
46 Correct 9 ms 5976 KB Output is correct
47 Correct 67 ms 9928 KB Output is correct
48 Correct 92 ms 11984 KB Output is correct
49 Correct 99 ms 11976 KB Output is correct
50 Correct 9 ms 5972 KB Output is correct
51 Correct 9 ms 5976 KB Output is correct
52 Correct 74 ms 9892 KB Output is correct
53 Correct 115 ms 11976 KB Output is correct
54 Correct 116 ms 11976 KB Output is correct
55 Correct 10 ms 6112 KB Output is correct
56 Correct 9 ms 5976 KB Output is correct
57 Correct 98 ms 9676 KB Output is correct
58 Correct 106 ms 10036 KB Output is correct
59 Correct 85 ms 10040 KB Output is correct
60 Correct 112 ms 11972 KB Output is correct
61 Correct 121 ms 11984 KB Output is correct
62 Correct 152 ms 11976 KB Output is correct
63 Correct 125 ms 11984 KB Output is correct
64 Correct 9 ms 5976 KB Output is correct
65 Correct 9 ms 5988 KB Output is correct
66 Correct 13 ms 6120 KB Output is correct
67 Correct 9 ms 5976 KB Output is correct
68 Execution timed out 4021 ms 9936 KB Time limit exceeded
69 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2397 ms 5464 KB Output is correct
2 Execution timed out 4024 ms 5976 KB Time limit exceeded
3 Halted 0 ms 0 KB -