题意: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 = 100005; 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 = 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 <= 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 = 0; 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] + 1; 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 = 1; i <= q; i++) { int op, l, r; scanf("%d%d%d", &op, &l, &r); if (op == 1)update(l, r); else printf("%lld ", ask(l, r)); } }
看到了2014年6月的卿学姐刷题推荐http://www.cnblogs.com/qscqesze/p/3852521.html 1320道题吧。。。Orz