hdu 5792 World is Exploding (树状数组)



Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:  abcd,1a<bn,1c<dn,Aa<Ab,Ac>Ad .
 

Input
The input consists of multiple test cases. 
Each test case begin with an integer n in a single line.

The next line contains n integers  A1,A2An .
1n50000
0Ai1e9
 

Output
For each test case,output a line contains an integer.
 

Sample Input
  
  
4 2 4 1 3 4 1 2 3 4
 

Sample Output
  
  
1 0
 

思路:算出总的,减去不合法的

#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n;
struct BIT
{

    double c[N+20];
    BIT()
    {
        memset(c , 0 , sizeof(c));
    }
    void init()
    {
        for(int i = 0 ; i <= n ; ++i) c[i] = 0;
    }
    int lowbit(int x)
    {
        return  x & (-x);
    }
    void update(int pos , int x)
    {
        while(pos <= n)
        {
            c[pos] += x;
            pos += lowbit(pos);
        }
    }
    int sum(int pos)
    {
        int ans = 0 ;
        while(pos)
        {
            ans += c[pos];
            pos -= lowbit(pos);
        }
        return ans;
    }

} t;

int a[N];
int pos[N];
int big[N];
int small[N];
int x[N];
int b[N],c[N];
int main()
{
    while(~scanf("%d",&n))
    {
        t.init();

        memset(pos, 0, sizeof(pos));
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d",&a[i]);
           // pos[a[i]] = i;
           x[i] = a[i];
        }

        sort(x + 1 , x + 1 + n);
        for(int i = 1 ; i <= n ; ++i)
        {
            pos[i] = lower_bound(x + 1 , x + n + 1 , a[i]) - (x + 1) + 1;
            //cerr << pos[i] << endl;
            a[i] = pos[i];
            b[a[i]] = c[a[i]] = 0;
        }

        int now;
        long long sum1 = 0, sum2 = 0;
        for(int i = 1; i <= n; ++i)
        {
            now = t.sum(a[i] - 1);
            t.update(a[i], 1);
            big[i] = now;
            small[i] = i - now - 1 - b[a[i]];
            sum1 += now;

            b[a[i]]++;

        }
        t.init();
        for(int i = n; i >= 1; --i)
        {
            now = t.sum(a[i] - 1);
            t.update(a[i], 1);
            small[i] += now;
            big[i] += (n -i +1) - now - 1 -c[a[i]];
            sum2 += now;

            c[a[i]]++;
        }
        long long ans = sum1 * sum2;
        for(int i = 1; i <= n; ++i)
        {
            //cout << "num: " << i << endl;
            //cout << big[i] << ' ' << small[i] << endl;
            ans -= (big[i] * small[i]);
        }
        printf("%I64d\n",ans);
        //cout << sum1 <<' ' <<sum2 << endl;
        //for()
    }
    return 0;
}



Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:  abcd,1a<bn,1c<dn,Aa<Ab,Ac>Ad .
 

Input
The input consists of multiple test cases. 
Each test case begin with an integer n in a single line.

The next line contains n integers  A1,A2An .
1n50000
0Ai1e9
 

Output
For each test case,output a line contains an integer.
 

Sample Input
   
   
4 2 4 1 3 4 1 2 3 4
 

Sample Output
   
   
1 0
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值