首页 > 代码库 > 【CDOJ931】Car race game(树状数组求逆序)

【CDOJ931】Car race game(树状数组求逆序)

题目连接:http://acm.uestc.edu.cn/#/problem/show/931

OJ评判系统有些坑,不支持__int64以及输出的%I64d大家注意。全开long long也会TLE,比较坑。逆序的基础操作题,不错。

 

 1 #include <bits/stdc++.h> 2 #define MAX 100010 3 using namespace std; 4  5 int s[MAX * 10]; 6  7 struct Node { 8     int x, v; 9 } node[MAX];10 11 bool cmp (Node a, Node b) {12     if (a.x == b.x) {13         return a.v > b.v;14     }15     return a.x < b.x;16 }17 18 int lowbit (int x) {19     return x & (-x);20 }21 22 int sum (int p) {23     int ans = 0;24     while (p) {25         ans += s[p];26         p -= lowbit(p);27     }28     return ans;29 }30 31 void add (int p, int del) {32     while (p <= 1000000) {33         s[p] += del;34         p += lowbit(p);35     }36 }37 38 int main () {39     int n, i;40     while (scanf("%d", &n) != EOF) {41         long long ans = 0;42         for (i = 0; i < n; ++ i) {43             scanf("%d %d", &node[i].x, &node[i].v);44         }45         sort (node, node + n, cmp);46         memset(s, 0, sizeof(s));47         for (i = 0; i < n; ++ i) {48             ans = ans + i - sum(node[i].v);49             add(node[i].v, 1);50         }51         printf ("%lld\n", ans);52     }53     return 0;54 }

 

【CDOJ931】Car race game(树状数组求逆序)