#include "seats.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
const int tree_siz = 1024*2048-1;
pii drzewo[tree_siz+1];
int oper[tree_siz+1];
pii poz[1'000'001];
vector<vector<pii>> corn = {{{0,-1},{1,-1},{1,0},{0,0}},{{0,1},{1,0},{1,1},{0,0}},{{0,-1},{-1,-1},{-1,0},{0,0}},{{0,1},{-1,0},{-1,1},{0,0}}};
int H,W;
vector<vi> arr;
void spych(int v)
{
drzewo[v*2].ff += oper[v];
drzewo[v*2+1].ff += oper[v];
oper[v*2] += oper[v];
oper[v*2+1] += oper[v];
oper[v] = 0;
}
pii merge(pii a, pii b)
{
if(a.ff != b.ff) return min(a,b);
return {a.ff,a.ss+b.ss};
}
void add_seg(int akt, int p1, int p2, int s1, int s2, int w)
{
if(p2 < s1 || p1 > s2) return;
if(p1 >= s1 && p2 <= s2)
{
drzewo[akt].ff += w;
oper[akt] += w;
return;
}
spych(akt);
add_seg(akt*2,p1,(p1+p2)/2,s1,s2,w);
add_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2,w);
drzewo[akt] = merge(drzewo[akt*2],drzewo[akt*2+1]);
}
int get_ans()
{
return drzewo[1].ss;
}
void change_val(int x, int y, int val)
{
forall(it,corn)
{
vi vals_sort;
forall(it2,it)
{
vals_sort.pb(arr[x+it2.ff][y+it2.ss]);
}
sort(all(vals_sort));
add_seg(1,0,tree_siz/2,vals_sort[0],vals_sort[1]-1,-1);
add_seg(1,0,tree_siz/2,vals_sort[2],vals_sort[3]-1,-1);
}
arr[x][y] = val;
forall(it,corn)
{
vi vals_sort;
forall(it2,it)
{
vals_sort.pb(arr[x+it2.ff][y+it2.ss]);
}
sort(all(vals_sort));
add_seg(1,0,tree_siz/2,vals_sort[0],vals_sort[1]-1,1);
add_seg(1,0,tree_siz/2,vals_sort[2],vals_sort[3]-1,1);
}
}
void give_initial_chart(int h, int w, vi R, vi C)
{
H = h;
W = w;
arr.resize(H+2,vi(W+2,H*W));
rep2(i,tree_siz/2+1,tree_siz)
{
drzewo[i] = {1e9,1};
}
for(int i = tree_siz/2; i >= 1; i--)
{
drzewo[i] = merge(drzewo[i*2],drzewo[i*2+1]);
}
add_seg(1,0,tree_siz/2,0,W*H-1,-1e9);
rep(i,H*W)
{
arr[R[i]+1][C[i]+1] = i;
poz[i] = {R[i]+1,C[i]+1};
}
rep2(i,1,H)
{
rep2(j,1,W)
{
vi vals_sort;
rep(k1,2)
{
rep(k2,2)
{
vals_sort.pb(arr[i+k1][j+k2]);
}
}
sort(all(vals_sort));
add_seg(1,0,tree_siz/2,vals_sort[0],vals_sort[1]-1,1);
add_seg(1,0,tree_siz/2,vals_sort[2],vals_sort[3]-1,1);
}
}
}
int swap_seats(int a, int b)
{
change_val(poz[a].ff,poz[a].ss,b);
change_val(poz[b].ff,poz[b].ss,a);
swap(poz[a],poz[b]);
return get_ans();
}
# | 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... |