[USACO 04OPEN]MooFest

Description

约翰的N 头奶牛每年都会参加“哞哞大会”。哞哞大会是奶牛界的盛事。集会上的活动很多,比如堆干草,跨栅栏,摸牛仔的屁股等等。它们参加活动时会聚在一起,第i 头奶牛的坐标为Xi,没有两头奶牛的坐标是相同的。奶牛们的叫声很大,第i 头和第j 头奶牛交流,会发出max{Vi; Vj}×|Xi − Xj | 的音量,其中Vi 和Vj 分别是第i 头和第j 头奶牛的听力。假设每对奶牛之间同时都在说话,请计算所有奶牛产生的音量之和是多少。

Input

• 第一行:单个整数N,1 ≤ N ≤ 20000

• 第二行到第N + 1 行:第i + 1 行有两个整数Vi 和Xi,1 ≤ Vi ≤ 20000; 1 ≤ Xi ≤ 20000

Output

• 单个整数:表示所有奶牛产生的音量之和

Sample Input

4
3 1
2 5
2 6
4 3

Sample Output

57

题解

开四个树状数组。

第一个为以 $V$ 为关键词,牛个数的前缀;

第二个为以 $V$ 为关键词, $X$ 的前缀;

第三个为以 $V$ 为关键词, $V$ 的前缀;

第四个为以 $V$ 为关键词, $X\times V$ 的前缀。

然后扫一遍随便乱搞就好了。

 1 //It is made by Awson on 2018.1.20
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <cstdio>
 9 #include <string>
10 #include <vector>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 #define Min(a, b) ((a) < (b) ? (a) : (b))
19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
20 #define writeln(x) (write(x), putchar('\n'))
21 #define lowbit(x) ((x)&(-(x)))
22 using namespace std;
23 const int N = 20000;
24 void read(int &x) {
25     char ch; bool flag = 0;
26     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
27     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
28     x *= 1-2*flag;
29 }
30 void write(LL x) {
31     if (x > 9) write(x/10);
32     putchar(x%10+48);
33 }
34 int n;
35 struct tt {
36     int v, x;
37     bool operator < (const tt &b) const {return x < b.x; }
38 }a[N+5];
39 struct bittree {
40     LL c[N+5];
41     void add(int x, int val) {for (; x <= N; x += lowbit(x)) c[x] += val; }
42     LL query(int x) {
43     LL ans = 0;
44     for (; x; x -= lowbit(x)) ans += c[x];
45     return ans;
46     }
47 }T1, T2, T3, T4; //T1 tol, T2 tolx, T3 tolt, T4 tolx*t
48 
49 void work() {
50     read(n); for (int i = 1; i <= n; i++) read(a[i].v), read(a[i].x);
51     sort(a+1, a+n+1); LL ans = 0;
52     for (int i = 1; i <= n; i++) {
53     ans += (T1.query(a[i].v)*a[i].x-T2.query(a[i].v))*a[i].v;
54     ans += (T3.query(N)-T3.query(a[i].v))*a[i].x-(T4.query(N)-T4.query(a[i].v));
55     T1.add(a[i].v, 1), T2.add(a[i].v, a[i].x), T3.add(a[i].v, a[i].v), T4.add(a[i].v, a[i].x*a[i].v);
56     }
57     writeln(ans);
58 }
59 int main() {
60     work();
61     return 0;
62 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/8321149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值