[BZOJ4636]蒟蒻的数列

试题描述

蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列
题目描述
DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k小的数改为k,他想知
道N次操作后数列中所有元素的和。他还要玩其他游戏,所以这个问题留给你解决。

输入

第一行一个整数N,然后有N行,每行三个正整数a、b、k。
N<=40000 , a、b、k<=10^9

输出

一个数,数列中所有元素的和

输入示例


输出示例


数据规模及约定

见“输入

题解

题目问的是最后的总和,所以对于每个元素我们只关心它经过所有包含它的操作后的值。每个操作 [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]蒟蒻的数列的更多相关文章

  1. [bzoj4636]蒟蒻的数列_线段树

    蒟蒻的数列 bzoj-4636 题目大意:给定一个序列,初始均为0.n次操作:每次讲一段区间中小于k的数都变成k.操作的最后询问全局和. 注释:$1\le n\le 4\cdot 10^4$. 想法: ...

  2. BZOJ4636: 蒟蒻的数列(动态开节点线段树)

    题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...

  3. 【BZOJ4636】蒟蒻的数列 STL

    [BZOJ4636]蒟蒻的数列 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个 ...

  4. 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 247  Solved: 113[Submit][Status][Discuss ...

  5. 【BZOJ】4636: 蒟蒻的数列

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 145  Solved: 71[Submit][Status][Discuss] ...

  6. BZOJ_4636_蒟蒻的数列_线段树+动态开点

    BZOJ_4636_蒟蒻的数列_线段树+动态开点 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将 ...

  7. BZOJ 4636: 蒟蒻的数列 分块

    4636: 蒟蒻的数列 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4636 Description 蒟蒻DCrusher不仅喜欢玩扑克 ...

  8. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  9. 【刷题】BZOJ 4636 蒟蒻的数列

    Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k小的数改为k,他想 ...

随机推荐

  1. linq入门系列导航

    写在前面 为什么突然想起来学学linq呢?还是源于在跟一个同事聊天的时候,说到他们正在弄得一个项目,在里面用到了linq to sql.突然想到距上次使用linq to sql是三年前的事情了.下班回 ...

  2. 第四章:javascript: 栈

    列表是一种最自然的数据组织方式.上一章已经介绍如何使用List类将数据组织成一个列表.如果数据存储的顺序不重要.也不必对数据进行查找,那么列表就是一种再好不过的数据结构.对于其它的一些应用,列表就显得 ...

  3. [转]数据库物化视图刷新SQL命令和查询被delete掉的数据

    原文地址:http://blog.csdn.net/wangyong191212/article/details/8024161 刷新物化视图的SQL命令: 在sql语句的命令窗口并输入如下命令: e ...

  4. [转]Java中继承、多态、重载和重写介绍

    什么是多态?它的实现机制是什么呢?重载和重写的区别在那里?这就是这一次我们要回顾的四个十分重要的概念:继承.多态.重载和重写. 继承(inheritance) 简单的说,继承就是在一个现有类型的基础上 ...

  5. Xcode7企业版打包

    今天才发现Xcode7企业账号打包竟然和以前稍微不一样了,一时手残,先把公司服务器以前的ipa包删了,吓得我的小心脏呢 首先选中然后选archive然后点export然后然后选中include man ...

  6. javascript自动转换大小写

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  7. Yii2 初体验

    看着Yii1.1有那么多的不爽,又看着Yii2一天天成熟起来,于是凑一个小项目的原型阶段,试着用Yii2搞一搞. 随手写了一点体会,以一个Yii1的熟练工人看向Yii2的视角,简单一说吧.(将来随时可 ...

  8. php返回json数据简单实例

    <?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice ...

  9. 本地与在线图片转Base64及图片预览

    查看效果:http://sandbox.runjs.cn/show/tgvbo9nq 本地图片转Base64(从而可以预览图片): function localImgLoad() { var src ...

  10. Starting zabbix_agentd: No such file or directory

    问题描述 [root@localhost admin]# service zabbix_agentd restart Shutting down zabbix_agentd: [FAILED] Sta ...