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 ...
随机推荐
- codeforces B. Ciel and Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/322/B 题目意思:给定红花.绿花和蓝花的朵数,问组成四种花束(3朵红花,3朵绿花,3朵蓝花,1朵红花+1 ...
- html5--3.10 input元素(9)
html5--3.10 input元素(9) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 ...
- YCSB-mapkeeper
首先 https://github.com/brianfrankcooper/YCSB/issues/885 最终是使用ycsb-0.1.4 版本进行,这个版本自带jar包 https://githu ...
- 百度API从经纬度坐标到地址的转换服务
/// <summary> /// 百度API从经纬度坐标到地址的转换服务 /// </summary> /// <param name="lng"& ...
- c++再探string之eager-copy、COW和SSO方案
在牛客网上看到一题字符串拷贝相关的题目,深入挖掘了下才发现原来C++中string的实现还是有好几种优化方法的. 原始题目是这样的: 关于代码输出正确的结果是()(Linux g++ 环境下编译运行) ...
- 选择合适的innodb_log_file_size
如果对 Innodb 数据表有大量的写入操作,那么选择合适的 innodb_log_file_size 值对提升MySQL性能很重要.然而设置太大了,就会增加恢复的时间,因此在MySQL崩溃或者突然断 ...
- HTML head元素
head标签中可以包含的标签元素有: <title>:定义html页面的标题 <meta>: <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解 ...
- Aaronson
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- sqlserver——视图
数据库中的视图是一个虚拟表.同真实的表一样,视图包含一系列带有名称的列和行数据,行和列数据用来自由定义视图和查询所引用的表,并且在引用视图时动态产生.本篇将通过一些实例来介绍视图的概念,视图的作用,创 ...
- Linux系统调用及其效率
操作系统相关概念: 操作系统---管理计算机硬件与软件资源的软件,是用户与系统操作交互的接口,为在它上面运行的程序提供服务. 操作系统内核 ----操作系统的核心.负责管理系统的进程.内核.设备驱动程 ...