教主的魔法【模拟】

题目链接


  可以说是一些块状数据结构,用分块数组来进行一个维护,然后对于每个被完整覆盖的块用一个永久化标记来进行存储一下,一道模板题。

【分块数组模版】

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e6 + 7;
int N, Q, h[maxN], num, len;
#define pii pair
#define MP(a, b) make_pair(a, b)
const int maxP = 1100;
pii t[maxP];
int lazy[maxP] = {0};
vector vt[maxP];
int main()
{
    scanf("%d%d", &N, &Q);
    for(int i=1; i<=N; i++) scanf("%d", &h[i]);
    len = (int)sqrt(N + 1);
    int id = 0;
    int pos = 1, end_pos;
    while(pos <= N)
    {
        id++;
        end_pos = pos + len - 1;
        end_pos = min(end_pos, N);
        t[id] = MP(pos, end_pos);
        pos = end_pos + 1;
    }
    for(int i=1; i<=id; i++)
    {
        for(int j=t[i].first; j<=t[i].second; j++)
        {
            vt[i].push_back(h[j]);
        }
        sort(vt[i].begin(), vt[i].end());
    }
    char op[3]; int L, R, W, beg_id, ql, qr, ans, tmp;
    for(int i=1; i<=Q; i++)
    {
        scanf("%s%d%d%d", op, &L, &R, &W);
        if(op[0] == 'M')
        {
            beg_id = (int)(upper_bound(t + 1, t + id + 1, MP(L, 0)) - t - 1);
            for(int ith = beg_id; ith <= id; ith++)
            {
                if(t[ith].first > R) break;
                if(t[ith].first >= L && t[ith].second <= R)
                {
                    lazy[ith] += W;
                }
                else
                {
                    ql = max(L, t[ith].first);
                    qr = min(R, t[ith].second);
                    vt[ith].clear();
                    for(int j=t[ith].first; j<=t[ith].second; j++)
                    {
                        if(j >= ql && j <= qr)
                        {
                            vt[ith].push_back(h[j] + W);
                            h[j] += W;
                        }
                        else vt[ith].push_back(h[j]);
                    }
                    sort(vt[ith].begin(), vt[ith].end());
                }
            }
        }
        else
        {
            ans = 0;
            beg_id = (int)(upper_bound(t + 1, t + id + 1, MP(L, 0)) - t - 1);
            for(int ith = beg_id; ith <= id; ith++)
            {
                if(t[ith].first > R) break;
                if(t[ith].first >= L && t[ith].second <= R)
                {
                    tmp = (int)(vt[ith].end() - lower_bound(vt[ith].begin(), vt[ith].end(), W - lazy[ith]));
                    ans += tmp;
                }
                else
                {
                    ql = max(L, t[ith].first);
                    qr = min(R, t[ith].second);
                    for(int j=ql; j<=qr; j++)
                    {
                        if(h[j] + lazy[ith] >= W)
                        {
                            ans ++;
                        }
                    }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}

 

你可能感兴趣的:(分块,分块)