World is Exploding (容斥 + 统计)
Posted wethura
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了World is Exploding (容斥 + 统计)相关的知识,希望对你有一定的参考价值。
题意:满足题目中的式子,a < b && c < d && Va < Vb && Vc > Vd
思路:先求不讨论位置重合的情况,把对应的2种关系相乘,然后得到的答案减去重合的地方。不想解释,我特么改着改着就对了。都不知道哪里错了,叫对了数据还是找不到。因为只有一组数据出错。
#include<bits/stdc++.h> using namespace std; const int maxn = 5e4 + 7; int tr[maxn], in[maxn], sar[maxn], ls[maxn], lb[maxn], rs[maxn], rb[maxn]; int n, m; int lowbit(int x){ return x & -x; } void add(int x, int d){ while(x <= n){ tr[x] += d; x += lowbit(x); } } int query(int x){ int ret = 0; while(x){ ret += tr[x]; x -= lowbit(x); } return ret; } int main(){ while(~scanf("%d", &n)){ long long ans = 0, sum1 = 0, sum2 = 0; for(int i = 1; i <= n; i ++) scanf("%d", &in[i]), sar[i] = in[i]; sort(sar + 1, sar + n + 1); m = unique(sar + 1, sar + n + 1) - sar; for(int i = 1; i <= n; i ++) in[i] = lower_bound(sar + 1, sar + n + 1, in[i]) - sar; memset(tr, 0, sizeof(tr)); for(int i = 1; i <= n; i ++){ ls[i] = query(in[i] - 1); lb[i] = query(n) - query(in[i]); sum1 += ls[i]; sum2 += lb[i]; add(in[i], 1); } for(int i = 1; i <= n; i ++){ rs[i] = query(in[i] - 1); rb[i] = query(n) - query(in[i]); add(in[i], -1); } ans = sum1 * sum2; for(int i = 1; i <= n; i ++){ ans -= 1LL * ls[i] * lb[i]; ans -= 1LL * ls[i] * rs[i]; ans -= 1LL * rb[i] * lb[i]; ans -= 1LL * rs[i] * rb[i]; } printf("%lld ",ans); } return 0; }
以上是关于World is Exploding (容斥 + 统计)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5792 World is Exploding (离散化+树状数组)
Codeforces 839D Winter is here - 暴力 - 容斥原理
ZOJ 1442 Dinner Is Ready 容斥原理 + java大数
(容斥)Codeforces Round #428 (Div. 2) D. Winter is here
Codeforces 839D Winter is here(容斥原理)
Codeforces Round #428 (Div. 2) D. Winter is here[数论II][容斥原理]