hihocoder 1488 排队接水

题目链接

分析:
首先,很好思考,只要将区间内最短优先排列就能得到答案。
但是直接暴力肯定会TLE,我们换一个思考方式。
假设我们已经得出了[L, R]区间的答案,那么[L + 1, R], [L - 1, R], [L, R + 1], [L, R - 1]都很容易计算得出。考虑新加入的数K应该在新的序列中排第几个,那么对原序列 x>K ,所有的数都会往后移动一个单位,新序列的值增加sum[x]*K,再加上K放入的贡献sum[x<=K] + K。只要用树状数组维护一下和就能在 log(N) 的时间内完成一次更新。
典型的莫队,把查询离线,然后计算。复杂度 N1.5log(maxA)

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

#define   offcin        ios::sync_with_stdio(false)
#define   DEBUG         freopen("#.txt", "r", stdin)
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   slch          v<<1
#define   srch          v<<1|1
#define   ll            long long
#define   ull           unsigned long long
#define   lowbit(x)     (x&-x)

const int    INF    = 0x3f3f3f3f;
const ll     INFF   = 1e18;
const double pi     = acos(-1.0);
const double inf    = 1e18;
const double eps    = 1e-9;
const ll     mod    = 1e9+7;
const int    maxmat = 10;
const ull    BASE   = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
inline ll bits(ll x) {
      return !x ? x : bits(x - lowbit(x)) + 1;
}
/*****************************************************/

const int maxn = 2e4 + 5;

long long sum[maxn];
long long num[maxn];
int t[maxn], b[maxn];
int N, M, maxv;
int cur_l, cur_r;
long long res;
long long a[maxn];

struct Node {
    int l, r, id;
    bool operator <(const Node &rhs) const {
        if (b[l] == b[rhs.l])
            return r < rhs.r;
        return b[l] < b[rhs.l];
    }
}node[maxn];

void update(ll *a, int x, long long val) {
    while (x <= N) {
        a[x] += val;
        x += lowbit(x);
    }
}

long long query(ll *a, int x) {
    long long ans = 0;
    while (x > 0) {
        ans += a[x];
        x -= lowbit(x);
    }
    return ans;
}

void edit(int pos, int op) {
    if (op == 0) {
        int nn = query(num, maxv) - query(num, t[pos]);
        long long S = query(sum, t[pos]);
        res += 1ll * t[pos] * nn + t[pos];
        res += S;
        update(num, t[pos], 1);
        update(sum, t[pos], t[pos]);
    }
    else {
        update(num, t[pos], -1);
        update(sum, t[pos], -t[pos]);
        int nn = query(num, maxv) - query(num, t[pos]);
        long long S = query(sum, t[pos]);
        res -= 1ll * t[pos] * nn + t[pos];
        res -= S;
    }
}

void solve(int L, int R, int id) {
    while (cur_r < R) edit(++ cur_r, 0);
    while (cur_r > R) edit(cur_r --, 1);
    while (cur_l > L) edit(-- cur_l, 0);
    while (cur_l < L) edit(cur_l ++, 1);
    a[id] = res;
}

int main(int argc, char const *argv[]) {
    int T; cin>>T;
    while (T --) {
        cin>>N>>M;
        maxv = -1;
        memset(num, 0, sizeof(num));
        memset(sum, 0, sizeof(sum));

        int block = ceil(sqrt((double)N));
        for (int i = 1; i <= N; i ++) {
            scanf("%d", t + i);
            b[i] = (i - 1) / block;
            maxv = max(maxv, t[i]);
        }

        for (int i = 1; i <= M; i ++) {
            scanf("%d%d", &node[i].l, &node[i].r);
            node[i].id = i;
        }

        sort(node + 1, node + M + 1);

        cur_l = 1, cur_r = 0;
        res = 0;

        for (int i = 1; i <= M; i ++)
            solve(node[i].l, node[i].r, node[i].id);

        for (int i = 1; i <= M; i ++)
            printf("%lld\n", a[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值