标签:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5697 | Accepted: 2481 |
Description
Input
Output
Sample Input
4 3 1 2 5 2 6 4 3
Sample Output
57
Source
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define MAXN 20005 struct P{ ll v, p; bool operator < (const P& t) const { return t.v > v; } }cow[MAXN]; ll c_ount[MAXN] = {0}; ll total[MAXN] = {0}; ll sum_tot[MAXN] = {0}; int n; ll lowbit(ll x) { return x & (-x); } void add(int x, int d, ll c[]) { while(x < MAXN) { c[x] += d; x += lowbit(x); } } ll Sum(ll x, ll c[]) { ll ret = 0; while(x > 0) { ret += c[x]; x -= lowbit(x); } return ret; } int main() { scanf("%d", &n); repu(i, 1, n + 1) scanf("%lld%lld", &cow[i].v, &cow[i].p); sort(cow + 1, cow + n + 1); repu(i, 1, n + 1) sum_tot[i] = sum_tot[i - 1] + cow[i].p; ll sum = 0, num_cow = 0, sum_total = 0; add(cow[1].p, 1, c_ount); add(cow[1].p, cow[1].p, total); repu(i, 2, n + 1) { num_cow = Sum(cow[i].p, c_ount); sum_total = Sum(cow[i].p, total); sum += cow[i].v * (num_cow * cow[i].p - sum_total + (sum_tot[i - 1] - sum_total - (i - 1 - num_cow) * cow[i].p)); add(cow[i].p, 1, c_ount); add(cow[i].p, cow[i].p, total); } printf("%lld\n", sum); return 0; }
标签:
原文地址:http://www.cnblogs.com/sunus/p/4480021.html