Submission #980295

# Submission time Handle Problem Language Result Execution time Memory
980295 2024-05-12T03:49:08 Z Ghetto Werewolf (IOI18_werewolf) C++17
0 / 100
1469 ms 482160 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 2e5 + 5, MAX_N_NODES = 5e5 + 5;

int n, m;
unordered_map<int, unordered_map<int, int>> edge_ind;

int par[2][MAX_N_NODES], weight[2][MAX_N_NODES];
vector<int> children[2][MAX_N_NODES];
int find_par(int ind, int u) {
    if (par[ind][u] == u) return u;
    par[ind][u] = find_par(ind, par[ind][u]);
    return par[ind][u];
}
void merge(int ind, int u, int v, int w) {
    int edge = edge_ind[u][v];
    // cout << u << " " << v << ": " << edge << endl;
    u = find_par(ind, u), v = find_par(ind, v);
    if (u == v) return;
    weight[ind][edge] = w;
    par[ind][u] = par[ind][v] = par[ind][edge] = edge;
    for (int x : {u, v}) children[ind][edge].push_back(x);
}
void precomp1(int ind, vector<pair<int, pii>> edges) {
    for (int u = 0; u < n; u++) par[ind][u] = u;
    for (pair<int, pii> edge : edges) merge(ind, edge.second.first, edge.second.second, edge.first);
    // if (ind == 1) {
    //     for (int u = 0; u < n + m; u++) {
    //         if (u >= n && children[ind][u].empty()) continue;
    //         cout << u << " " << weight[ind][u] << ": ";
    //         for (int v : children[ind][u]) cout << v << " ";
    //         cout << endl;
    //     }
    // }
}

int n_ids[2], id[2][MAX_N_NODES], rev_id[2][MAX_N_NODES], f_id[2][MAX_N_NODES];
int anc[2][MAX_N_NODES][22];
void dfs(int ind, int u) {
    n_ids[ind]++, id[ind][u] = n_ids[ind], rev_id[ind][n_ids[ind]] = u;
    for (int v : children[ind][u]) dfs(ind, v), anc[ind][v][0] = u;
    f_id[ind][u] = n_ids[ind];
}
int bin_lift(int ind, int u, int x) { // Should never be a problem that weight for < n uninited and that 0 is possible node
    for (int i = 19; i >= 0; i--) {
        if (anc[ind][u][i] == 0) continue;
        if (ind == 0 && weight[ind][anc[ind][u][i]] < x) continue;
        if (ind == 1 && weight[ind][anc[ind][u][i]] > x) continue;
        u = anc[ind][u][i];
    }
    return u;
}
void precomp2(int ind) {
    int top = -1;
    for (int u = n; u < n + m; u++)
        if (find_par(ind, u) == u) top = u;
    dfs(ind, top);
    for (int i = 1; i <= 19; i++)
        for (int u = 0; u < n + m; u++)
            anc[ind][u][i] = anc[ind][anc[ind][u][i - 1]][i - 1];
    // if (ind == 0 || ind == 1) {
    //     for (int u = 0; u < n + m; u++) {
    //         if (u >= n && children[ind][u].empty()) continue;
    //         cout << u << ": " << id[ind][u] << endl;
    //     }
    // }
}

struct Node {
    int val;
    Node* l_child, * r_child;
    Node(int x) {
        val = x, l_child = r_child = nullptr;
    }
    Node(Node* l, Node* r) {
        val = l->val + r->val, l_child = l, r_child = r;
    }
};
Node* root[MAX_N_NODES];
Node* build(int lo = 1, int hi = n_ids[0]) {
    if (lo == hi) return new Node(0);
    int mid = (lo + hi) / 2;
    return new Node(build(lo, mid), build(mid + 1, hi));
}
Node* update(int i, Node* u, int lo = 1, int hi = n_ids[0]) {
    if (i < lo || i > hi) return nullptr;
    if (lo == hi) return new Node(1);
    int mid = (lo + hi) / 2;
    Node* l_resp = update(i, u->l_child, lo, mid), * r_resp = update(i, u->r_child, mid + 1, hi);
    return new Node((l_resp) ? l_resp : u->l_child, (r_resp) ? r_resp : u->r_child);
}
int query(int l, int r, Node* u, int lo = 1, int hi = n_ids[0]) {
    if (l > hi || r < lo) return 0;
    if (l <= lo && hi <= r) return u->val;
    int mid = (lo + hi) / 2;
    return query(l, r, u->l_child, lo, mid) + query(l, r, u->r_child, mid + 1, hi);
}
int query2d(int l, int r, int lo, int hi) {
    return query(lo, hi, root[r]) - query(lo, hi, root[l - 1]);
}
void precomp3() {
    root[0] = build();
    for (int i = 1; i <= n_ids[0]; i++) {
        int u = rev_id[0][i];
        if (u >= n && children[1][u].empty()) root[i] = root[i - 1];
        else root[i] = update(id[1][rev_id[0][i]], root[i - 1]); 
    }
    // for (int i = 1; i <= n_ids[0]; i++) cout << rev_id[0][i] << " ";
    // cout << endl;
    // for (int i = 1; i <= n_ids[1]; i++) cout << rev_id[1][i] << " ";
    // cout << endl;

    // cout << query2d(8, 10, 1, 9) << endl;
}

vector<int> check_validity(int tmp_n, vector<int> edge1, vector<int> edge2, vector<int> s, vector<int> f, vector<int> l, vector<int> r) {
    n = tmp_n, m = edge1.size();
    vector<pair<int, pii>> edges[2]; // 0 = desc, 1 = asc
    for (int i = 0; i < edge1.size(); i++) {
        edge_ind[edge1[i]][edge2[i]] = edge_ind[edge2[i]][edge1[i]] = n + i;
        edges[0].push_back({min(edge1[i], edge2[i]), {edge1[i], edge2[i]}});
        edges[1].push_back({max(edge1[i], edge2[i]), {edge1[i], edge2[i]}});
    }       
    sort(edges[0].rbegin(), edges[0].rend()), sort(edges[1].begin(), edges[1].end());
    
    for (int i = 0; i <= 1; i++) precomp1(i, edges[i]);
    for (int i = 0; i <= 1; i++) precomp2(i);
    precomp3();
    
    vector<int> ans;
    for (int tc = 0; tc < s.size(); tc++) {
        int u = bin_lift(0, s[tc], l[tc]), v = bin_lift(1, f[tc], r[tc]);
        // cout << u << " " << v << endl;
        bool new_ans = query2d(id[0][u], f_id[0][u], id[1][v], f_id[1][v]);
        ans.push_back(new_ans);
    }
    return ans;
}

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (int i = 0; i < edge1.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
werewolf.cpp:133:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     for (int tc = 0; tc < s.size(); tc++) {
      |                      ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 47708 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 47708 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1469 ms 482160 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 47708 KB Output isn't correct
2 Halted 0 ms 0 KB -