hdu-5792 World is Exploding(容斥+树状数组)

题目链接:

World is Exploding

Time Limit: 2000/1000 MS (Java/Others)  

  Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,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,A2?An.
1≤n≤50000
0≤Ai≤1e9

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

题意:

问符合题目给的四元组有多少个;

思路:

容斥,先算出a,b,c,d满足Aa<Ab&&Ac<Ad的个数,再减去a==c,a==d,b==c,b==d的个数,就是答案了,因为不可能有两个相等的出现;然后就是用树状数组

求pres[i],preb[i],nexs[i],nexb[i];分别表示第i个数前边比它小,比它大,后面比它小比它大的个数具体的看代码吧;

AC代码:

/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=5e4+10;
const int maxn=1e3+14;
const double eps=1e-8;

int n,fa[N],pres[N],preb[N],nexs[N],nexb[N],sum[N];
struct node
{
    int a,id;
}po[N];
int cmp(node x,node y)
{
    if(x.a==y.a)return x.id<y.id;
    return x.a<y.a;
}
int cmp1(node x,node y)
{
    if(x.a==y.a)return x.id<y.id;
    return x.a>y.a;
}

int lowbit(int x){return x&(-x);}

inline void update(int x)
{
    while(x<=n)
    {
        sum[x]++;
        x+=lowbit(x);
    }
}
int query(int x)
{
    int s=0;
    while(x)
    {
        s+=sum[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
        while(scanf("%d",&n)!=EOF)
        {
            For(i,1,n)read(po[i].a),po[i].id=i;
            sort(po+1,po+n+1,cmp);
            po[0].a=-1;
            mst(sum,0);
            For(i,1,n)
            {
                if(po[i].a==po[i-1].a)fa[i]=fa[i-1];
                else fa[i]=i;
                pres[po[i].id]=query(po[i].id)-(i-fa[i]);
                nexs[po[i].id]=fa[i]-1-pres[po[i].id];
                update(po[i].id);
            }
            mst(sum,0);
            sort(po+1,po+n+1,cmp1);
            For(i,1,n)
            {
                if(po[i].a==po[i-1].a)fa[i]=fa[i-1];
                else fa[i]=i;
                preb[po[i].id]=query(po[i].id)-(i-fa[i]);
                nexb[po[i].id]=fa[i]-1-preb[po[i].id];
                update(po[i].id);
            }
            sort(po+1,po+n+1,cmp1);
            LL ans1=0,ans2=0,ans;
            For(i,1,n)
            {
                ans1=ans1+pres[i];
                ans2=ans2+preb[i];
            }
            ans=ans1*ans2;
            For(i,1,n)
            {
                ans=ans-nexs[i]*nexb[i];//a==c
                ans=ans-preb[i]*nexb[i];//a==d
                ans=ans-pres[i]*nexs[i];//b==c
                ans=ans-pres[i]*preb[i];//b==d
            }
            cout<<ans<<endl;
        }
        return 0;
}

  

时间: 2024-10-11 15:17:42

hdu-5792 World is Exploding(容斥+树状数组)的相关文章

bzoj4361:isn(dp+容斥+树状数组)

题面 darkbzoj 题解 \(g[i]\)表示长度为\(i\)的非降序列的个数 那么, \[ ans = \sum_{i=1}^{n}g[i]*(n-i)!-g[i+1]*(n-i-1)!*(i+1) \] 怎么求\(g[i]\)呢 设\(f[i][j]\)为长度为\(i\)的非降序列,以最后一个数是\(j\)的数量 \(f[i][j] = \sum f[i-1][k](k<=j)\) 这样是\(O(n^3)\) 因为带修改,所以树状数组优化转移 复杂度:\(O(n^2logn)\) Cod

HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences                                  Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                             

hdu 3584 二进制0,1反转 三维树状数组 及三维树状数组模板

先贴自己类比二维树状数组写的三维树状数组模板: 开始的时候循环体内j=y,k=z,没写,以为自己思路错了,,,hehe..... 更高维的树状数组以此类比 const int MAXN = 100+10; int c[MAXN][MAXN][MAXN];int X,Y,Z; int N; inline int lowbit(int x){return x&(-x);} void update(int x, int y, int z, int v) { int i=x,j=y,k=z; while

HDU 4417 类似求逆序数的树状数组

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2250    Accepted Submission(s): 1092 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)

题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路径上有几条桥. 解题思路: 这个题目有很多次操作,包含查询和删边两类,首先想到的是连通分量加缩点.如果按照顺序来,删边时候求桥就是问题了.所以可以离线处理,然后一边记录答案一边加边缩点. 对于一个图,把连通分量缩成一个点后,这个图就成为了一棵树, 然后深度差就等于桥的数目.查询的时候对于(u, v)

hdu 1556 Color the ball (扫描线+树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14237    Accepted Submission(s): 7120 Problem Description N个气球排成一排,从左到右依次编号为1,

HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army.

HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Problem Description The h-index of an author is the largest h where he has at least h papers with citations not les