2 条题解

  • 0
    @ 2025-10-8 17:02:03

    by hansang:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=1e5+10, M=1e6+10;
    LL a[N], c[M]; int n, l, C; 
    struct node{LL x, d;} b[N];
    void add(LL x, LL w){
        if(x==0) return ;
        for(LL i=x; i>=1; i-=i&-i) c[i]+=w;
    }
    LL query(LL x){
        LL res=0;
        for(LL i=x; i<=a[n]; i+=i&-i) res+=c[i];
        return res;
    }
    int main(){
        scanf("%d%d%d", &n, &l, &C);
        for(int i=1; i<=n; i++) scanf("%lld", &a[i]); 
        sort(a+1, a+n+1); LL ans=0;
        for(int i=1; i<=n; i++){
            b[i].x=l*a[i]/a[n];
            b[i].d=l*a[i]%a[n]+1;
            ans+=(i-(n-i+1))*b[i].x;
        }
        memset(c, 0, sizeof(c));
        for(int i=1; i<=n; i++){
            ans-=query(b[i].d+1);
            add(b[i].d, 1);
        }
        printf("%lld\n", ans);
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:01:53

      by hansang:

      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const int N=1e5+10, M=1e6+10;
      LL a[N], c[M]; int n, l, C; 
      struct node{LL x, d;} b[N];
      void add(LL x, LL w){
          if(x==0) return ;
          for(LL i=x; i>=1; i-=i&-i) c[i]+=w;
      }
      LL query(LL x){
          LL res=0;
          for(LL i=x; i<=a[n]; i+=i&-i) res+=c[i];
          return res;
      }
      int main(){
          scanf("%d%d%d", &n, &l, &C);
          for(int i=1; i<=n; i++) scanf("%lld", &a[i]); 
          sort(a+1, a+n+1); LL ans=0;
          for(int i=1; i<=n; i++){
              b[i].x=l*a[i]/a[n];
              b[i].d=l*a[i]%a[n]+1;
              ans+=(i-(n-i+1))*b[i].x;
          }
          memset(c, 0, sizeof(c));
          for(int i=1; i<=n; i++){
              ans-=query(b[i].d+1);
              add(b[i].d, 1);
          }
          printf("%lld\n", ans);
          return 0;
      } 
      • 1

      USACO(53)树状数组2:奶牛赛跑P3054 [USACO12OPEN] Running Laps S

      信息

      ID
      2650
      时间
      1000ms
      内存
      128MiB
      难度
      9
      标签
      递交数
      9
      已通过
      4
      上传者