World is Exploding

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5792

Description


Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a 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

##Source

2016 Multi-University Training Contest 5


##题意:

找出有多少个四元组(a,b,c,d)满足a≠b≠c≠d,a Ad.


##题解:

下面将题意中的Aa Ad称为降序对.
首先会想到如果没有a≠b≠c≠d的限制,那么可以分别找到所有升序对和降序对数目相乘. 所以在有a≠b≠c≠d限制的情况下,需要考虑去重.
枚举元素num[i],考虑以num[i]为升序对右边界的情况:
1. 以num[i]为右边界的升序对个数为:1~i-1中比i小的个数.
2. 降序对:所有降序对 - 包含升序对中元素的降序对.
3. 包含升序对中元素的降序对:Sum((右边比num[j]小 + 左边比num[j]大) + (右边比num[i]小 + 左边比num[i]大)). (j为1~i-1中比num[i]小的数).

接下来的任务是:求出任一数左边比它大、小,右边比它大、小的数的个数各是多少.
这里可以用求逆序数的方法来求. 先将数据离散化.
注意TLE:尽量使用一次树状数组或线段树操作(O(nlogn)) + 多个O(n)的遍历来求得上述四个数组.
若分别使用2次甚至更多次的的线段树操作,将会由于常数太大而TLE(TLE代码附后,使用三次线段树操作).

分别使用left_large,left_small,right_small,right_large来表示上述的四个量. all_less为全部降序对数.
那么结果为:
left_small[i] * all_less - Σ(left_large[i]+right_small[i], left_large[j]+right_small[j]); (其中j为i左边比num[i]小的数,其个数为left_small[i]).
优化:若是直接用上述式子来求,则要再维护一次树状数组求比num[i]小的数的所涉及的重复降序对.
实际上,对于每个num[i],在减号右边left_large[i]+right_small[i]被计数的次数应该是i的右边比num[i]大的数的个数.
所以上式可优化为代码所示的式子:(总共仅需一次树状数组操作)
ans += left_small[i] * (all_less - left_large[i] - right_small[i]);
ans -= right_large[i] * (left_large[i] + right_small[i]);


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 55000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

LL num[maxn], tmpa[maxn];

LL c[maxn];

LL left_large[maxn], left_small[maxn];

LL right_small[maxn], right_large[maxn];

LL tol_cnt[maxn], tol_small[maxn], tol_large[maxn];

LL lowbit(LL x) {

return x & (-x);

}

void update(LL x, LL d) {

while(x <=n) {

c[x] += d;

x += lowbit(x);

}

}

LL get_sum(LL x) {

LL ret = 0;

while(x > 0) {

ret += c[x];

x -= lowbit(x);

}

return ret;

}

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%d", &n) != EOF)
{
for(int i=1; i<=n; i++)
scanf("%d", &num[i]), tmpa[i] = num[i];
sort(tmpa+1, tmpa+1+n); map<LL, LL> mymap;
LL sz = 0; mymap.clear();
for(int i=1; i<=n; i++) {
if(!mymap.count(tmpa[i])) {
sz++;
mymap.insert(make_pair(tmpa[i], sz));
}
} for(int i=1; i<=n; i++) {
num[i] = mymap[num[i]];
} memset(left_large, 0, sizeof(left_large));
memset(right_large, 0, sizeof(right_large));
memset(right_small, 0, sizeof(right_small));
memset(left_small, 0, sizeof(left_small));
memset(tol_small, 0, sizeof(tol_small));
memset(tol_large, 0, sizeof(tol_large));
memset(tol_cnt, 0, sizeof(tol_cnt));
memset(c, 0, sizeof(c)); for(int i=1; i<=n; i++) {
tol_cnt[num[i]]++;
}
for(int i=1; i<=sz; i++) {
tol_small[i] = tol_small[i-1] + tol_cnt[i-1];
}
for(int i=sz; i>=1; i--) {
tol_large[i] = tol_large[i+1] + tol_cnt[i+1];
} LL all_less = 0;
for(int i=1; i<=n; i++) {
left_large[i] = get_sum(sz) - get_sum(num[i]);
right_large[i] = tol_large[num[i]] - left_large[i]; left_small[i] = get_sum(num[i]-1);
right_small[i] = tol_small[num[i]] - left_small[i]; all_less += right_small[i]; update(num[i], 1);
} LL ans = 0;
for(int i=1; i<=n; i++) {
ans += left_small[i] * (all_less - left_large[i] - right_small[i]);
ans -= right_large[i] * (left_large[i] + right_small[i]);
} printf("%I64d\n", ans);
} return 0;

}


