【分块】【P2801】教主的魔法
Description
给你一个长度为 的序列,要求资瓷区间加,查询区间大于等于 的数的个数
Input
第一行是 代表序列长度和操作个数
下面一行代表序列
下面 行,每行四个参数,分别为
如果 则区间加
如果 则查询
Output
对每次查询输出结果
Hint
Solution
看到序列这么长,操作数这么少,常见的复杂度平衡的数据结构大概不起作用,于是考虑分块。
分块后考虑块内如何查询答案:可以对块内整个进行排序,然后lowerbound一下即可。
考虑修改时,如果修改整个块则不会对块内大小顺序造成影响,可以直接处理。
边界上直接暴力修改,修改完暴力排序,因为只会暴力两个块,所以复杂度还是 的,总复杂度 ,可以通过本题。
Code
#include <cmath>
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 1000010;
int n, q;
ll MU[maxn], belong[maxn], temp[maxn], tag[maxn], lc[maxn], rc[maxn];
void rebuild(ci);
int main() {
freopen("1.in", "r", stdin);
qr(n); qr(q);
for (rg int i = 1; i <= n; ++i) qr(MU[i]);
for (rg int i = 1; i <= n; ++i) temp[i] = MU[i];
for (rg int i = 1, sn = sqrt(n); i <= n; ++i) belong[i] = i / sn;
for (rg int i = 1, j = 1; i <= n; i = j) {
while(belong[j] == belong[i]) ++j;
std::sort(temp + i, temp + j);
lc[belong[i]] = i; rc[belong[i]] = j;
}
int a, b, c; rg char ch;
while (q--) {
do {ch = IPT::GetChar();} while((ch != 'M') && (ch != 'A'));
if (ch == 'M') {
a = b = c = 0;
qr(a); qr(b); qr(c);
if (belong[a] == belong[b]) {
for (rg int i = a; i <= b; ++i) {
MU[i] += c;
}
rebuild(a);
} else {
for (rg int i = belong[a] + 1; i < belong[b]; ++i) tag[i] += c;
for (rg int i = a; belong[i] == belong[a]; ++i) MU[i] += c;
for (rg int i = b; belong[i] == belong[b]; --i) MU[i] += c;
rebuild(a); rebuild(b);
}
} else {
a = b = c = 0;
qr(a); qr(b); qr(c);
int _ret = 0;
if (belong[a] == belong[b]) {
c -= tag[belong[a]];
for (rg int i = a; i <= b; ++i) if (MU[i] >= c) ++_ret;
} else {
for (rg int i = belong[a] + 1; i < belong[b]; ++i) _ret += temp + rc[i] - std::lower_bound(temp + lc[i], temp + rc[i], c - tag[i]);
c -= tag[belong[a]];
for (rg int i = a; belong[i] == belong[a]; ++i) _ret += MU[i] >= c;
c += tag[belong[a]]; c -= tag[belong[b]];
for (rg int i = b; belong[i] == belong[b]; --i) _ret += MU[i] >= c;
}
qw(_ret, '\n', true);
}
}
return 0;
}
void rebuild(ci a) {
for (rg int i = lc[belong[a]]; belong[i] == belong[a]; ++i) temp[i] = MU[i];
std::sort(temp + lc[belong[a]], temp + rc[belong[a]]);
}
Summary
当查询较少但是序列较长时,考虑分块来降低维护代价。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具