zoukankan      html  css  js  c++  java
  • [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 imes 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('
    '))
    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 }
  • 相关阅读:
    DB2中的prepare和bind
    MDC 设置CURRENTMDC ROLLOUT MODE
    TSO缩写
    docker资料仓库搭建
    mfs 使用心得
    个人简介
    C# 关于字符串中 符合正则表达式的指定字符的替换的方法
    学而不思则罔,思而不学则殆
    点击按钮下载效果
    菜鸟成长之路SQL Server事物学习,高手跳过
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8321149.html
Copyright © 2011-2022 走看看