BZOJ 3343: 教主的魔法 分块算法

来源:互联网 发布:centos6 搭建php环境 编辑:程序博客网 时间:2024/05/16 01:42

Description
教主最近学会了一种神奇的魔法,能够使人长高。于是他准备演示给XMYZ信息组每个英雄看。于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1、2、……、N。
每个人的身高一开始都是不超过1000的正整数。教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W。(虽然L=R时并不符合区间的书写规范,但我们可以认为是单独增加第L(R)个英雄的身高)
CYZ、光哥和ZJQ等人不信教主的邪,于是他们有时候会问WD闭区间 [L, R] 内有多少英雄身高大于等于C,以验证教主的魔法是否真的有效。
WD巨懒,于是他把这个回答的任务交给了你。

Input
第1行为两个整数N、Q。Q为问题数与教主的施法数总和。
第2行有N个正整数,第i个数代表第i个英雄的身高。
第3到第Q+2行每行有一个操作:
(1) 若第一个字母为“M”,则紧接着有三个数字L、R、W。表示对闭区间 [L, R] 内所有英雄的身高加上W。
(2) 若第一个字母为“A”,则紧接着有三个数字L、R、C。询问闭区间 [L, R] 内有多少英雄的身高大于等于C。

Output
对每个“A”询问输出一行,仅含一个整数,表示闭区间 [L, R] 内身高大于等于C的英雄数。

Sample Input
5 3

1 2 3 4 5

A 1 5 4

M 3 5 1

A 1 5 4

Sample Output
2

3

HINT

【输入输出样例说明】

原先5个英雄身高为1、2、3、4、5,此时[1, 5]间有2个英雄的身高大于等于4。教主施法后变为1、2、4、5、6,此时[1, 5]间有3个英雄的身高大于等于4。

【数据范围】

对30%的数据,N≤1000,Q≤1000。

对100%的数据,N≤1000000,Q≤3000,1≤W≤1000,1≤C≤1,000,000,000。

解题方法:
分块算法
就是每一块的个数为根号n
修改: 对于一整块,直接打add标记,头尾俩块不完整的进行暴力修改重构
查询: 每一块内排序,在第i块内二分查找大等于C-add[i]的数字,头尾俩块暴力查询

#include <bits/stdc++.h>using namespace std;const int maxn = 1000010;int n, m, num, belong[maxn], block, l[maxn], r[maxn], a[maxn], add[maxn], d[maxn];void build(){    block = sqrt(n);    num = n / block; if(n%block) num++;    for(int i = 1; i <= num; i++) l[i] = (i-1)*block + 1, r[i] = i*block;    r[num] = n;    for(int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;    for(int i = 1; i <= n; i++) d[i] = a[i];    for(int i = 1; i <= num; i++) sort(d + l[i], d + r[i] + 1);}void update(int L, int R, int W){    if(belong[L] == belong[R]){        for(int i = l[belong[L]]; i <= r[belong[R]]; i++) a[i] += add[belong[L]];        add[belong[L]] = 0;        for(int i = L; i <= R; i++) a[i] += W;        for(int i = l[belong[L]]; i <= r[belong[R]]; i++) d[i] = a[i];        sort(d + l[L], d + r[R] + 1);        return ;    }    for(int i = l[belong[L]]; i <= r[belong[L]]; i++) a[i] += add[belong[L]]; add[belong[L]] = 0;    for(int i = L; i <= r[belong[L]]; i++) a[i] += W;    for(int i = l[belong[L]]; i <= r[belong[L]]; i++) d[i] = a[i];    sort(d + l[belong[L]], d + r[belong[L]] + 1);    for(int i = l[belong[R]]; i <= r[belong[R]]; i++) a[i] += add[belong[R]]; add[belong[R]] = 0;    for(int i = l[belong[R]]; i <= R; i++) a[i] += W;    for(int i = l[belong[R]]; i <= r[belong[R]]; i++) d[i] = a[i];    sort(d + l[belong[R]], d + r[belong[R]] + 1);    for(int i = belong[L] + 1; i < belong[R]; i++) add[i] += W;}void query(int L, int R, int W){    int ans = 0;    if(belong[L] == belong[R]){        for(int i = L; i <= R; i++){            if(a[i] + add[belong[i]] >= W) ans++;        }        printf("%d\n", ans);        return;    }    for(int i = L; i <= r[belong[L]]; i++)        if(a[i] + add[belong[i]] >= W) ans++;    for(int i = l[belong[R]]; i <= R; i++)        if(a[i] + add[belong[i]] >= W) ans++;    for(int i = belong[L] + 1; i < belong[R]; i++){        int x = l[i], y = r[i], Ans = 0;        while(x <= y){            int mid = (x + y) / 2;            if(d[mid] + add[i] >= W) y = mid - 1, Ans = r[i] - mid + 1;            else x = mid + 1;        }        ans += Ans;    }    printf("%d\n", ans);}int main(){    scanf("%d%d", &n, &m);    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);    build();    for(int i = 1; i <= m; i++){        char op[5];        int x, y, z;        scanf("%s%d%d%d", op, &x, &y, &z);        if(op[0] == 'A') query(x, y, z);        else update(x, y, z);    }    return 0;}
0 0