Olympiad in Informatics

[Fortuna OJ] Mar 4th - Group A Problem-Solving Report

A - The Pitch-Black Train Carries Several Lies

This problem should be a textbook problem for the domain-extended DSU, so I won't go over it.

// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200;
int n, m, fa[MAX_N << 2];
int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }
void unite(int x, int y) { fa[find(x)] = fa[find(y)]; }
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= 2 * n; i++)
        fa[i] = i;
    while (m--)
    {
        int opt, x, y;
        scanf("%d%d%d", &opt, &x, &y);
        if (opt == 0)
            unite(x, y), unite(x + n, y + n);
        else if (opt == 1 || opt == 2)
            unite(x, y + n), unite(x + n, y);
        else if (find(x) == find(y) || find(x + n) == find(y + n))
            puts("1");
        else
            puts("0");
    }
    return 0;
}

B - God Handed Down an Oracle at the Summer Solstice Festival

Even the stupid know the equation for this problem is:

\[ f[i] = \sum_{[i,j] \in validSet} f[j] \]

However, enumerating like this is \(O(n^2)\), a really garbage complexity. We can use a Fenwick tree to take care of this transition, and then the complexity is \(O(n log n)\). First, the expression for the absolute difference is \(|numOfSummer - numOfWinter|\). We can consider taking prefix sums, setting the contribution of each summer person to \(1\) and that of each winter person to \(-1\), so the set of \(f[j]\) the equation asks for actually lies in the interval \([-k,k]\); then adding one more person is equivalent to shifting the whole interval rightward\leftward, so the real interval is \([sum[i]-k,sum[i]+k]\), and once that is computed, just go to \(sum[i]\) in the Fenwick tree and stuff \(f[i]\) in.

// B.cpp
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define ll long long
using namespace std;
const int MAX_N = 2e5 + 2000, mod = 1e9 + 7;
ll tree[MAX_N << 2], f[MAX_N], n, prefix[MAX_N], tmpx, k;
// the number domain still exists;
void add(int x, ll d)
{
    x += MAX_N;
    while (x <= 2 * MAX_N)
        tree[x] += d, x += lowbit(x);
}
ll sum(int x)
{
    x += MAX_N;
    ll ans = 0;
    while (x)
        ans += tree[x], x -= lowbit(x);
    return ans;
}
int main()
{
    scanf("%lld%lld", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &tmpx), prefix[i] = prefix[i - 1] + ((tmpx == 1) ? 1 : -1);
    add(0, 1);
    for (int i = 1; i <= n; i++)
        f[i] = (sum(prefix[i] + k) - sum(prefix[i] - k - 1) + mod) % mod, add(prefix[i], f[i]);
    printf("%lld", f[n]);
    return 0;
}

C - The Shattered Segment Tree

First, a conclusion:

Number of nodes the interval is located to (the answer) = 2 * interval length - number of nodes completely contained in the interval. —Solution

So, all we need is to take care of counting that latter term, the number of completely contained nodes. Mathematically speaking, we need to count the intervals \([l',r'],l'\geq l, r' \leq r\). Obviously, this is just a fucking 2D partial order problem. So, problem over.

// C.cpp
#include <bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
#define pr pair<int, int>
using namespace std;
const int MAX_N = 1e5 + 2000;
int n, m, tmpx, tmpy1, tot, tree[MAX_N << 2];
pr nodes[MAX_N << 2];
struct query
{
    int l, r, ans, id;
    bool operator<(const query &q) const { return id < q.id; }
} queries[MAX_N];
bool cmp(pr a, pr b) { return a.second < b.second; }
bool cmp2(query a, query b) { return a.r < b.r; }
void init(int l, int r)
{
    if (l == r)
    {
        nodes[++tot].first = l, nodes[tot].second = r;
        return;
    }
    nodes[++tot].first = l, nodes[tot].second = r;
    int mid;
    scanf("%d", &mid);
    init(l, mid), init(mid + 1, r);
}
void add(int x, int d)
{
    while (x <= n)
        tree[x] += d, x += lowbit(x);
}
ll sum(int x)
{
    ll ans = 0;
    while (x)
        ans += tree[x], x -= lowbit(x);
    return ans;
}
int main()
{
    scanf("%d%d", &n, &m);
    init(1, n);
    sort(nodes + 1, nodes + 1 + tot, cmp);
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &queries[i].l, &queries[i].r), queries[i].id = i;
    sort(queries + 1, queries + 1 + m, cmp2);
    int cur = 1;
    for (int i = 1; i <= m; i++)
    {
        int xlimit = queries[i].r;
        while (nodes[cur].second <= xlimit && cur <= tot)
            add(nodes[cur].first, 1), cur++;
        queries[i].ans = sum(queries[i].r) - sum(queries[i].l - 1);
    }
    sort(queries + 1, queries + 1 + m);
    for (int i = 1; i <= m; i++)
        printf("%lld\n", 2 * (queries[i].r - queries[i].l + 1) - queries[i].ans);
    return 0;
}