楼兰图腾

【题目描述】

在完成了分配任务之后,西部 314 来到了楼兰古城的西部。相传很久以前这片土地上(比楼
兰古城还早)生活着两个部落,一个部落崇拜尖刀(‘ V’ ),一个部落崇拜铁锹(‘∧’ ),
他们分别用 V 和∧的形状来代表各自部落的图腾。
西部 314 在楼兰古城的下面发现了一幅巨大的壁画,壁画上被标记出了 n 个点,经测量发
现这 N 个点的水平位置和竖直位置是两两不同的。西部 314 认为这幅壁画所包含的信息与
这 N 个点的相对位置有关,因此不妨设坐标分别为(1,y1),(2,y2),...,(n,yn)其中 y1..yn
是 1 到 n 的一个排列。
西部 314 打算研究这幅壁画中包含着多少个图腾, 其中 V 图腾的定义如下(注意:图腾的
形式只和这三个纵坐标的相对大小排列顺序有关) 1≤i<j<k≤n 且 yi>yj,yj<yk;而崇拜∧
的部落的图腾被定义为 1≤i<j<k≤n 且 yi<yj,yj>yk;
西部 314 想知道,这 n 个点中两个部落图腾的数目。因此,你需要编写一个程序来求出 V
的个数和∧的个数。

Input Data

第一行一个数 n
第二行是 n 个数,分别代表 y1,y2,...,yn

Output Data

两个数,中间用空格隔开,依次为 V 的个数和∧的个数。

Input

5
1 5 3 2 4

Output

3 4

Data Limit

10%的数据 n≤600
40%的数据 n≤5000
100%的数据 n≤200000

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include <sstream>
#include<vector>
#include<cmath>    
#include<stack>
#include<time.h>
#include<ctime>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define LD long double
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define inf 1<<30
#define maxn 1<<17+1
#define  ll unsigned long long 
int c[200009] = {};
int a[200009] = {};
LL R[200009] = {};
LL L[200009] = {};
int n;
int lowbit(int x) {
    return x & (-x);
}
int get_sum(int x) {//获得前x的个数之和
    int sum = 0;
    while (x > 0) {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}
void add(int x){//在第x的位置上加一
    while (x <= n){
        c[x]++;
        x += lowbit(x);
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    ms(c);
    for (int i = n; i >= 1; i--) {//依次获得第i个数的右边比他小的数的数量
        R[i] = get_sum(a[i] - 1);
        add(a[i]);
    }
    ms(c);
    for (int i = 1; i <= n; i++) {//依次获得第i个数的左边比他小的数的数量
        L[i] = get_sum(a[i] - 1);
        add(a[i]);
    }
    LL ans1 = 0;//中间小两头大的数量 ,即v
    LL ans2 = 0;//中间大两头小的数量 ,即Λ
    for (int i = 1; i <= n; i++) {
        ans1 += (LL)(i - L[i] - 1) * (n - i - R[i]);
        ans2 += (LL)(L[i]) * R[i];
    }
    cout << ans1 << " " << ans2 << endl;
}

 

posted @ 2021-01-26 19:36  夜灯长明  阅读(239)  评论(0编辑  收藏  举报