[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. $().each() 与 $.each()解析

    在jquery 中我们可以选择$().each() 与 $.each() 进行迭代对象和数组 $(items).each(function(){ //item }) , 而后者则 $.each(ite ...

  2. [codevs3223]素数密度(筛)

    题目:http://codevs.cn/problem/3223/ 分析: 可以算出来最大质因子最大不超过50000,因为如果超过50000,那么平方就超过maxlongint了.所以可以筛出5000 ...

  3. 3、面向对象以及winform的简单运用(类的初步认识)

    什么是类? “类”是面向对象编程的基本单元,一个类一般包含两种成员:字段和方法——即变量和函数. 例: //字段或变量的定义 public int age; //方法或函数的定义 public int ...

  4. java线程技术6_线程的挂起和唤醒[转]

    转自:http://blog.chinaunix.net/uid-122937-id-215913.html 1. 线程的挂起和唤醒      挂起实际上是让线程进入“非可执行”状态下,在这个状态下C ...

  5. NABCD模型需求分析

    仓库管理系统的NABCD模型 N-Need仓库管理是与我们日常生活息息相关的问题,随着改革开放的不断深入,经济飞速的发展,企业要想生存.发展,要想在激烈的市场竞争中立于不败之地,没有现代化的管理是万万 ...

  6. 第四次个人作业--必应词典(PC端)分析

    part.1 调研.评测 ·bug简述: 1.初始使用时,我觉得划译功能很方便,可是在QQ和浏览器上多次使用划译功能后,我发现并不是每次划译总会做出翻译,而是显示原文而已.如下图: 2.敲代码是出现b ...

  7. ansible-3 主机清单hosts的设置

    主机清单的设置参考:http://www.ansible.com.cn/docs/intro_inventory.html [ceshi]192.168.220.98log ansible_ssh_h ...

  8. Activiti 学习资料收集

    Activiti工作流引擎使用 http://www.open-open.com/lib/view/open1350460225367.html Activiti初学者教程 http://blog.c ...

  9. Chrome商店Crx离线安装包下载

    第一步:找到Chrome的扩展应用ID 第二步:输入扩展应用ID 第三步:单击 生成 按钮. 第四步:在这里右键另存为即可下载.

  10. The C Programming Language (second edition) 实践代码(置于此以作备份)

    1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...