卿学姐与公主 UESTC - 1324 分块模板题

时间:2022-08-22 17:06:28

题意:http://acm.uestc.edu.cn/#/problem/show/1324 中文题,自己看喽。

题解:分块模板,update时顺便更新块属性。ask时先判掉belong[l]==belong[r]。build函数时直接用模板喽。

坑:打错了个字母,改了一下还改错了。还写错个括号。

ac代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<cmath>
using namespace std;
const long long maxn = ;
int belong[maxn], num, block, l[maxn], r[maxn], n, q;
long long Max[maxn], a[maxn];
void build()
{
block = sqrt(n);
num = n / block;
if (n%block) num++;
for (int i = ; i <= num; i++)
l[i] = (i - ) * block + , r[i] = i*block;
r[num] = n;
for (int i = ; i <= n; i++) {
belong[i] = (i - ) / block + ;
} for (int i = ; i <= num; i++)
for (int j = l[i]; j <= r[i]; j++)
Max[i] = max(Max[i], a[j]); }
void update(int x, int y) {
a[x] += y;
Max[belong[x]] = max(Max[belong[x]], a[x]);
}
long long ask(int x, int y) {
long long ans = ;
if (belong[x] == belong[y]) {
for (int i = x; i <= y; i++) ans = max(ans, a[i]);
return ans;
}
for (int i = x; i <= r[belong[x]]; i++) {
ans = max(ans, a[i]);
}
for (int i = belong[x] + ; i < belong[y]; i++) {
ans = max(ans, Max[i]);
}
for (int i = l[belong[y]]; i <= y; i++) {
ans = max(ans, a[i]);
}
return ans;
}
int main() {
scanf("%d%d", &n, &q);
build(); for (int i = ; i <= q; i++) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if (op == )update(l, r);
else printf("%lld\n", ask(l, r)); }
}

看到了2014年6月的卿学姐刷题推荐http://www.cnblogs.com/qscqesze/p/3852521.html 1320道题吧。。。Orz