Submission #969364

#TimeUsernameProblemLanguageResultExecution timeMemory
969364steveonalexLand of the Rainbow Gold (APIO17_rainbow)C++17
100 / 100
797 ms214232 KiB
#include <bits/stdc++.h> #include "rainbow.h" using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} 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);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct PST{ struct Node{ int child[2]; int sum; Node(){memset(child, -1, sizeof child); sum = 0;} }; int L, R; vector<Node> a; vector<int> version; void add_child(int &x){ x = a.size(); a.push_back(Node()); } PST(int _l, int _r){ L = _l, R = _r; a.push_back(Node()); version.push_back(0); build_tree(L, R, 0); } void build_tree(int l, int r, int id){ if (l == r) return; int mid = (l + r) >> 1; add_child(a[id].child[0]); add_child(a[id].child[1]); build_tree(l, mid, a[id].child[0]); build_tree(mid + 1, r, a[id].child[1]); } void update(int i, int v, int l, int r, int id, int prev_id){ a[id] = a[prev_id]; a[id].sum += v; if (l == r) return; int mid = (l + r) >> 1; if (i <= mid){ add_child(a[id].child[0]); update(i, v, l, mid, a[id].child[0], a[prev_id].child[0]); } else{ add_child(a[id].child[1]); update(i, v, mid + 1, r, a[id].child[1], a[prev_id].child[1]); } } void update(int i, int v){ int prev = version.back(); version.push_back(0); add_child(version.back()); update(i, v, L, R, version.back(), prev); } int get(int u, int v, int l, int r, int id){ if (u <= l && r <= v) return a[id].sum; int mid = (l + r) >> 1; int ans = 0; if (u <= mid) ans += get(u, v, l, mid, a[id].child[0]); if (v > mid) ans += get(u, v, mid + 1, r, a[id].child[1]); return ans; } int get(int l, int r, int ver){ if (l > r) return 0; int id = version[ver]; return get(l, r, L, R, id); } int get_version(){return version.size();} }; const int INF = 1e9 + 69; const int N = 2e5 + 69; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; #define Point pair<int, int> vector<Point> dragon, verti_edge, hori_edge, vertices; int xL, xR, yL, yR; int cur_face; PST dragon_cnt(1, N), vertices_cnt(1, N), verti_edge_cnt(1, N), hori_edge_cnt(1, N); vector<int> dragon_row, vertices_row, verti_edge_row, hori_edge_row; void init(int n, int m, int sr, int sc, int k, char *S) { Point coord = {sr, sc}; dragon.push_back(coord); for(int i = 0; i<k; ++i){ if (S[i] == 'N') coord.first--; else if (S[i] == 'S') coord.first++; else if (S[i] == 'W') coord.second--; else coord.second++; dragon.push_back(coord); } remove_dup(dragon); for(pair<int, int> i: dragon){ for(int d = 0; d < 4; ++d){ if (d <= 1) hori_edge.push_back({i.first - (d == 0), i.second}); else verti_edge.push_back({i.first, i.second - (d == 2)}); } } remove_dup(hori_edge); remove_dup(verti_edge); for(pair<int, int> i: hori_edge) { vertices.push_back({i.first, i.second-1}); vertices.push_back({i.first, i.second}); } for(pair<int, int> i: verti_edge){ vertices.push_back({i.first-1, i.second}); vertices.push_back({i.first, i.second}); } remove_dup(vertices); /*---------------------------------------*/ dragon_row.push_back(0); vector<vector<int>> sigma(N); for(pair<int, int> i: dragon) sigma[i.first].push_back(i.second); for(int i = 1; i<N; ++i){ for(int j: sigma[i]) dragon_cnt.update(j, 1); dragon_row.push_back(dragon_cnt.get_version() - 1); } /*---------------------------------------*/ for(int i = 0; i<N; ++i) sigma[i].clear(); vertices_row.push_back(0); for(pair<int, int> i: vertices) if (i.first > 0 && i.second > 0) sigma[i.first].push_back(i.second); for(int i = 1; i<N; ++i){ for(int j: sigma[i]) vertices_cnt.update(j, 1); vertices_row.push_back(vertices_cnt.get_version() - 1); } /*---------------------------------------*/ for(int i = 0; i<N; ++i) sigma[i].clear(); hori_edge_row.push_back(0); for(pair<int, int> i: hori_edge) if (i.first > 0 && i.second > 0) sigma[i.first].push_back(i.second); for(int i = 1; i<N; ++i){ for(int j: sigma[i]) hori_edge_cnt.update(j, 1); hori_edge_row.push_back(hori_edge_cnt.get_version() - 1); } /*---------------------------------------*/ for(int i = 0; i<N; ++i) sigma[i].clear(); verti_edge_row.push_back(0); for(pair<int, int> i: verti_edge) if (i.first > 0 && i.second > 0) sigma[i.first].push_back(i.second); for(int i = 1; i<N; ++i){ for(int j: sigma[i]) verti_edge_cnt.update(j, 1); verti_edge_row.push_back(verti_edge_cnt.get_version() - 1); } /*---------------------------------------*/ cur_face = 2 + hori_edge.size() + verti_edge.size() - vertices.size() - dragon.size(); xL = yL = INF; xR = yR = 0; for(pair<int, int> i: dragon){ maximize(xR, i.first); maximize(yR, i.second); minimize(xL, i.first); minimize(yL, i.second); } } int colour(int ar, int ac, int br, int bc) { if (ar < xL && ac < yL && br > xR && bc > yR) return cur_face; int v_cnt = ((br - ar) + (bc - ac)) * 2, e_cnt = v_cnt; int block_cnt = dragon_cnt.get(ac, bc, dragon_row[br]) - dragon_cnt.get(ac, bc, dragon_row[ar - 1]); v_cnt += vertices_cnt.get(ac, bc - 1, vertices_row[br-1]) - vertices_cnt.get(ac, bc - 1, vertices_row[ar-1]); e_cnt += hori_edge_cnt.get(ac, bc, hori_edge_row[br-1]) - hori_edge_cnt.get(ac, bc, hori_edge_row[ar-1]); e_cnt += verti_edge_cnt.get(ac, bc-1, verti_edge_row[br]) - verti_edge_cnt.get(ac, bc-1, verti_edge_row[ar-1]); return 1 + e_cnt - v_cnt - block_cnt; } // int main(void){ // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.inp", "r", stdin); // int n, m, k; cin >> n >> m; // int sr, sc; cin >> sr >> sc; // cin >> k; // string s; cin >> s; // init(n, m, sr, sc, k, (char*) s.c_str()); // int q; cin >> q; // while(q--) { // int a, b, c, d; cin >> a >> b >> c >> d; // cout << colour(a, b, c, d) << "\n"; // } // return 0; // }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...