이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "seats.h"
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll gcd(ll a, ll b){return __gcd(a, b);}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T the_array_itself, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: the_array_itself) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct SegmentTree{
int n;
vector<pair<int,int>> a;
vector<int> lazy;
SegmentTree(int _n){
n = _n;
a.resize(n * 4 + 4);
lazy.resize(n * 4 + 4);
build_tree(0, n-1, 1);
}
pair<int, int> combine(pair<int, int> a, pair<int, int> b){
pair<int, int> c = {min(a.first, b.first), 0};
if (a.first == c.first) c.second += a.second;
if (b.first == c.first) c.second += b.second;
return c;
}
void build_tree(int l, int r, int id){
if (l == r){a[id] = {0, 1}; return;}
int mid = (l + r) >> 1;
build_tree(l, mid, id * 2); build_tree(mid + 1, r, id * 2 + 1);
a[id] = combine(a[id * 2], a[id * 2 + 1]);
}
void apply(int id, int val){
a[id].first += val;
lazy[id] += val;
}
void down(int id){
apply(id * 2, lazy[id]); apply(id * 2 + 1, lazy[id]);
lazy[id] = 0;
}
void update(int u, int v, int val, int l, int r, int id){
if (u <= l && r <= v){
apply(id, val);
return;
}
down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, val, l, mid, id * 2);
if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
a[id] = combine(a[id * 2], a[id * 2 + 1]);
}
void update(int l, int r, int val){
update(l, r, val, 0, n-1, 1);
}
int get(){
return a[1].second;
}
};
const int INF = 1e9 + 69;
int h, w;
vector<int> r, c;
vector<int> a;
SegmentTree st(1);
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
h = H, w = W;
r = R; c = C;
if (h != 1) exit(1);
st = SegmentTree(w);
a.resize(w);
for(int i = 0; i < w; ++i) {
a[c[i]] = i;
st.update(i, i, i + 1);
}
for(int i = 0; i < w; ++i){
if (i > 0 && a[i-1] < a[i]) st.update(a[i], w - 1, -1);
if (i + 1 < w && a[i+1] < a[i]) st.update(a[i], w - 1, -1);
}
}
void update(int i, int val){
if (i - 1 >= 0 && a[i-1] != -1){
st.update(max(a[i], a[i-1]), w-1, -val);
}
if (i + 1 < w && a[i+1] != -1){
st.update(max(a[i], a[i+1]), w-1, -val);
}
}
int swap_seats(int x, int y) {
int p1 = c[x], p2 = c[y];
swap(c[x], c[y]);
update(p1, -1);
update(p2, -1);
swap(a[p1], a[p2]);
update(p2, 1);
update(p1, 1);
return st.get();
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n, m;cin >> n >> m;
// vector<int> r(n * m), c(n*m);
// for(int i = 0; i < n*m; ++i) cin >> r[i] >> c[i];
// give_initial_chart(n, m, r, c);
// int q; cin >> q;
// while(q--){
// int u, v; cin >> u >> v;
// cout << swap_seats(u, v) << "\n";
// }
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
// return 0;
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |