Submission #1081985

# Submission time Handle Problem Language Result Execution time Memory
1081985 2024-08-30T13:57:19 Z cpptowin Spring cleaning (CEOI20_cleaning) C++17
0 / 100
281 ms 38484 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if (x < 0)
        x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if (x < 0)
        x += mod;
}
struct SegmentTree
{
    vector<int> st, lazy;
    int n;
    stack<pair<int,int>> event;
    /// real n
    SegmentTree(int _n = 0) : n(_n)
    {
        st.resize(4 * n + 10);
        lazy.resize(4 * n + 10);
    }
    void resize(int _n)
    {
        n = _n;
        st.resize(4 * n + 10);
        lazy.resize(4 * n + 10);
    }
    void down(int id, int l, int r)
    {
        if (lazy[id] == 0)
            return;
        int mid = l + r >> 1;
        st[id << 1] = (mid - l + 1) - st[id << 1];
        lazy[id << 1] ^= 1;
        st[id << 1 | 1] = (r - mid) - st[id << 1 | 1];
        lazy[id << 1 | 1] ^= 1;
        lazy[id] = 0;
    }
    void build(int id, int l, int r)
    {
        if (l == r)
        {
            st[id] = r - l + 1;
            return;
        }
        int mid = l + r >> 1;
        build(id << 1, l, mid);
        build(id << 1 | 1, mid + 1, r);
        st[id] = st[id << 1] + st[id << 1 | 1];
    }
    void update(int id, int l, int r, int u, int v)
    {
        if (u > r || l > v)
            return;
        if (u <= l && r <= v)
        {
            st[id] = (r - l + 1) - st[id];
            lazy[id] ^= 1;
            return;
        }
        down(id, l, r);
        int mid = (l + r) >> 1;
        update(id << 1, l, mid, u, v);
        update(id << 1 | 1, mid + 1, r, u, v);
        st[id] = st[id << 1] + st[id << 1 | 1];
    }

    int get(int id, int l, int r, int u, int v)
    {
        if (l > v || u > r)
            return 0;
        if (u <= l && r <= v)
            return st[id];
        down(id, l, r);
        int mid = (l + r) >> 1;
        return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
    }
    int get(int l, int r)
    {
        return get(1, 1, n, l, r);
    }
    void update(int l, int r)
    {
        event.push({l,r});
        update(1, 1, n, l, r);
    }
    void rollback(int i)
    {
        while(ss(event) > i) 
        {
            auto [l,r] = event.top();
            update(1,1,n,l,r);
            event.pop();
        }
    }
} st(maxn);
vi ke[maxn];
int n, q;
int par[maxn][20];
int h[maxn];
int head[maxn], ind[maxn], pos[maxn], sz[maxn];
int cnt, chaincnt;
void dfs(int u)
{
    sz[u] = 1;
    for (int v : ke[u])
    {
        if (v == par[u][0])
            continue;
        h[v] = h[u] + 1;
        par[v][0] = u;
        fo(i, 1, 19) par[v][i] = par[par[v][i - 1]][i - 1];
        dfs(v);
        sz[u] += sz[v];
    }
}
void hld(int u)
{
    if (head[chaincnt] == 0)
        head[chaincnt] = u;
    ind[u] = chaincnt;
    pos[u] = ++cnt;
    int maxx = 0, sc = -1;
    for (int v : ke[u])
    {
        if (v == par[u][0])
            continue;
        if (sz[v] > maxx)
        {
            maxx = sz[v], sc = v;
        }
    }
    if (sc != -1)
        hld(sc);
    for (int v : ke[u])
        if (v != sc && v != par[u][0])
        {
            ++chaincnt;
            hld(v);
        }
}
int lca(int u, int v)
{
    if (h[u] < h[v])
        swap(u, v);
    int del = h[u] - h[v];
    fod(i, 19, 0) if (bit(del, i)) u = par[u][i];
    if (u == v)
        return u;
    fod(i, 19, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
    return par[u][0];
}
void change(int u, int v)
{
    int uchain, vchain = ind[v];
    while (1)
    {
        uchain = ind[u];
        if (uchain == vchain)
        {
            st.update(pos[v], pos[u]);
            return;
        }
        st.update(pos[par[head[uchain]][0]], pos[u]);
        u = par[head[uchain]][0];
    }
}
bool ok[maxn];
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q;
    st.resize(n);
    st.build(1, 1, n);
    fo(i, 1, n - 1)
    {
        int u, v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    int root;
    fo(i, 1, n) if (ss(ke[i]) > 1)
    {
        root = i;
        dfs(root);
        hld(root);
        break;
    }
    int leaves = 0;
    fo(i, 1, n) if (ss(ke[i]) == 1)
    {
        ok[i] = 1;
        leaves++;
        change(i, root);
    }
    while (q--)
    {
        int m;
        cin >> m;
        vi v(m);
        int l1 = leaves;
        int timer = ss(st.event);
        for (int &x : v)
        {
            cin >> x;
            if (ok[x])
            {
                ok[x] = 0;
            }
            else
            {
                leaves++;
                change(x, root);
            }
        }
        if (leaves % 2 == 1)
            cout << -1;
        else
            cout << n + m + st.st[1] - 2;
        en;
        leaves = l1;
        st.rollback(timer);
        for(int x : v) ok[x] = (ss(ke[x]) == 1);
    }
}

Compilation message

cleaning.cpp: In member function 'void SegmentTree::down(long long int, long long int, long long int)':
cleaning.cpp:80:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |         int mid = l + r >> 1;
      |                   ~~^~~
cleaning.cpp: In member function 'void SegmentTree::build(long long int, long long int, long long int)':
cleaning.cpp:94:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |         int mid = l + r >> 1;
      |                   ~~^~~
cleaning.cpp: At global scope:
cleaning.cpp:217:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  217 | main()
      | ^~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:269:23: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
  269 |                 change(x, root);
      |                 ~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Incorrect 104 ms 15396 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 47 ms 13452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 34 ms 12888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 15604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 192 ms 30556 KB Output is correct
2 Incorrect 281 ms 30548 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 265 ms 38484 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Incorrect 104 ms 15396 KB Output isn't correct
3 Halted 0 ms 0 KB -