HDU 5792 World is Exploding (离散化+树状数组)
题意:给定 n 个数,让你数出 a < b && c < d && a != b != c != d && Aa < Ab && Ac > Ad。
析:首先,给的数太大了,先要进行离散化处理,然后先算出Aa < Ab 和 Ac > Ad。这可以用树状数组解决,一个正向的,一个反向,同时再求出四种数,然后减去,就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 100;
const int mod = 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> v; int a[maxn];
int sum[2][maxn<<1]; int lowbit(int x){ return -x&x; } void add(int pos, int x){
while(x <= n){
++sum[pos][x];
x += lowbit(x);
}
} int query(int pos, int x){
int ans = 0;
while(x){
ans += sum[pos][x];
x -= lowbit(x);
}
return ans;
} int getPos(int x){
return lower_bound(v.begin(), v.end(), x) - v.begin();
} int b[maxn], c[maxn], d[maxn], e[maxn]; int main(){
while(scanf("%d", &n) == 1){
v.cl; ms(sum, 0);
v.pb(-1);
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
v.pb(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end()); LL ans = 0;
LL tmp1 = 0, tmp2 = 0;
LL tmp3 = 0, tmp4 = 0, tmp5 = 0;
for(int i = 0; i < n; ++i){
int pos = getPos(a[i]);
b[i] = query(0, pos-1);
tmp1 += b[i]; // min
c[i] = i - query(0, pos);
tmp3 += (LL)b[i] * c[i];
add(0, pos);
} for(int i = n-1; i >= 0; --i){
int pos = getPos(a[i]);
d[i] = query(1, pos-1);
tmp2 += d[i]; //right min
e[i] = (n-i-1) - query(1, pos);
tmp4 += (LL)d[i] * e[i];
//tmp4 += (n-i-1) - query(1, pos); // right max
add(1, pos);
} LL tmp6 = 0;
for(int i = 0; i < n; ++i){
tmp5 += (LL)c[i] * e[i];
tmp6 += (LL)b[i] * d[i];
} ans = tmp1 * tmp2 - tmp3 - tmp4 - tmp5 - tmp6;
printf("%I64d\n", ans); }
return 0;
}
HDU 5792 World is Exploding (离散化+树状数组)的更多相关文章
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 5862 Counting Intersections(离散化 + 树状数组)
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
随机推荐
- ColorPic 一套簡單好用的顏色選擇器!
做美工的時候,常常會有配色.抓取顏色及獲取顏色代碼的困擾,專業人士可能有很好的工具來協助,但對於偶爾需要或非經常接觸美工的人來說,即便是有很好的工具,也還要花很多時間進行學習,常常就是看到一個漂亮的顏 ...
- 用CSS绘制最常见的40种形状和图形
今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和网站小图标,真的非常强大,分享给大家. Square(正方 ...
- java实现时钟方法汇总
import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; import java ...
- python3.5+flask+mysql
该篇博客配置环境为:python版本3.5,flask2.0,python3中已经不再支持MySQLdb模块,所有这里我用了pymysql,所有使用前应该 安装pymysql:pip install ...
- CentOS7.6安装JDK(Openjdk) - mvn package报错汇总
错误一: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK ...
- 一个不明觉厉的貌似包含很多linux资料索引的网页
http://man.lupaworld.com/content/other/Linux/linuxmanage/node108.html 貌似是个官方的doc之类的...
- 软件项目需求调研中的5W+1H定律案例分析
转:http://www.educity.cn/se/620915.html 对于软件的需求调研活动,虽然曾经写过三篇相关的需求管理文章,出发角度是从整体的需求管理过程考虑:在引入CMM(二)需求管理 ...
- 佛祖保佑、永无BUG!!!
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...
- Java设计模式学习06——静态代理与动态代理(转)
原地址:http://blog.csdn.net/xu__cg/article/details/52970885 一.代理模式 为某个对象提供一个代理,从而控制这个代理的访问.代理类和委托类具有共同的 ...
- Python的索引迭代
Python中,迭代永远是取出元素本身,而非元素的索引. 对于有序集合,元素确实是有索引的.有的时候,我们确实想在 for 循环中拿到索引,怎么办? 方法是使用 enumerate() 函数: > ...