When you understand the question, you will find that this is a question of reverse order. Use a as the first keyword, and B as the second keyword to sort the original input data in ascending order. Then, for each B, check whether there are several bigger ones.
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace STD; struct way {int A, B; bool operator <(const struct way t) const {return a <t. A | A = T. A & B <t. B ;}} P [1000005]; int C [1005], t, n, m, K; inline int lowbit (int K) {return K & (-k );} void insert (int I, int d) {While (I <= m) {C [I] + = D; I + = lowbit (I );}} int getsum (int I) {int T = 0; while (I) {T + = C [I]; I-= lowbit (I) ;}return t ;} int main () {scanf ("% d", & T); int c = 1; _ int64 sum; while (t --) {scanf ("% d", & N, & M, & K); For (INT I = 0; I <K; I ++) scanf ("% d", & P [I]. a, & P [I]. b); sort (p, p + k); sum = 0; memset (C, 0, sizeof (INT) * (m + 1 )); for (INT I = 0; I <K; I ++) {sum + = getsum (M)-getsum (P [I]. b); insert (P [I]. b, 1) ;}printf ("Test Case % d: % i64d \ n", C ++, sum) ;}return 0 ;}