####TLE代码:(三次线段树操作)
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 55000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
LL num[maxn], tmpa[maxn];
LL pre[maxn];
LL last[maxn]; struct Tree
{
int left,right;
LL cur;
LL sum;
}tree[maxn<<2]; void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right; if(left==right){
tree[i].sum = 0;
tree[i].cur = 0;
return ;
} int mid=mid(left,right); build(i<<1,left,mid);
build(i<<1|1,mid+1,right); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
tree[i].cur=tree[i<<1].cur+tree[i<<1|1].cur;
} void update(int i,int x,LL d)
{
if(tree[i].left==tree[i].right){
tree[i].sum+=1;
tree[i].cur+=d;
return;
} int mid=mid(tree[i].left,tree[i].right); if(x<=mid) update(i<<1,x,d);
else update(i<<1|1,x,d); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
tree[i].cur=tree[i<<1].cur+tree[i<<1|1].cur;
} LL query(int i,int left,int right)
{
if(tree[i].left==left&&tree[i].right==right)
return tree[i].sum; int mid=mid(tree[i].left,tree[i].right); if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);
} LL query2(int i,int left,int right)
{
if(tree[i].left==left&&tree[i].right==right)
return tree[i].cur; int mid=mid(tree[i].left,tree[i].right); if(right<=mid) return query2(i<<1,left,right);
else if(left>mid) return query2(i<<1|1,left,right);
else return query2(i<<1,left,mid)+query2(i<<1|1,mid+1,right);
} map<LL, LL> mymap; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d", &n) != EOF)
{
for(int i=1; i<=n; i++)
scanf("%d", &num[i]), tmpa[i] = num[i];
sort(tmpa+1, tmpa+1+n); LL sz = 0; mymap.clear();
for(int i=1; i<=n; i++) {
if(!mymap.count(tmpa[i])) {
sz++;
mymap.insert(make_pair(tmpa[i], sz));
}
} for(int i=1; i<=n; i++) {
num[i] = mymap[num[i]];
} fill(pre, pre+n+1, 0);
fill(last, last+n+1, 0); build(1, 1, sz);
for(int i=1; i<=n; i++) {
update(1, num[i], 1);
if(num[i] == sz) continue;
pre[i] = query(1, num[i]+1, sz);
}
build(1, 1, sz);
for(int i=n; i>=1; i--) {
update(1, num[i], 1);
if(num[i] == 1) continue;
last[i] = query(1, 1, num[i]-1);
} LL all_less = 0;
for(int i=1; i<=n; i++) {
all_less += last[i];
} build(1, 1, sz);
LL ans = 0;
for(int i=1; i<=n; i++) {
update(1, num[i], pre[i]+last[i]);
if(num[i] == 1) continue;
LL pre_num = query(1, 1, num[i]-1);
LL pre_sum = query2(1, 1, num[i]-1); ans += pre_num * (all_less - pre[i] - last[i]) - pre_sum;
} printf("%I64d\n", ans);
} return 0;
}

HDU 5792 World is Exploding (树状数组)的更多相关文章

  1. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  2. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  3. hdu 5792 World is Exploding 树状数组+离散化+容斥

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  7. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  8. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  9. hdu 5592 ZYB's Game 树状数组

    ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...

  10. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

随机推荐

  1. NDK(17)让ndk支持完整C++,exception,rtti,

    C++ Support The Android platform provides a very minimal C++ runtime support library (/system/lib/li ...

  2. VS2015中快捷注释代码块

    注释ctrl+k   ctrl+c 反注释ctrl+k ctrl+u 需要注意的是第二个ctrl不能省略的

  3. LA 3357 (递推 找规律) Pinary

    n位不含前导零不含连续1的数共有fib(n)个,fib(n)为斐波那契数列. 所以可以预处理一下fib的前缀和,查找一下第n个数是k位数,然后再递归计算它是第k位数里的多少位. 举个例子,比如说要找第 ...

  4. hdu 3535 AreYouBusy

    // 混合背包// xiaoA想尽量多花时间做ACM,但老板要求他在T时间内做完n堆工作,每个工作耗时ac[i][j],// 幸福感ag[i][j],每堆工作有m[i]个工作,每堆工作都有一个性质,/ ...

  5. ios第三方开源库

    1.AFNetworking 目前比较推荐的iOS网络请求组件,默认网络请求是异步,通过block回调的方式对返回数据进行处理. 2.FMDB 对sqlite数据库操作进行了封装,demo也比较简单. ...

  6. 【转】话说我打算一天学完object c语法,系列1--------来自书Objective-c程序设计

    原文网址:http://blog.csdn.net/zengraoli/article/details/8993466 类型: NSString NSInteger NSLong控制台输出 NSObj ...

  7. Oracle 不同故障的恢复方案

    之前在Blog中对RMAN 的备份和恢复做了说明,刚看了下,在恢复这块还有知识点遗漏了. 而且恢复这块很重要,如果DB 真要出了什么问题,就要掌握对应的恢复方法. 所以把DB的恢复这块单独拿出来说明一 ...

  8. [Everyday Mathematics]20150129

    计算下列积分 $$\bex \int_a^b (x-a)^2(b-x)^3\rd x. \eex$$

  9. MultiSet

    Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口.Guava中定义的新集合有: Multi ...

  10. setImageResource和setImageDrawable区别

    ImageView设置图片的方式有很多钟,可以在xml里面写android:src=”@drawable/xxx”,也可以在java代码里面设置. 在java里面的设置方式也有多种,方法包括:setI ...