답안 #713217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713217 2023-03-21T10:06:16 Z Pety The Potion of Great Power (CEOI20_potion) C++14
100 / 100
2670 ms 48372 KB
#include <bits/stdc++.h>
#define ll long long
 
using namespace std;
 
const int INF = 1e9;
const int MOD = 1e9 + 7;
 
int H[200002], N, lg2[200002];
 
struct interval {
  int l, r, ind;
  bool operator < (const interval &other) {
    return l < other.l;
  }
};
 
vector<interval>v[200002];
 
void init (int n, int d, int h[]) {
  N = n;
  for (int i = 1; i <= n; i++)
    H[i] = h[i - 1];
}
 
vector<vector<int>>rmq[200002];
int uwu;
 
void curseChanges(int U, int A[], int B[]) {
  uwu = U;
  map<pair<int, int>, int>mp;
  for (int i = 0; i < U; i++) {
    A[i]++;
    B[i]++;
    if (A[i] > B[i])
      swap(A[i], B[i]);
    if (mp[{A[i], B[i]}] == 0) {
      mp[{A[i], B[i]}] = i + 1;
    }
    else {
      v[A[i]].push_back({mp[{A[i], B[i]}], i + 1, B[i]});
      v[B[i]].push_back({mp[{A[i], B[i]}], i + 1, A[i]});
      mp[{A[i], B[i]}] = 0;
    }
  }
  for (auto it : mp) {
    if (it.second) {
      v[it.first.first].push_back({it.second, U + 1, it.first.second});
      v[it.first.second].push_back({it.second, U + 1, it.first.first});
    }
  }
  for (int i = 1; i <= N; i++)
    sort(v[i].begin(), v[i].end());
  for (int i = 2; i <= U; i++)
    lg2[i] = lg2[i / 2] + 1;
  for (int i = 1; i <= N; i++) {
    int k = lg2[v[i].size()];
    rmq[i].resize(k + 1);
    for (int j = 0; j <= k; j++)
      rmq[i][j].resize(v[i].size());
    for (int j = 0; j < v[i].size(); j++)
      rmq[i][0][j] = j;
    for (int j = 1; j <= k; j++)
      for (int p = 0; p + (1 << j) - 1 < v[i].size(); p++) {
        int ind1 = rmq[i][j - 1][p];
        int ind2 = rmq[i][j - 1][p + (1 << (j - 1))];
        if (v[i][ind1].r > v[i][ind2].r)
          rmq[i][j][p] = ind1;
        else
          rmq[i][j][p] = ind2;
      }
  }
}
 
vector<int> ans;
 
int query (int shaman, int st, int dr) {
  int l = lg2[dr - st + 1];
  if (v[shaman][rmq[shaman][l][st]].r > v[shaman][rmq[shaman][l][dr - (1 << l) + 1]].r)
    return rmq[shaman][l][st];
  else 
    return rmq[shaman][l][dr - (1 << l) + 1];
}
 
void rec(int shaman, int st, int dr, int day) {
  if (st > dr)
    return;
  int ind = query(shaman, st, dr);
  if (v[shaman][ind].r > day)
    ans.push_back(H[v[shaman][ind].ind]);
  else
    return;
  rec(shaman, st, ind - 1, day);
  rec(shaman, ind + 1, dr, day);
}
 
vector<int> find_list (int shaman, int day) {
  int st = 0, dr = v[shaman].size() - 1, lim = -1;
  while (st <= dr) {
    int mid = (st + dr) / 2;
    if (v[shaman][mid].l <= day) {
      lim = mid;
      st = mid + 1;
    }
    else
      dr = mid - 1;
  }
  ans.clear();
  rec(shaman, 0, lim, day);
  return ans;
}
 
