萌新初学OI,求助线段树RE
查看原帖
萌新初学OI,求助线段树RE
246019
Elma_楼主2020/5/30 12:13

RT,全部RE/kk

不知道哪里又写挂了/kk 有神仙帮忙看看吗/kel/kk/dk

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 1000005
#define ll long long
using namespace std;

ll n, m, p, ans;
ll tree[maxn << 2], tag[maxn << 2];
ll a[maxn], op[maxn], l[maxn], r[maxn];

inline ll lson(ll x) { return x << 1; }
inline ll rson(ll x) { return x << 1 | 1; }
inline void push_up(ll x) { tree[x] = tree[lson(x)] + tree[rson(x)]; }

inline int read()
{
    char v = getchar();int x = 0, f = 1;
    while (!isdigit(v)) { if (v == '-')f = -1;v = getchar(); }
    while (isdigit(v)) { x = x * 10 + v - 48;v = getchar(); }
    return x * f;
}

inline void build(ll x, ll l, ll r, ll k)
{
    if (l == r)
    {
        tree[x] = a[l] >= k;
        tag[x] = 0;
        return;
    }
    ll mid = (l + r) >> 1;
    build(lson(x), l, mid, k);
    build(rson(x), mid + 1, r, k);
    push_up(x);
}

inline void push_down(ll x, ll l, ll r)
{
    if (!tag[x]) return;
    tag[lson(x)] = tag[rson(x)] = tag[x];
    ll mid = (l + r) >> 1;
    if (tag[x] == 1)
    {
        tree[lson(x)] = mid - l + 1;
        tree[rson(x)] = r - mid;
    }
    else tree[lson(x)] = tree[rson(x)] = 0;
    tag[x] = 0;
}

inline ll query(ll x, ll l, ll r, ll ql, ll qr)
{
    if (ql <= l && qr >= r) return tree[x];
    push_down(x, l, r);
    ll res = 0, mid = (l + r) >> 1;
    if (ql <= mid) res += query(lson(x), l, mid, ql, qr);
    else if (qr > mid) res += query(rson(x), mid + 1, r, ql, qr);
    return res;
}

inline ll querypoint(ll x, ll l, ll r, ll k)
{
    if (l == k && r == k) return tree[x];
    push_down(x, l, r);
    ll mid = (l + r) >> 1;
    if (l <= mid) return querypoint(lson(x), l, mid, k);
    else return querypoint(rson(x), mid + 1, r, k);
}

inline void update(ll x, ll l, ll r, ll ul, ll ur, ll k)
{
    if (ul <= l && ur >= r)
    {
        tree[x] = k * (r - l + 1);
        tag[x] = k ? 1 : -1;
        return;
    }
    ll mid = (l + r) >> 1;
    push_down(x, l, r);
    if (ul <= mid) update(lson(x), l, mid, ul, ur, k);
    if (ur > mid) update(rson(x), mid + 1, r, ul, ur, k);
    push_up(x);
}

inline bool check(ll x)
{
    build(1, 1, n, x);
    for (ll i = 1;i <= m;i++)
    {
        ll cnt = query(1, 1, n, l[i], r[i]);
        if (op[i] == 0)
        {
            update(1, 1, n, r[i] - cnt + 1, r[i], 1);
            update(1, 1, n, l[i], r[i] - cnt + 1, 0);
        }
        else
        {
            update(1, 1, n, l[i], l[i] + cnt - 1, 1);
            update(1, 1, n, l[i] + cnt - 1, r[i], 0);
        }
    }
    return querypoint(1, 1, n, p);
}

int main(void)
{
    n = read(), m = read();
    for (ll i = 1;i <= n;i++) a[i] = read();
    for (ll i = 1;i <= m;i++)
        op[i] = read(), l[i] = read(), r[i] = read();
    p = read();
    ll nl = 1, nr = n;
    while (nl <= nr)
    {
        ll mid = (nl + nr) >> 1;
        if (check(mid)) ans = mid, nl = mid + 1;
        else nr = mid - 1;
    }
    cout << ans << endl;
	 return 0;
}
2020/5/30 12:13
加载中...