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 树状数组查询后缀、最值的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Beta Round #12 (Div 2 Only)

    Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 #include<bits/stdc++ ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  7. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  8. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. codeforces 469B Chat Online 解题报告

    题目链接:http://codeforces.com/problemset/problem/469/B 题目意思:给出 Little Z 的上线时间段,分别是[a1, b1], [a2, b2],.. ...

  2. Python: PS 滤镜--旋转模糊

    本文用 Python 实现 PS 滤镜中的旋转模糊,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/392 ...

  3. SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)

    You are given a sequence A[1], A[2], ..., A[N]. (0 ≤ A[i] < 231, 1 ≤ N ≤ 12000). A query is defin ...

  4. bzoj 1798 Seq 维护序列seq —— 线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 这题还4A... 注意:cnt 从1开始:各种模 p:乘法标记初始值是 1:可能乘 0 ...

  5. sqlServer:convert()函数

    在oracle中对于时间格式的转换用到的比较多,相对也比较了解,现在换了新的项目组,使用sqlserver数据库,需要对这个数据库的一些常用函数进行重新学习和熟悉,现在根据w3c及网上博文对conve ...

  6. hibernate Criteria中or和and的用法

    /s筛选去除无效数据 /*      detachedCriteria.add( Restrictions.or( Restrictions.like("chanpin", &qu ...

  7. HDOJ-2058

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. csvreader 来操作csv文件

    http://www.cnitblog.com/rd416/archive/2010/07/08/47248.html

  9. mysql使用小结

    一.修改 mysql 的 root 密码 mysql> use mysql;  mysql> update user set password=password('123456') whe ...

  10. Bootstrap 自适应排列顺序

    一.前用 我们在做一些页面的设计时,总会想到自适应的问题.其实 Bootstrap 框架就很好的融合这个问题了.下面是我学习 Bootstrap 的总结. 二.问题来源 我为什么会遇见这个问题,是因为 ...