卿學姐與公主 UESTC - 1324 分塊模板題
阿新 • • 發佈:2018-03-09
sca const AC warnings int turn mat ios math
題意: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> usingnamespace 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\n", ask(l, r)); } }
看到了2014年6月的卿學姐刷題推薦http://www.cnblogs.com/qscqesze/p/3852521.html 1320道題吧。。。Orz
卿學姐與公主 UESTC - 1324 分塊模板題