树状数组求逆序和模板
C++ code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define lowbit(i) (i & -i); using namespace std; struct lines { long l, r, i; }line[30001]; bool cmp_1(lines, lines); bool cmp_2(lines, lines); int main() { long A, B, n, i, k, b, T[30001], m, j; while(scanf("%ld%ld", &A, &B) == 2) { scanf("%ld", &n); for(i = 1; i <= n; i++) { scanf("%ld%ld", &k, &b); line[i].l = k * A + b; line[i].r = k * B + b; T[i] = lowbit(i); } sort(line + 1, line + n + 1, cmp_1); for(i = 1; i <= n; i++) line[i].i = i; sort(line + 1, line + n + 1, cmp_2); T[0] = m = 0; for(i = 1; i <= n; i++) { j = line[i].i - 1; while(j) { m += T[j]; j -= lowbit(j); } j = line[i].i; while(j <= n) { T[j]--; j += lowbit(j); } } printf("%ld\n", n + m + 1); } return 0; } bool cmp_1(lines a, lines b) { return a.l > b.l; } bool cmp_2(lines a, lines b) { return a.r > b.r; }