답안 #759578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759578 2023-06-16T12:35:28 Z Sam_a17 The Potion of Great Power (CEOI20_potion) C++17
100 / 100
2040 ms 79300 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <atcoder/all>
#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 pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
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(unsigned long long t) {cerr << t;}
void print(long double 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> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} 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 random generations
// mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// 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};
 
// long long ka() {
// 	long long x = 0;
// 	bool z = false;
// 	while (1)
// 	{
// 		char y = getchar();
// 		if (y == '-')
// 			z = true;
// 		else if ('0' <= y && y <= '9')
// 			x = x * 10 + y - '0';
// 		else
// 		{
// 			if (z)
// 				x *= -1;
// 			return x;
// 		}
// 	}
// }
 
// 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);
  // }else {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "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 inf = 1e9;
const int N = 262149;
int n, d, h[N];
// int u, a[N], b[N];
int u;

int sz;
vector<pair<int, int>> mp[2 * N];
 
void init(int N, int D, int H[]) {
  n = N, d = D;
  for(int i = 0; i < n; i++) {
    h[i] = H[i];
  }
}
 
 
void upd(int l, int r, int xi, int yi, int x, int lx, int rx) {
  if(lx >= r || rx <= l) {
    return;
  }
  
  if(lx >= l && rx <= r) {
    mp[x].push_back({xi, yi});
    mp[x].push_back({yi, xi});
    return;
  }
 
  int mid = (lx + rx) / 2;
  upd(l, r, xi, yi, 2 * x + 1, lx, mid);
  upd(l, r, xi, yi, 2 * x + 2, mid, rx);
}
 
 
vector<int> stt[N];
// vector<int> sttt
 
void qry(int u, int xi, int x, int lx, int rx) {
  auto it = lower_bound(all(mp[x]), make_pair(xi, -1));
  while(it != mp[x].end() && it->first == xi) {
    stt[xi].push_back(h[it->second]);
    it = next(it);
  }

  if(rx - lx == 1) {
    return;
  }
 
  int mid = (lx + rx) / 2;
  if(u < mid) {
    qry(u, xi, 2 * x + 1, lx, mid);
  } else {
    qry(u, xi, 2 * x + 2, mid, rx);
  }
}
 
void iniit(int s) {
  sz = 1;
  while(sz < s) {
    sz *= 2;
  }
}
 
void curseChanges(int U, int A[], int B[]) {
  u = U;
  iniit(u);
 
  map<pair<int, int>, int> open;
  for(int i = 0; i < u; i++) {
    if(A[i] > B[i]) swap(A[i], B[i]);
 
    if(open.find({A[i], B[i]}) != open.end()) {
      upd(open[{A[i], B[i]}], i, A[i], B[i], 0, 0, sz);
      open.erase({A[i], B[i]});
    } else {
      open[{A[i], B[i]}] = i;
    }
  }
 
  for(auto i: open) {
    upd(i.second, u, i.first.first, i.first.second, 0, 0, sz);
  }

  for(int i = 0; i < 2 * sz; i++) {
    sort(all(mp[i]));
    uniq(mp[i]);
  }
}
 
int question(int x, int y, int v) {

  qry(v - 1, x, 0, 0, sz);
  qry(v - 1, y, 0, 0, sz);
 
  if(stt[x].empty() || stt[y].empty()) {
    {
      vector<int> emp;
      emp.swap(stt[x]);
    }
    {
      vector<int> emp;
      emp.swap(stt[y]);
    }
    return inf;
  } 

  //
  sort(all(stt[x]));
  sort(all(stt[y]));
 
  int ans = inf;
  auto it = 0;
  int lst = stt[y].back();
  
  for(auto i: stt[x]) {
    while(it < sz(stt[y]) && stt[y][it] <= i) {
      ans = min(ans, abs(stt[y][it] - i));
      it++;
    }
 
    if(it != sz(stt[y])) {
      ans = min(ans, abs(stt[y][it] - i));
    }
 
    if(it != 0) {
      ans = min(ans, abs(stt[y][it - 1] - i));
    }
 
    ans = min(ans, abs(i - lst));
  }
 
  {
    vector<int> emp;
    emp.swap(stt[x]);
  }
  {
    vector<int> emp;
    emp.swap(stt[y]);
  }
  return ans;
}

Compilation message

potion.cpp: In function 'void setIO(std::string)':
potion.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
potion.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 18712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19024 KB Output is correct
2 Correct 12 ms 18988 KB Output is correct
3 Correct 11 ms 18896 KB Output is correct
4 Correct 21 ms 19800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 728 ms 77248 KB Output is correct
2 Correct 749 ms 77244 KB Output is correct
3 Correct 316 ms 42724 KB Output is correct
4 Correct 1315 ms 67556 KB Output is correct
5 Correct 803 ms 76476 KB Output is correct
6 Correct 2013 ms 62468 KB Output is correct
7 Correct 696 ms 62312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 810 ms 79300 KB Output is correct
2 Correct 945 ms 47220 KB Output is correct
3 Correct 1022 ms 62312 KB Output is correct
4 Correct 1227 ms 62372 KB Output is correct
5 Correct 984 ms 76928 KB Output is correct
6 Correct 1276 ms 62328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 21320 KB Output is correct
2 Correct 140 ms 20044 KB Output is correct
3 Correct 124 ms 19728 KB Output is correct
4 Correct 533 ms 20992 KB Output is correct
5 Correct 525 ms 21160 KB Output is correct
6 Correct 146 ms 21200 KB Output is correct
7 Correct 422 ms 20176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 18712 KB Output is correct
2 Correct 13 ms 19024 KB Output is correct
3 Correct 12 ms 18988 KB Output is correct
4 Correct 11 ms 18896 KB Output is correct
5 Correct 21 ms 19800 KB Output is correct
6 Correct 728 ms 77248 KB Output is correct
7 Correct 749 ms 77244 KB Output is correct
8 Correct 316 ms 42724 KB Output is correct
9 Correct 1315 ms 67556 KB Output is correct
10 Correct 803 ms 76476 KB Output is correct
11 Correct 2013 ms 62468 KB Output is correct
12 Correct 696 ms 62312 KB Output is correct
13 Correct 810 ms 79300 KB Output is correct
14 Correct 945 ms 47220 KB Output is correct
15 Correct 1022 ms 62312 KB Output is correct
16 Correct 1227 ms 62372 KB Output is correct
17 Correct 984 ms 76928 KB Output is correct
18 Correct 1276 ms 62328 KB Output is correct
19 Correct 100 ms 21320 KB Output is correct
20 Correct 140 ms 20044 KB Output is correct
21 Correct 124 ms 19728 KB Output is correct
22 Correct 533 ms 20992 KB Output is correct
23 Correct 525 ms 21160 KB Output is correct
24 Correct 146 ms 21200 KB Output is correct
25 Correct 422 ms 20176 KB Output is correct
26 Correct 631 ms 43844 KB Output is correct
27 Correct 1215 ms 62424 KB Output is correct
28 Correct 1433 ms 73332 KB Output is correct
29 Correct 1332 ms 67664 KB Output is correct
30 Correct 2040 ms 62356 KB Output is correct
31 Correct 1610 ms 46428 KB Output is correct
32 Correct 1849 ms 62616 KB Output is correct