zoukankan      html  css  js  c++  java
  • 树状数组,逆序对——CodeForces

    题目含义

    给出一堆数和一个数T

    找出满足区间和严格小于T的区间个数

    题目分析

    又是求区间和,那么就考虑前缀和[a,b]=sum[b]-sum[a-1]<T

    如果求出前缀和用两个循环一个个试,那肯定会超时

    而式子可以化作 sum[j]-T<sum[i],j>i,这就很像一个求逆序对的问题了

    题目代码

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn=2e5+7;
    LL a[maxn],sum[maxn],temp[maxn],c[maxn],t;
    int n;
    int lowbit(int x){
        return x&(-x);
    }
    void add(int x){
        while(x<=n+1){
            c[x]++;
            x+=lowbit(x);
        }
    }
    int ask(int x){
        int ans=0;
        while(x){
            ans+=c[x];
            x-=lowbit(x);
        }
        return ans;
    }
    int main(){
        scanf("%d%lld",&n,&t);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum[i]=sum[i-1]+a[i];
            temp[i]=sum[i];
        }
        sort(temp,temp+1+n);
        LL ans=0;
        for(int i=1;i<=n;i++){
            int pos=lower_bound(temp,temp+1+n,sum[i-1])-temp+1;
            add(pos);
            pos=upper_bound(temp,temp+1+n,sum[i]-t)-temp;
            ans+=i-ask(pos);
        }
        printf("%lld
    ",ans);
        return 0;
    }

    又因为数组开小了,卡了一会儿。。。

  • 相关阅读:
    PHP之旅3 php数组以及遍历数组 以及each() list() foreach()
    NSSetUncaughtExceptionHandler
    runtime
    Objective-C中的instancetype和id区别
    tableView 局部刷新
    CGAffineTransform
    iOS中文本属性Attributes
    ios 相机 自定义 相片的截取
    php程序的生命周期
    PHP代码执行流程
  • 原文地址:https://www.cnblogs.com/helman/p/11222747.html
Copyright © 2011-2022 走看看