Codeforces 1321 E. World of Darkraft: Battle for Azathoth (segment tree)

Here Insert Picture Description

Meaning of the questions:

Have n n weapons each weapon a i a_i The cost to buy attack c a i c_{a_i} m m a defense each armor b i b_i spend c b i c_{b_i} p p monster each x x attack y Y defense out z from gold, weapons, armor choose one choose one, to be able to kill the monsters are killed, you can kill the monster is greater than its defense attack your defenses greater than its attack, and asked the killing income - can take up to buy equipment how much is
the initial return set for weapon c a i c_{a_i} , Then press the defense of small to large to enumerate each piece of armor, while constantly updated attack less than the current armor defense force, the answer is the weapon of return - the maximum armor prices. The largest segment tree maintenance interval coverage.

AC Code:

const int N = 2e5 + 5;
const int INF = 0x7fffffff;
int n, m, p;

struct node
{
    int a, c;
    node(int x = 0)
    {
        a = x, c = 0;
    }
} wp[N], ar[N];
bool operator<(node a, node b)
{
    return a.a < b.a;
}
struct node2
{
    int x, y, z;
} mon[N];

bool operator<(node2 a, node2 b)
{
    return a.x < b.x;
}

int ans = -INF;
int mx[N << 2], tag[N << 2];

inline void add(int k, int v)
{
    mx[k] += v;
    tag[k] += v;
}
inline void pushup(int k)
{
    mx[k] = max(mx[k << 1], mx[k << 1 | 1]);
}
inline void pushdwn(int k)
{
    add(k << 1, tag[k]);
    add(k << 1 | 1, tag[k]);
    tag[k] = 0;
}
void build(int k, int l, int r)
{
    if (l == r)
    {
        mx[k] = -ar[l].c;
        return;
    }
    int mid = l + r >> 1;
    build(k << 1, l, mid);
    build(k << 1 | 1, mid + 1, r);
    pushup(k);
    return;
}

void modify(int k, int l, int r, int qx, int qy, int qv)
{
    if (qx <= l && r <= qy)
        return add(k, qv);
    pushdwn(k);
    int mid = l + r >> 1;
    if (qx <= mid)
        modify(k << 1, l, mid, qx, qy, qv);
    if (mid < qy)
        modify(k << 1 | 1, mid + 1, r, qx, qy, qv);
    pushup(k);
    return;
}

int main()
{
    sddd(n, m, p);
    rep(i, 1, n)
        sdd(wp[i].a, wp[i].c);
    rep(i, 1, m)
        sdd(ar[i].a, ar[i].c);
    sort(wp + 1, wp + n + 1);
    sort(ar + 1, ar + m + 1);
    rep(i, 1, p)
        sddd(mon[i].x, mon[i].y, mon[i].z);
    sort(mon + 1, mon + p + 1);
    build(1, 1, m);
    for (int i = 1, s1 = 0; i <= n; i++)
    {
        while (s1 < p && mon[s1 + 1].x < wp[i].a)
        {
            s1++;
            int w = upper_bound(ar + 1, ar + m + 1, node(mon[s1].y)) - ar;
            if (w <= m)
                modify(1, 1, m, w, m, mon[s1].z);
        }
        ans = max(ans, mx[1] - wp[i].c);
    }
    pd(ans);
    return 0;
}

Published 704 original articles · won praise 420 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_43627087/article/details/104612503