[BZOJ4636]蒟蒻的数列
[BZOJ4636]蒟蒻的数列
试题描述
输入
输出
输入示例
输出示例
数据规模及约定
见“输入”
题解
题目问的是最后的总和,所以对于每个元素我们只关心它经过所有包含它的操作后的值。每个操作 [a, b) -> k 建一个点 (a, b-1),权值为 k,则对于第 i 个数,找到点 (i, i) 左上方所有点的最大权值即为这个数最终的值。
看到这样的水题,而且数据范围又是 40000,感觉非常像带根号的算法,就忍不住写 kd 树了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 40010
#define oo 2147483647
#define LL long long
int n, lc[maxn], rc[maxn], root, Cur;
struct Node {
int x[2], mx[2], mn[2], val, maxv;
bool operator < (const Node& t) const { return x[Cur] != t.x[Cur] ? x[Cur] < t.x[Cur] : x[Cur^1] < t.x[Cur^1]; }
} ns[maxn], x; void maintain(int o) {
int l = lc[o], r = rc[o];
for(int i = 0; i < 2; i++) {
ns[o].mx[i] = max(max(ns[l].mx[i], ns[r].mx[i]), ns[o].x[i]);
ns[o].mn[i] = min(min(ns[l].mn[i], ns[r].mn[i]), ns[o].x[i]);
}
ns[o].maxv = max(max(ns[l].maxv, ns[r].maxv), ns[o].val);
return ;
}
void build(int& o, int L, int R, int cur) {
if(L > R){ o = 0; return ; }
int M = L + R >> 1; o = M;
Cur = cur; nth_element(ns + L, ns + M, ns + R + 1);
build(lc[o], L, M - 1, cur ^ 1); build(rc[o], M + 1, R, cur ^ 1);
maintain(o);
return ;
}
bool all(int o) { return ns[o].mx[0] <= x.x[0] && ns[o].mn[1] >= x.x[1]; }
bool has(int o) { return ns[o].mn[0] <= x.x[0] && ns[o].mx[1] >= x.x[1]; }
int query(int o) {
if(!o) return 0;
int l = lc[o], r = rc[o], ans = 0;
if(ns[o].x[0] <= x.x[0] && ns[o].x[1] >= x.x[1]) ans = ns[o].val;
if(ns[l].maxv > ns[r].maxv) {
if(all(l)) ans = max(ans, ns[l].maxv);
else if(has(l) && ns[l].maxv > ans) ans = max(ans, query(l));
if(all(r)) ans = max(ans, ns[r].maxv);
else if(has(r) && ns[r].maxv > ans) ans = max(ans, query(r));
}
else {
if(all(r)) ans = max(ans, ns[r].maxv);
else if(has(r) && ns[r].maxv > ans) ans = max(ans, query(r));
if(all(l)) ans = max(ans, ns[l].maxv);
else if(has(l) && ns[l].maxv > ans) ans = max(ans, query(l));
}
return ans;
} struct Cmd { int a, b, k; } cs[maxn];
int cnt, num[maxn<<2], tp[maxn<<2];
LL ans;
int main() {
ns[0].mx[0] = ns[0].mx[1] = -oo;
ns[0].mn[0] = ns[0].mn[1] = oo;
ns[0].maxv = -oo;
n = read();
for(int i = 1; i <= n; i++) {
num[++cnt] = cs[i].a = read();
num[++cnt] = cs[i].a + 1;
num[++cnt] = cs[i].b = read() - 1;
num[++cnt] = cs[i].b + 1;
cs[i].k = read();
}
sort(num + 1, num + cnt + 1);
cnt = unique(num + 1, num + cnt + 1) - num;
// for(int i = 1; i <= cnt; i++) printf("%d ", num[i]); putchar('\n');
for(int i = 1; i <= n; i++) {
int x0 = lower_bound(num + 1, num + cnt + 1, cs[i].a) - num,
x1 = lower_bound(num + 1, num + cnt + 1, cs[i].b) - num;
ns[i].x[0] = x0; tp[x0] = 1;
ns[i].x[1] = x1; tp[x1] = 1;
ns[i].maxv = ns[i].val = cs[i].k;
}
for(int i = 1; i <= cnt; i++) if(!tp[i]) tp[i] = num[i+1] - num[i-1] - 1; // for(int i = 1; i <= n; i++) printf("%d: %d %d %d\n", i, ns[i].x[0], ns[i].x[1], ns[i].val);
build(root, 1, n, 0);
// for(int i = 1; i <= n; i++) printf("%d: %d %d %d\n", i, ns[i].x[0], ns[i].x[1], ns[i].val);
for(int i = 1; i <= cnt; i++) {
x.x[0] = x.x[1] = i;
int val = query(root);
// printf("%d: %d %d\n", i, val, tp[i]);
ans += (LL)tp[i] * (LL)val;
} printf("%lld\n", ans); return 0;
}
[BZOJ4636]蒟蒻的数列的更多相关文章
- [bzoj4636]蒟蒻的数列_线段树
蒟蒻的数列 bzoj-4636 题目大意:给定一个序列,初始均为0.n次操作:每次讲一段区间中小于k的数都变成k.操作的最后询问全局和. 注释:$1\le n\le 4\cdot 10^4$. 想法: ...
- BZOJ4636: 蒟蒻的数列(动态开节点线段树)
题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...
- 【BZOJ4636】蒟蒻的数列 STL
[BZOJ4636]蒟蒻的数列 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个 ...
- 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 247 Solved: 113[Submit][Status][Discuss ...
- 【BZOJ】4636: 蒟蒻的数列
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 145 Solved: 71[Submit][Status][Discuss] ...
- BZOJ_4636_蒟蒻的数列_线段树+动态开点
BZOJ_4636_蒟蒻的数列_线段树+动态开点 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将 ...
- BZOJ 4636: 蒟蒻的数列 分块
4636: 蒟蒻的数列 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4636 Description 蒟蒻DCrusher不仅喜欢玩扑克 ...
- 【bzoj4636】蒟蒻的数列 离散化+线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...
- 【刷题】BZOJ 4636 蒟蒻的数列
Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k小的数改为k,他想 ...
随机推荐
- Object C学习笔记21-typedef用法
在上一章的学习过程中遇到了一个关键字typedef,这个关键字是C语言中的关键字,因为Object C是C的扩展同样也是支持typedef的. 一. 基本作用 typedef是C中的关键字,它的主要作 ...
- css的6中居中的方式
请先看blog:http://blog.csdn.net/wolinxuebin/article/details/7615098
- 编写高质量代码改善C#程序的157个建议[避免finaly内的无效代码、避免嵌套异常、避免吃掉异常、注意循环异常处理]
前言 本文已同步到http://www.cnblogs.com/aehyok/p/3624579.html.本文主要来学习以下几点建议 建议61.避免在finally内撰写无效代码 建议62.避免嵌套 ...
- JS表单学习笔记(思维导图)
导图
- 前端筑基篇(一)->ajax跨域原理以及解决方案
说明 跨域主要是由于浏览器的“同源策略”引起,分为多种类型,本文主要探讨Ajax请求跨域问题 前言 参考来源 什么是跨域 ajax跨域的表现 跨域的原理 如何解决跨域问题 JSONP方式解决跨域问题 ...
- 35.Android之带删除按钮EditText学习
今天实现Android里自定义带删除功能的EditText,效果如下: 当输入内容时,EditText变为带有一个删除功能按钮的编辑框,如图: 实现代码很简单,直接上代码, 布局文件xml: < ...
- easyVS
easyVS 详细说明点这里
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Php学习之SESSION反序列化机制
在php.ini中存在三项配置项:session.save_path="" --设置session的存储路径session.save_handler="" -- ...
- IOS基础之 (十二) Block
一 定义 Block封装了一段代码,可以在任何时候执行. Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值. 二 使用 1. 定义函数指针,然后在实现. #import & ...