1. 程式人生 > 實用技巧 >X - Gorgeous Sequence HDU - 5306 吉司機線段樹

X - Gorgeous Sequence HDU - 5306 吉司機線段樹

X - Gorgeous Sequence

HDU - 5306

這個題目我開始不會寫,匆匆的掃了一眼題解就開始寫了,寫完改了兩三個小時的bug之後終於發現自己理解錯了。。。

這個是吉司機線段樹

#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#define
lc k<<1 #define rc k<<1|1 #define EF if(ch==EOF) return x; #define inf 0x3f3f3f3f3f3f3f3f using namespace std; typedef long long ll; const int maxn = 1e6 + 10; ll sum[maxn * 4]; int Max[maxn * 4], nMax[maxn * 4], num[maxn * 4]; int a[maxn]; int read() { int x = 0, f = 1; char ch = getchar();
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; EF; ch = getchar(); } while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void push_up(int id) { sum[id] = sum[id << 1] + sum[id << 1 | 1]; Max[id] = max(Max[id << 1
], Max[id << 1 | 1]); nMax[id] = -1; if (Max[id << 1] == Max[id << 1 | 1]) num[id] = num[id << 1] + num[id << 1 | 1]; else { if (Max[id << 1] > Max[id << 1 | 1]) num[id] = num[id << 1], nMax[id] = Max[id << 1 | 1]; else num[id] = num[id << 1 | 1], nMax[id] = Max[id << 1]; } nMax[id] = max(nMax[id], max(nMax[id << 1], nMax[id << 1 | 1])); } void build(int id, int l, int r) { if (l == r) { sum[id] = Max[id] = a[l]; nMax[id] = -1; num[id] = 1; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); push_up(id); } void change(int id, int v) { if (v >= Max[id]) return; sum[id] += (v - Max[id]) * 1ll * num[id]; Max[id] = v; } void push_down(int id) { change(id << 1, Max[id]); change(id << 1 | 1, Max[id]); } void update(int id, int l, int r, int x, int y, int v) { if (v >= Max[id]) return; if (x <= l && y >= r && v > nMax[id]) { change(id, v); return; } push_down(id); int mid = (l + r) >> 1; if (x <= mid) update(id << 1, l, mid, x, y, v); if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, v); push_up(id); } int query_max(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return Max[id]; int mid = (l + r) >> 1; int ans = 0; push_down(id); if (x <= mid) ans = max(ans, query_max(id << 1, l, mid, x, y)); if (y > mid) ans = max(ans, query_max(id << 1 | 1, mid + 1, r, x, y)); return ans; } ll query_sum(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return sum[id]; int mid = (l + r) >> 1; push_down(id); ll ans = 0; if (x <= mid) ans += query_sum(id << 1, l, mid, x, y); if (y > mid) ans += query_sum(id << 1 | 1, mid + 1, r, x, y); return ans; } int main() { int t; t = read(); while (t--) { int n, m; n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); build(1, 1, n); while (m--) { int opt, x, y, val; opt = read(); x = read(); y = read(); if (opt == 0) { val = read(); update(1, 1, n, x, y, val); } if (opt == 1) { int ans = query_max(1, 1, n, x, y); printf("%d\n", ans); } if (opt == 2) { ll ans = query_sum(1, 1, n, x, y); printf("%lld\n", ans); } } } return 0; }

#include <cstring>#include <queue>#include <cstdlib>#include <cstdio>#include <iostream>#include <algorithm>#include <map>#include <vector>#define lc k<<1#define rc k<<1|1#define EF if(ch==EOF) return x;#define inf 0x3f3f3f3f3f3f3f3fusingnamespacestd; typedeflonglong ll; constint maxn = 1e6 + 10; ll sum[maxn * 4]; int Max[maxn * 4], nMax[maxn * 4], num[maxn * 4]; int a[maxn]; int read() { int x = 0, f = 1; char ch = getchar(); while (ch<'0' || ch>'9') { if (ch == '-')f = -1; EF; ch = getchar(); } while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void push_up(int id) { sum[id] = sum[id << 1] + sum[id << 1 | 1]; Max[id] = max(Max[id << 1], Max[id << 1 | 1]); nMax[id] = -1; if (Max[id << 1] == Max[id << 1 | 1]) num[id] = num[id << 1] + num[id << 1 | 1]; else { if (Max[id << 1] > Max[id << 1 | 1]) num[id] = num[id << 1], nMax[id] = Max[id << 1 | 1]; else num[id] = num[id << 1 | 1], nMax[id] = Max[id << 1]; } nMax[id] = max(nMax[id], max(nMax[id << 1], nMax[id << 1 | 1])); } void build(int id, int l, int r) { if (l == r) { sum[id] = Max[id] = a[l]; nMax[id] = -1; num[id] = 1; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); push_up(id); } void change(int id, int v) { if (v >= Max[id]) return; sum[id] += (v - Max[id]) * 1ll * num[id]; Max[id] = v; } void push_down(int id) { change(id << 1, Max[id]); change(id << 1 | 1, Max[id]); } void update(int id, int l, int r, int x, int y, int v) { if (v >= Max[id]) return; if (x <= l && y >= r && v > nMax[id]) { change(id, v); return; } push_down(id); int mid = (l + r) >> 1; if (x <= mid) update(id << 1, l, mid, x, y, v); if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, v); push_up(id); } int query_max(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return Max[id]; int mid = (l + r) >> 1; int ans = 0; push_down(id); if (x <= mid) ans = max(ans, query_max(id << 1, l, mid, x, y)); if (y > mid) ans = max(ans, query_max(id << 1 | 1, mid + 1, r, x, y)); return ans; } ll query_sum(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return sum[id]; int mid = (l + r) >> 1; push_down(id); ll ans = 0; if (x <= mid) ans += query_sum(id << 1, l, mid, x, y); if (y > mid) ans += query_sum(id << 1 | 1, mid + 1, r, x, y); return ans; } int main() { int t; t = read(); while (t--) { int n, m; n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); build(1, 1, n); while (m--) { int opt, x, y, val; opt = read(); x = read(); y = read(); if (opt == 0) { val = read(); update(1, 1, n, x, y, val); } if (opt == 1) { int ans = query_max(1, 1, n, x, y); printf("%d\n", ans); } if (opt == 2) { ll ans = query_sum(1, 1, n, x, y); printf("%lld\n", ans); } } } return0; }