分块,样例没过求条

P1253 扶苏的问题

WydnksqhbD @ 2024-03-12 13:57:01

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6 + 5, B = 1e4;
const ll INF = 1e18;
int n, m, len, cnt;
int id[N];
ll a[N],maxn[B],t1[B],t2[B];
void add (int l, int r, ll k)
{
    for (int i = l; i <= r; i ++)
    {
        if (i % len == 1)
        {
            maxn[id[i]] += k;
            t1[id[i]] += k;
            i += len - 1;
        }
        else
        {
            a[i] += k;
            maxn[id[i]] = max (maxn[id[i]], a[i]);
        }
    }
}
void update (int l, int r, ll x)
{
    for (int i = l; i <= n; i ++)
    {
        if (i % len == 1)
        {
            maxn[id[i]] = x;
            t2[id[i]] = x;
            t1[id[i]] = 0;
            i += len - 1;
        }
        else
        {
            a[i] = x;
            maxn[id[i]] = max (maxn[id[i]], a[i]);
        }
    }
}
ll query (int l, int r)
{
    ll ans=-INF;
    for (int i = l; i <= r; i ++)
    {
        if (i % len == 1)
        {
            ans = max (ans, maxn[id[i]]);
            i += len - 1;
        }
        else
        {
            ll temp = t2[id[i]];
            if (temp == -INF)
            {
                temp = a[i];
            }
            ans = max (ans, temp + t1[id[i]]);
        }
    }
    return ans;
}
int main ()
{
    scanf ("%d %d", &n, &m);
    len = sqrt (n);
    for (int i = 1; i <= n; i ++)
    {
        scanf ("%d", &a[i]);
        if (i % len == 1)
        {
            cnt ++;
        }
        id[i] = cnt;
        maxn[cnt] = max (maxn[cnt], a[i]);
        t2[cnt] = -INF;
    }
    while (m --)
    {
        int op, l, r;
        scanf ("%d %d %d", &op, &l, &r);
        if (op == 1)
        {
            ll x;
            scanf ("%lld", &x);
            add (l, r, x);
        }
        else if (op == 2)
        {
            ll x;
            scanf ("%lld", &x);
            update (l, r, x);
        }
        else
        {
            printf ("%lld \n", query (l, r));
        }
    }
    return 0;
}

by sansesantongshun @ 2024-04-13 12:53:47

《分块》请看数据范围


by kongdeqian @ 2024-07-20 00:29:17

分块过不了吧O(n*sqrt(n))啊


by stils @ 2024-07-24 14:40:24

@kongdeqian 我打了个分块,不会超时但是WA了...


by kongdeqian @ 2024-07-24 15:42:35

@stils
......好吧,但我的线段树最慢的点也跑了1.28s。。。可能是我太菜了


by stils @ 2024-07-24 15:50:22

@kongdeqian 我跑的1.88s,也差不多算是压线了


|