题目描述
给定N,以及A1,A2,……AN,求所有的数对(i,j)同时满足:
(1)i<j
(2)2Ai>Aj
思路
对于逆序对有显然的线段树做法
#include#define N 4000001 #define LL long long LL t[N]; int a[N]; int insert(int p, int l, int r, int x) { if (l == r) { t[p] = 1; return 0; } int mid = (l + r) >> 1; if (x <= mid) insert(p << 1, l, mid, x); else insert(p << 1 | 1, mid + 1, r, x); t[p] = t[p << 1] + t[p << 1 | 1]; } LL find(int p, int l, int r, int x, int y) { if (x <= l && y >= r) return t[p]; int mid = (l + r) >> 1; if (y <= mid) return find(p << 1, l, mid, x, y); else if (x > mid) return find(p << 1 | 1, mid + 1, r, x, y); else return find(p << 1, l, mid, x, mid) + find(p << 1 | 1, mid + 1, r, mid + 1, y); } int main() { int n; scanf("%d", &n); LL ans = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); ans += find(1, 1, 10 * n, a[i] / 2 + 1, 10 * n); insert(1, 1, 10 * n, a[i]); } printf("%lld\n", ans); }
Comments NOTHING