# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
80622 |
2018-10-21T18:42:09 Z |
qkxwsm |
Seats (IOI18_seats) |
C++14 |
|
0 ms |
0 KB |
#include "seats.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
struct custom_hash
{
template<class T>
unsigned long long operator()(T v) const
{
unsigned long long x = v;
x += FIXED_RANDOM; x += 11400714819323198485ull;
x = (x ^ (x >> 30)) * 13787848793156543929ull;
x = (x ^ (x >> 27)) * 10723151780598845931ull;
return x ^ (x >> 31);
}
};
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;
template<class T>
void ckmin(T &a, T b)
{
a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
T nmod(T &x, U mod)
{
if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
return (b ? gcd(b, a % b) : a);
}
template<class T>
T randomize(T mod)
{
return (uniform_int_distribution<T>(0, mod - 1))(rng);
}
#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;
#define sz(x) ((int) (x.size()))
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;
#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 1000013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef pair<pii, pii> ppp;
typedef pair<pii, int> ppi;
int N, M, K;
pii coor[MAXN];
vector<vector<int> > grid;
pii operator + (const pii &a, const pii &b)
{
return {{a.fi + b.fi}, {a.se + b.se}};
}
struct segtree
{
pii lazy[3 * MAXN];
ppi stor[3 * MAXN];
ppi comb(ppi a, ppi b)
{
ppi res = {{0, 0}, 0};
if (a.fi <= b.fi)
{
res.fi = a.fi;
res.se += a.se;
}
if (b.fi <= a.fi)
{
res.fi = b.fi;
res.se += b.se;
}
return res;
}
void build(int w, int L, int R)
{
stor[w] = {{0, 0}, 1};
lazy[w] = {0, 0};
if (L == R) return;
int mid = (L + R) >> 1;
build(w << 1, L, mid);
build(w << 1 | 1, mid + 1, R);
stor[w] = comb(stor[w << 1], stor[w << 1 | 1]);
cerr << L << ' ' << R << ' ' << stor[w].fi.fi << ' ' << stor[w].fi.se << ' ' << stor[w].se << endl;
}
void push(int w, int L, int R)
{
stor[w].fi = stor[w].fi + lazy[w];
if (L != R)
{
lazy[w << 1] = lazy[w << 1] + lazy[w];
lazy[w << 1 | 1] = lazy[w << 1 | 1] + lazy[w];
}
lazy[w] = {0, 0};
}
void update(int w, int L, int R, int a, int b, pii v)
{
push(w, L, R);
if (b < L || R < a) return;
if (a <= L && R <= b)
{
lazy[w] = lazy[w] + v;
push(w, L, R);
return;
}
int mid = (L + R) >> 1;
update(w << 1, L, mid, a, b, v);
update(w << 1 | 1, mid + 1, R, a, b, v);
stor[w] = comb(stor[w << 1], stor[w << 1 | 1]);
}
void upd(int l, int r, pii p)
{
ckmin(r, K - 1);
ckmax(l, 0);
if (l > r) return;
update(1, 0, K - 1, l, r, p);
}
};
segtree seg;
array<int, 4> sort4(int a, int b, int c, int d)
{
array<int, 4> arra = {a, b, c, d};
sort(arra.begin(), arra.end());
return arra;
}
void give_initial_chart(int h, int w, vector<int> R, vector<int> C)
{
N = h + 2;
M = w + 2;
K = w * h;
grid.resize(N);
int cc = K;
for (int i = 0; i < N; i++)
{
grid[i].resize(M);
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (i == 0 || i == N - 1 || j == 0 || j == M - 1)
{
grid[i][j] = cc; cc++;
}
}
}
for (int i = 0; i < K; i++)
{
R[i]++; C[i]++;
coor[i] = {R[i], C[i]};
grid[R[i]][C[i]] = i;
}
seg.build(1, 0, K - 1);
for (int i = 0; i < N - 1; i++)
{
for (int j = 0; j < M - 1; j++)
{
array<int, 4> p = sort4(grid[i][j], grid[i + 1][j], grid[i][j + 1], grid[i + 1][j + 1]);
seg.upd(p[2], p[3] - 1, {1, 0});
seg.upd(p[0], p[1] - 1, {0, 1});
}
}
// cerr << "what the\n";
// cerr << seg.query(1, 0, M - 1, 0, M - 1).se << endl;
// cerr << "wtf\n";
//for each range (L, R), you want to store: (size of range) (# of edges entirely in this range) and if it's -1, you win
//this is great, but what do you delete an edge?
}
int swap_seats(int a, int b)
{
pii p0 = coor[a], p1 = coor[b];
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x = p0.fi + i, y = p0.se + j;
array<int, 4> p = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
seg.upd(p[2], p[3] - 1, {-1, 0});
seg.upd(p[0], p[1] - 1, {0, -1});
}
}
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x = p1.fi + i, y = p1.se + j;
array<int, 4> p = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
seg.upd(p[2], p[3] - 1, {-1, 0});
seg.upd(p[0], p[1] - 1, {0, -1});
}
}
swap(coor[a], coor[b]);
swap(grid[p0.fi][p0.se], grid[p1.fi][p1.se]);
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x = p0.fi + i, y = p0.se + j;
array<int, 4> p = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
seg.upd(p[2], p[3] - 1, {1, 0});
seg.upd(p[0], p[1] - 1, {0, 1});
}
}
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
int x = p1.fi + i, y = p1.se + j;
array<int, 4> p = sort4(grid[x][y], grid[x + 1][y], grid[x][y + 1], grid[x + 1][y + 1]);
seg.upd(p[2], p[3] - 1, {1, 0});
seg.upd(p[0], p[1] - 1, {0, 1});
}
}
seg.push(1, 0, K - 1);
return seg.stor[1].se;
}
Compilation message
Compilation timeout while compiling seats