답안 #585168

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

using namespace std;

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;
  vector<int> mn(4 * n, inf);               
  vector<vector<int>> ll(4 * n, vector<int>(k));
  vector<vector<int>> rr(4 * n, vector<int>(k));
  function<void(int)> pull = [&](int node) {
    for (int i = 0; i < k; i++) {
      if (ll[node + node][i] != -1) {
        ll[node][i] = ll[node + node][i];
      } else {
        ll[node][i] = ll[node + node + 1][i];
      }
      if (rr[node + node + 1][i] != -1) {
        rr[node][i] = rr[node + node + 1][i];
      } else {
        rr[node][i] = rr[node + node][i];
      }
    }
    mn[node] = min(mn[node + node], mn[node + node + 1]);
    vector<pair<int, int>> ql, qr;
    for (int i = 0; i < k; i++) {
      if (rr[node + node][i] != -1) {
        ql.emplace_back(rr[node + node][i], i);
      }   
      if (ll[node + node + 1][i] != -1) {
        qr.emplace_back(ll[node + node + 1][i], i);
      }
    }
    sort(ql.begin(), ql.end());
    sort(qr.begin(), qr.end());
    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 (auto& p : ql) {
      Add(p.second);
    }
    int ptr = 0;
    for (auto& p : ql) {
      while (ptr < (int) qr.size() && d < k) {
        Add(qr[ptr].second);
        ptr += 1;  
      }
      if (d == k) {
        int b = (ptr == 0 ? ql.back().first : qr[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) {
      fill(ll[node].begin(), ll[node].end(), -1);
      fill(rr[node].begin(), rr[node].end(), -1);
      ll[node][a[l]] = rr[node][a[l]] = l;
      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);                                              
  };
  function<void(int, int, int, int)> modify = [&](int node, int l, int r, int i) {
    if (l == r) {
      fill(ll[node].begin(), ll[node].end(), -1);
      fill(rr[node].begin(), rr[node].end(), -1);
      ll[node][a[l]] = rr[node][a[l]] = l;
      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);
  };
  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:86:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |     int mid = l + r >> 1;
      |               ~~^~~
nekameleoni.cpp: In lambda function:
nekameleoni.cpp:99:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 3156 KB Output is correct
2 Correct 28 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 6100 KB Output is correct
2 Correct 81 ms 6124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 9552 KB Output is correct
2 Correct 179 ms 9556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1507 ms 38216 KB Output is correct
2 Execution timed out 3063 ms 130516 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2238 ms 100836 KB Output is correct
2 Execution timed out 3101 ms 174804 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2885 ms 77284 KB Output is correct
2 Execution timed out 3097 ms 150944 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3081 ms 121360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3043 ms 112496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3068 ms 185204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3094 ms 184944 KB Time limit exceeded
2 Halted 0 ms 0 KB -