[BZOJ4627][BeiJing2016]回转寿司 cdq分治

怎么短怎么写对吧,cdq分治代替数据结构
把区间连续的看成前缀和相减,排序单调队列维护即可
注意sum[0]

/**************************************************************
    Problem: 4627
    User: di4CoveRy
    Language: C++
    Result: Accepted
    Time:716 ms
    Memory:2464 kb
****************************************************************/

#include 
#include 
#include 
#define N 100050
using namespace std;
typedef long long LL;
LL sum[N],ans;
int a[N],n,L,R;

void solve(int l,int r) {
    if (l > r) return ;
    int mid = (l + r) >> 1;
    if (l == r) return ;
    solve(l,mid); solve(mid+1,r);
    int h = l , t = l-1;
    for (int _=mid+1;_<=r;_++) {
        while (t+1 <= mid && sum[_] - sum[t+1] >= L) t++;
        while (h+1 <= mid+1 && sum[_] - sum[h] > R) h++;
        ans += max(t-h+1 , 0);
    }
    sort(sum+l,sum+r+1);
}

int main() {
    scanf("%d%d%d",&n,&L,&R);
    for (int _=1;_<=n;_++) scanf("%d",&a[_]) , sum[_] = sum[_-1] + a[_];
    solve(0,n);
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(CDQ分治)