P8512 [Ynoi Easy Round 2021] TEST_152 题解

HH 的项链の套路。

离线下来,对询问的 $r$ 扫描线,维护 $z_i=\max\{j|i\in[l_j,r_j],j\le r\}$,$d_x=\sum\limits_{z_i=x}v_{z_i}$。

若 $z_i\ge l$,则 $a_i=v_{z_i}$,否则 $a_i=0$,则询问 $l,r$ 的答案为 $\sum\limits_{z_i\ge l}v_{z_i}=\sum\limits_{i\ge l}d_i$。

ODT 维护 $z_i$,同时 BIT 维护 $d_i$。

#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;
struct S
{
    int l, r, i;
} k[500050];
struct N
{
    int l, r, z;
    bool operator<(N b) const { return l < b.l; }
};
set<N> s;
set<N>::iterator i, j;
long long c[500050], g[500050];
int n, m, q, l[500050], r[500050], v[500050];
bool C(S a, S b) { return a.r < b.r; }
void M(int x, long long k)
{
    for (; x; x &= x - 1)
        c[x] += k;
}
long long Q(int x)
{
    long long q = 0;
    for (; x <= n; x += x & -x)
        q += c[x];
    return q;
}
set<N>::iterator S(int k)
{
    i = s.lower_bound({k});
    if (i != s.end() && i->l == k)
        return i;
    int l = (--i)->l, r = i->r, z = i->z;
    s.erase(i);
    s.insert({l, k - 1, z});
    return s.insert({k, r, z}).first;
}
void A(int l, int r, int z)
{
    M(z, 1ll * v[z] * (r - l + 1));
    j = S(r + 1);
    i = S(l);
    for (set<N>::iterator k = i; k != j; ++k)
        M(k->z, -1ll * v[k->z] * (k->r - k->l + 1));
    s.erase(i, j);
    s.insert({l, r, z});
}
int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= m; ++i)
        s.insert({i, i, 0});
    for (int i = 1; i <= n; ++i)
        scanf("%d%d%d", l + i, r + i, v + i);
    for (int i = 0; i < q; ++i)
        scanf("%d%d", &k[i].l, &k[i].r), k[i].i = i;
    sort(k, k + q, C);
    for (int i = 0, j = 0; i < q; ++i)
    {
        while (j < k[i].r)
            ++j, A(l[j], r[j], j);
        g[k[i].i] = Q(k[i].l);
    }
    for (int i = 0; i < q; ++i)
        printf("%lld\n", g[i]);
    return 0;
}
posted @ 2023-02-27 08:42  5k_sync_closer  阅读(4)  评论(0编辑  收藏  举报  来源