Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
http://codeforces.com/problemset/problem/12/D
这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了。
设三个权值分别是b[1], b[2], b[2];
首先,先把b[1]值离散化,离散成一个个id,那么只能是在id比较大的地方找了。然后把b[2]排序,倒序查询,也就是先查询最大的,当然它是没可能自杀的,因为它已经最大了,然后查询完后,把它压进bit后,以后在bit查询,就不需要管b[2]了,因为在bit里面的b[2]永远都是较大者,其实这是一个常用的方法啦。hack点是相同的值不能提前更新,细节看代码 + 慢慢debug
现在说说bit的作用,bit维护的是b[3],因为我们已经离散化b[1]了,那么每一个元素的下标就相当于离散化的b[1],所以加进去BIT的时候用id做下标即可。bit维护后缀最大值。
hack点的意思是,假如你的a[i].b[2] == a[j].b[2],那么不能在查询a[j]前提前把a[i]加入bit,因为bit查询默认是b[2]成立的,但是提前更新了的话,不成立。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#include <stack>
const int maxn = 5e5 + ;
int c[maxn], en;
int lowbit(int x) {
return x & (-x);
}
void upDate(int pos, int val) {
while (pos) {
c[pos] = max(c[pos], val);
pos -= lowbit(pos);
}
}
int ask(int pos) {
int ans = -;
while (pos <= en) {
ans = max(ans, c[pos]);
pos += lowbit(pos);
}
return ans;
}
struct Node {
int a[], id;
}a[maxn];
stack<int> st;
bool cmp0(struct Node a, struct Node b) {
return a.a[] < b.a[];
}
bool cmp1(struct Node a, struct Node b) {
return a.a[] < b.a[];
} void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) scanf("%d", &a[i].a[]);
for (int i = ; i <= n; ++i) scanf("%d", &a[i].a[]);
for (int i = ; i <= n; ++i) scanf("%d", &a[i].a[]);
sort(a + , a + + n, cmp0);
en = ;
a[].id = en;
for (int i = ; i <= n; ++i) {
if (a[i].a[] == a[i - ].a[]) {
a[i].id = en;
} else a[i].id = ++en;
}
en += ;
memset(c, -, sizeof c);
sort(a + , a + + n, cmp1);
// for (int i = 1; i <= n; ++i) {
// cout << a[i].a[1] << " " << a[i].a[2] << " " << a[i].a[3] << " " << a[i].id << endl;
// }
st.push(n);
int ans = ;
for (int i = n - ; i >= ; --i) {
if (a[i].a[] == a[i + ].a[]) {
st.push(i);
int res = ask(a[i].id + );
ans += res > a[i].a[];
} else {
while (!st.empty()) {
int res = st.top();
st.pop();
upDate(a[res].id, a[res].a[]);
}
int res = ask(a[i].id + );
ans += res > a[i].a[];
st.push(i);
// upDate(a[i].id, a[i].a[3]);
}
}
cout << ans << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值的更多相关文章
- Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组
http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0 终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1 ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball sort/map
D. Ball Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/12/D D ...
- Codeforces Beta Round #12 (Div 2 Only)
Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 #include<bits/stdc++ ...
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)
[链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
随机推荐
- ubuntu熟悉过程中遇到一些小问题记录一下
0. 使用su命令时提示: authentication failur 安装ubuntu系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在终端中输入如下命令:sudo p ...
- AutoIt:应用WMI接口,打印远程机器安装的所有应用程序
Dim $computer ="computer name" Dim $Remoteuser ="Login account" Dim $Remotepass ...
- 关于yolov3 训练输出值
Region xx: cfg文件中yolo-layer的索引: Avg IOU:当前迭代中,预测的box与标注的box的平均交并比,越大越好,期望数值为1: Class: 标注物体的分类准确率,越大越 ...
- nvidia-smi 查看GPU信息字段解读
第一栏的Fan:N/A是风扇转速,从0到100%之间变动,这个速度是计算机期望的风扇转速,实际情况下如果风扇堵转,可能打不到显示的转速.有的设备不会返回转速,因为它不依赖风扇冷却而是通过其他外设保持低 ...
- bzoj2957楼房重建——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树维护原点到楼顶的斜率,可以知道答案就是从原点开始斜率递增的个数: 记录一个mx数 ...
- 4.oracle正确卸载步骤
oracle 11g如何完全卸载 方法/步骤1: 停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 方法/步骤2: 在开始菜单中,找到oracle-> ...
- matlab 2017版本修改帮助文档为离线
安装matlab2017a后发现,帮助文档为在线版本,必须关联账号. 经过查询资料,帮助文档默认是使用web, on mathworks.com 可以将帮助文档改为离线版本. 具体修改方法: Pref ...
- 多线程之----定时器TIMER
结上一篇 多线程的简单介绍 http://www.cnblogs.com/duanxiaojun/p/6595847.html 在上一讲中我主要是对多线程学习这个系列做了一个大致的学习计划,然后对 ...
- java中有关socket通信的学习笔记
最近做的项目中使用到了一些基于java的socket长连接的一些功能,用来穿透有关行业的网闸.用到了也就学习了一下,下面是对学习内容的一个笔记,记录一下也希望有兴趣的同学可以参考一下,加深对javas ...
- jQuery EasyUI API 中文文档 - Tree树使用介绍
用 $.fn.tree.defaults 重写了 defaults. 依赖 draggable droppable 用法 Tree 能在 <ul> 元素里定义,此标记可以定义为叶节点和子节 ...