zoukankan      html  css  js  c++  java
  • Super OJ 序列计数

    题意:

    给出序列 a1,a2,……an(0≤ai≤109),求三元组(ai,aj,ak)(1≤i<j<k≤n)满足 ai<aj>ak 的数量。

    分析:

    开两个(BIT),分别维护前面比它小的和后面比它大的,然后组合计数一下即可

    代码:

    #include<bits/stdc++.h>
    #define lowbit(x) (x & (-x))
    #define ll long long
    #define N (100000 + 5)
    using namespace std;
    inline int read() {
    	int cnt = 0, f = 1; char c = getchar();
    	while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    	while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
    	return cnt * f;
    }
    int n, q, a[N], b[N << 1];
    ll ans;
    void pre() {
    	sort(b + 1, b + n + 1);
    	q = unique(b + 1, b + n + 1) - b - 1;
    	for (register int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + q + 1, a[i]) - b;
    }
    struct node {
    	int BIT[N];
    	void insert (int x) {for (; x <= n; x += lowbit(x)) ++BIT[x];}
    	void Delete (int x) {for (; x <= n; x += lowbit(x)) --BIT[x];}
    	ll query(int x) {ll ans = 0; for (; x; x -= lowbit(x)) ans += BIT[x]; return ans;}
    }BIT1, BIT2;
    int main() {
    	n = read();
    	for (register int i = 1; i <= n; ++i) a[i] = b[i] = read();
    	pre();
    	for (register int i = 1; i <= n; ++i) BIT2.insert(a[i]);
    	BIT1.insert(a[1]);
    	for (register int i = 2; i <= n; ++i) {
    		BIT1.insert(a[i]);
    		BIT2.Delete(a[i - 1]);
    		ans += (BIT1.query(a[i] - 1) * (BIT2.query(a[i] - 1)));
    //		cout<<BIT1.query(a[i] - 1)<< " " << BIT2.query(a[i])<<"
    ";
    	}
    	printf("%lld", ans);
    	return 0;	
    }
    
  • 相关阅读:
    采购标准流程及底层分析
    ORACLE FORM ZA 常用子程序
    在R12中实现多OU编程
    FORM未找到数据的原因
    在Oracle的FORM中高亮显示鼠标点击或光标所在的行
    MPICH运行程序时出错之解决方法
    两个基于C++的MPI编辑例子
    面向对象PHP面向对象的特性
    PHP 数组遍历 foreach 语法结构
    php BC高精确度函数库
  • 原文地址:https://www.cnblogs.com/kma093/p/11791403.html
Copyright © 2011-2022 走看看