답안 #585183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585183 2022-06-28T12:07:27 Z MilosMilutinovic Nekameleoni (COCI15_nekameleoni) C++14
42 / 140
3000 ms 458056 KB
/**
 *    author:  wxhtzdy
 *    created: 28.06.2022 12:53:33
**/
#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const int K = 55;

int mn[4 * N], sz_l[4 * N], sz_r[4 * N];
pair<int, int> ll[4 * N][K * 2];
pair<int, int> rr[4 * N][K * 2];
  
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  const int inf = (int) 1e6;
  for (int i = 0; i < 4 * n; i++) {
    mn[i] = inf;
    sz_l[i] = sz_r[i] = 0;
  }
  function<void(int, int, int)> pull = [&](int node, int l, int r) {
    {
      sz_l[node] = 0;
      long long seen = 0;
      int i = 0, j = 0;
      function<void(pair<int, int>)> Insert = [&](pair<int, int> v) {
        if (!(seen >> v.second & 1)) {
          seen = seen | (1LL << v.second);
          ll[node][sz_l[node]++] = v;
        }
      };
      while (i < sz_l[node + node] || j < sz_l[node + node + 1]) {
        if (i == sz_l[node + node] || (j < sz_l[node + node + 1] && ll[node + node][i].first > ll[node + node + 1][j].first)) {
          Insert(ll[node + node + 1][j++]);
        } else {
          Insert(ll[node + node][i++]);
        }
      }
    }
    {
      sz_r[node] = 0;
      long long seen = 0;
      int i = sz_r[node + node] - 1, j = sz_r[node + node + 1] - 1;
      function<void(pair<int, int>)> Insert = [&](pair<int, int> v) {
        if (!(seen >> v.second & 1)) {
          seen = seen | (1LL << v.second);
          rr[node][sz_r[node]++] = v;
        }
      };
      while (i >= 0 || j >= 0) {
        if (i < 0 || (j >= 0 && rr[node + node][i].first < rr[node + node + 1][j].first)) {
          Insert(rr[node + node + 1][j--]);
        } else {
          Insert(rr[node + node][i--]);
        }
      }                 
      reverse(rr[node], rr[node] + sz_r[node]);
    }
    mn[node] = min(mn[node + node], mn[node + node + 1]);
    vector<int> cnt(k);
    int d = 0;
    function<void(int)> Add = [&](int x) {
      if (cnt[x] == 0) {
        d += 1;
      }
      cnt[x] += 1;
    };
    function<void(int)> Rem = [&](int x) {
      cnt[x] -= 1;
      if (cnt[x] == 0) {
        d -= 1;
      }
    };
    for (int i = 0; i < sz_r[node + node]; i++) {
      Add(rr[node + node][i].second);
    }
    int ptr = 0;
    for (int i = 0; i < sz_r[node + node]; i++) {
      auto& p = rr[node + node][i];
      while (ptr < sz_l[node + node + 1] && d < k) {
        Add(ll[node + node + 1][ptr].second);
        ptr += 1;  
      }
      if (d == k) {
        int b = (ptr == 0 ? rr[node + node][sz_r[node + node] - 1].first : ll[node + node + 1][ptr - 1].first);
        mn[node] = min(mn[node], b - p.first + 1);
      }               
      Rem(p.second);
    }
  };
  function<void(int, int, int)> build = [&](int node, int l, int r) {
    if (l == r) {
      sz_l[node] = 0;  
      sz_r[node] = 0;
      ll[node][sz_l[node]++] = make_pair(l, a[l]);
      rr[node][sz_r[node]++] = make_pair(r, a[r]);
      mn[node] = (k == 1 ? 1 : inf);
      return;
    }
    int mid = l + r >> 1;
    build(node + node, l, mid);
    build(node + node + 1, mid + 1, r);
    pull(node, l, r);                                              
  };
  function<void(int, int, int, int)> modify = [&](int node, int l, int r, int i) {
    if (l == r) {
      sz_l[node] = 0;  
      sz_r[node] = 0;
      ll[node][sz_l[node]++] = make_pair(l, a[l]);
      rr[node][sz_r[node]++] = make_pair(r, a[r]);
      mn[node] = (k == 1 ? 1 : inf);
      return;
    }
    int mid = l + r >> 1;
    if (i <= mid) {
      modify(node + node, l, mid, i);
    } else {
      modify(node + node + 1, mid + 1, r, i);
    }
    pull(node, l, r);
  };
  build(1, 0, n - 1);
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int i, v;
      cin >> i >> v;
      --i; --v;
      a[i] = v;
      modify(1, 0, n - 1, i);
    } else {   
      cout << (mn[1] >= inf ? -1 : mn[1]) << '\n';
    }
  }                                                        
  return 0;
}

Compilation message

nekameleoni.cpp: In lambda function:
nekameleoni.cpp:110:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  110 |     int mid = l + r >> 1;
      |               ~~^~~
nekameleoni.cpp: In lambda function:
nekameleoni.cpp:124:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  124 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 14548 KB Output is correct
2 Correct 23 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 16724 KB Output is correct
2 Correct 61 ms 16816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 23416 KB Output is correct
2 Correct 122 ms 23424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1054 ms 97472 KB Output is correct
2 Execution timed out 3061 ms 278476 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1588 ms 229380 KB Output is correct
2 Execution timed out 3063 ms 443260 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2180 ms 197124 KB Output is correct
2 Execution timed out 3083 ms 385596 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2726 ms 230340 KB Output is correct
2 Execution timed out 3095 ms 403740 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2591 ms 230200 KB Output is correct
2 Execution timed out 3082 ms 424788 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3105 ms 458008 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3138 ms 458056 KB Time limit exceeded
2 Halted 0 ms 0 KB -