이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int min(int &a, int &b){
if(a < b) return a;
return b;
}
inline int max(int &a, int &b){
if(a < b) return b;
return a;
}
struct node{
node *l, *r;
pair<int, int> mini;
pair<int, int> maxi;
void update(){
mini.first = min(l->mini.first, r->mini.first);
mini.second = min(l->mini.second, r->mini.second);
maxi.first = max(l->maxi.first, r->maxi.first);
maxi.second = max(l->maxi.second, r->maxi.second);
}
};
vector< pair<int, int> > v;
void Build(node* root, int left, int right){
if(left == right){
root->maxi.second = root->mini.second = v[left-1].second;
root->maxi.first = root->mini.first = v[left-1].first;
return;
}
int mid = (left+right)/2;
root->l = new node{NULL,NULL,{0,0},{0,0}};
root->r = new node{NULL,NULL,{0,0},{0,0}};
Build(root->l, left, mid);
Build(root->r, mid+1, right);
root->update();
}
void update(node* root, int left, int right, int index, pair<int, int> p){
if(left > index || right < index) return;
if(left == index && right == index){
root->maxi.second = root->mini.second = p.second;
root->maxi.first = root->mini.first = p.first;
return;
}
int mid = (left+right)/2;
update(root->l, left, mid, index, p);
update(root->r, mid+1, right, index, p);
root->update();
}
pair<int, int> queryMin(node* root, int left, int right, int qLeft, int qRight){
if(left > qRight || right < qLeft) return {INT_MAX, INT_MAX};
if(left >= qLeft && right <= qRight){
return root->mini;
}
int mid = (left+right)/2;
pair<int, int> p = queryMin(root->l, left, mid, qLeft, qRight);
pair<int, int> q = queryMin(root->r, mid+1, right, qLeft, qRight);
return {min(p.first, q.first), min(p.second, q.second)};
}
pair<int, int> queryMax(node* root, int left, int right, int qLeft, int qRight){
if(left > qRight || right < qLeft) return {INT_MIN, INT_MIN};
if(left >= qLeft && right <= qRight){
return root->maxi;
}
int mid = (left+right)/2;
pair<int, int> p = queryMax(root->l, left, mid, qLeft, qRight);
pair<int, int> q = queryMax(root->r, mid+1, right, qLeft, qRight);
return {max(p.first, q.first), max(p.second, q.second)};
}
int h, w;
node* root = new node{NULL,NULL,{0,0},{0,0}};
#undef int
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
#define int long long
h = H;
w = W;
v.resize(h*w);
for(int i = 0; i < H*W; i++){
v[i] = make_pair(R[i], C[i]);
}
Build(root, 1, H*W);
}
#undef int
int swap_seats(int a, int b) {
#define int long long
update(root, 1, h*w, a+1, v[b]);
update(root, 1, h*w, b+1, v[a]);
swap(v[b], v[a]);
int curr = 1;
int ans = 0;
while(curr <= h*w){
pair<int, int> mini = queryMin(root, 1, h*w, 1, curr);
pair<int, int> maxi = queryMax(root, 1, h*w, 1, curr);
//if(curr == 5) break;
if((maxi.first - mini.first + 1)*(maxi.second - mini.second + 1) == curr){
ans++;
curr++;
}else{
assert((maxi.first - mini.first + 1)*(maxi.second - mini.second + 1) > curr);
curr = (maxi.first - mini.first + 1)*(maxi.second - mini.second + 1);
}
//cout << ans << endl;
}
return ans;
#undef int
}
# | 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... |