HDOJ【5575】【单调栈】【可并堆】
来源:互联网 发布:淘宝家具店铺排行榜 编辑:程序博客网 时间:2024/06/16 00:02
好久木有做题啦!
/* I will wait for you */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <algorithm>#include <iostream>#include <fstream>#include <vector>#include <queue>#include <deque>#include <set>#include <map>#include <string>#include <ext/pb_ds/priority_queue.hpp>#define fi first#define se secondusing namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> pii;typedef map<int, int> mii;const int maxn = 500010;const int maxm = 1010;const int maxs = 26;const int inf = 0x3f3f3f3f;const int P = 1000000007;const double eps = 1e-6;inline ll read(){ ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f;}struct query{ int x, y, type;} e[maxn];bool operator < (query a, query b){ return a.y > b.y;}int n, m, top, h[maxn], sta[maxn], sum_1[maxn], sum_2[maxn], dp[maxn];__gnu_pbds::priority_queue<query> pri[maxn];void init(){ for (int i = 1; i <= n; i++) { pri[i].clear(), dp[i] = 0; sum_1[i] = sum_2[i] = 0; }}int main(){ int T = read(), cnt = 0; while (T--) { n = read(), m = read(), init(); for (int i = 1; i <= n - 1; i++) h[i] = read(); h[0] = h[n] = inf; for (int i = 1; i <= m; i++) { int x = read(), y = read() + 1, t = read(); e[i] = (query) {x, y, t}; pri[x].push(e[i]), sum_2[x] += (t == 0); } top = sta[0] = 0; for (int i = 1; i <= n; i++) { while (h[i] > h[sta[top]]) { int cnt_1 = 0, cnt_2 = 0, hig = 0; int ans = sum_1[i] + sum_2[i], j = sta[top]; while (!pri[i].empty() && pri[i].top().y <= h[j]) { query tmp = pri[i].top(); pri[i].pop(); if (tmp.y != hig) { sum_1[i] += cnt_1, cnt_1 = 0; sum_2[i] -= cnt_2, cnt_2 = 0; ans = max(ans, sum_1[i] + sum_2[i]); hig = tmp.y; } cnt_1 += (tmp.type == 1); cnt_2 += (tmp.type == 0); } sum_1[i] += cnt_1, sum_2[i] -= cnt_2; ans = max(ans, sum_1[i] + sum_2[i]); dp[i] = max(dp[i], dp[j] + ans); sum_1[i] += sum_1[j], sum_2[i] += sum_2[j]; top -= 1, pri[i].join(pri[j]); } int cnt_1 = 0, cnt_2 = 0, hig = 0; int ans = sum_1[i] + sum_2[i], j = sta[top]; while (!pri[i].empty() && pri[i].top().y <= h[i]) { query tmp = pri[i].top(); pri[i].pop(); if (tmp.y != hig) { sum_1[i] += cnt_1, cnt_1 = 0; sum_2[i] -= cnt_2, cnt_2 = 0; ans = max(ans, sum_1[i] + sum_2[i]); hig = tmp.y; } cnt_1 += (tmp.type == 1); cnt_2 += (tmp.type == 0); } sum_1[i] += cnt_1, sum_2[i] -= cnt_2; ans = max(ans, sum_1[i] + sum_2[i]); dp[i] = max(dp[i], dp[j] + ans); sta[++top] = i; } printf("Case #%d: %d\n", ++cnt, dp[n]); } return 0;}
0 0
- HDOJ【5575】【单调栈】【可并堆】
- HDU1512 可并堆
- 【模板】可并堆
- 可并堆?左偏树?
- 可并堆模板
- 可并堆
- 左偏树(可并堆)
- HDU1512 左偏树(可并堆)
- 猴子大王 可并堆
- 可并堆之左偏树
- luogu 3377 可并堆
- 【左偏树(可并堆)模板】
- 可并堆与二叉堆
- ZOJ 2334 可并堆<斜堆>
- 左偏树/斜堆/可并堆-洛谷P3377 【模板】左偏树(可并堆)
- 【可并堆的应用】poj3016
- 可并堆 poj3016 不平衡的美
- BZOJ 1455 罗马游戏 可并堆
- 差分约束详解,详解!!!hdoj 1384
- android NFC开发
- C循环语句
- mysql 建表时类型长度的意思
- 最近一段时间总结(一)
- HDOJ【5575】【单调栈】【可并堆】
- Python扩展库psutil用法精要
- 【Codeforces Round 331 (Div 2)A】【水题】Wilbur and Swimming Pool 给出矩形1~4个顶点让你还原矩形
- 【tmp】引论
- 黑马程序员--StringBuffer和数据缓冲池的面试题
- 浅谈系统的高可用性
- 13_Java Character类
- $.extend()和(function($){….})(jQuery)
- C++读取*.bin文件时无法获取0x20