int question (int X, int Y, int day) {
  X++;
  Y++;
  vector<int> D1 = find_list(X, day);
  vector<int> D2 = find_list(Y, day);
  sort(D1.begin(), D1.end());
  sort(D2.begin(), D2.end());
  int j = 0;
  int ans = INF;
  for (int i = 0; i < D1.size(); i++) {
    while (j < D2.size() && D2[j] < D1[i])
      j++;
    if (j < D2.size())
      ans = min(ans, D2[j] - D1[i]);
    if (j > 0)
      ans = min(ans, D1[i] - D2[j - 1]);   
  }
  return ans;
}

Compilation message

potion.cpp: In function 'void curseChanges(int, int*, int*)':
potion.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<interval>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int j = 0; j < v[i].size(); j++)
      |                     ~~^~~~~~~~~~~~~
potion.cpp:64:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<interval>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |       for (int p = 0; p + (1 << j) - 1 < v[i].size(); p++) {
      |                       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:122:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |   for (int i = 0; i < D1.size(); i++) {
      |                   ~~^~~~~~~~~~~
potion.cpp:123:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |     while (j < D2.size() && D2[j] < D1[i])
      |            ~~^~~~~~~~~~~
potion.cpp:125:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |     if (j < D2.size())
      |         ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9936 KB Output is correct
2 Correct 6 ms 9940 KB Output is correct
3 Correct 6 ms 9936 KB Output is correct
4 Correct 24 ms 13824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 338 ms 48280 KB Output is correct
2 Correct 342 ms 48372 KB Output is correct
3 Correct 180 ms 27136 KB Output is correct
4 Correct 1400 ms 37800 KB Output is correct
5 Correct 472 ms 43496 KB Output is correct
6 Correct 2623 ms 37040 KB Output is correct
7 Correct 572 ms 35332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 309 ms 48200 KB Output is correct
2 Correct 1593 ms 30672 KB Output is correct
3 Correct 909 ms 37172 KB Output is correct
4 Correct 1464 ms 37128 KB Output is correct
5 Correct 399 ms 48120 KB Output is correct
6 Correct 1665 ms 37088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 11856 KB Output is correct
2 Correct 74 ms 10996 KB Output is correct
3 Correct 136 ms 10704 KB Output is correct
4 Correct 784 ms 11472 KB Output is correct
5 Correct 758 ms 11756 KB Output is correct
6 Correct 91 ms 11332 KB Output is correct
7 Correct 685 ms 10832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 6 ms 9936 KB Output is correct
3 Correct 6 ms 9940 KB Output is correct
4 Correct 6 ms 9936 KB Output is correct
5 Correct 24 ms 13824 KB Output is correct
6 Correct 338 ms 48280 KB Output is correct
7 Correct 342 ms 48372 KB Output is correct
8 Correct 180 ms 27136 KB Output is correct
9 Correct 1400 ms 37800 KB Output is correct
10 Correct 472 ms 43496 KB Output is correct
11 Correct 2623 ms 37040 KB Output is correct
12 Correct 572 ms 35332 KB Output is correct
13 Correct 309 ms 48200 KB Output is correct
14 Correct 1593 ms 30672 KB Output is correct
15 Correct 909 ms 37172 KB Output is correct
16 Correct 1464 ms 37128 KB Output is correct
17 Correct 399 ms 48120 KB Output is correct
18 Correct 1665 ms 37088 KB Output is correct
19 Correct 45 ms 11856 KB Output is correct
20 Correct 74 ms 10996 KB Output is correct
21 Correct 136 ms 10704 KB Output is correct
22 Correct 784 ms 11472 KB Output is correct
23 Correct 758 ms 11756 KB Output is correct
24 Correct 91 ms 11332 KB Output is correct
25 Correct 685 ms 10832 KB Output is correct
26 Correct 861 ms 30776 KB Output is correct
27 Correct 1331 ms 37136 KB Output is correct
28 Correct 1477 ms 44512 KB Output is correct
29 Correct 1279 ms 37760 KB Output is correct
30 Correct 2670 ms 37372 KB Output is correct
31 Correct 2572 ms 30804 KB Output is correct
32 Correct 2341 ms 37204 KB Output is correct