Codeforces Round #433 C - Boredom

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<utility>
#include<bitset>
#include<complex>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define FOR(i,a,b) for(int i=a; i<b; ++i)
#define MP make_pair
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 2e5;
const int maxnode = maxn * 32;

int n, m;
int tot = 0, rot[maxn + 5];
int tree[maxnode + 5], L[maxnode + 5], R[maxnode + 5];

int Build(int l, int r) {
    int rt = tot++;
    tree[rt] = L[rt] = R[rt] = 0;
    if(l < r) {
        int mid = (l + r) >> 1;
        L[rt] = Build(l, mid);
        R[rt] = Build(mid + 1, r);
    }
    return rt;
}

int Update(int x, int l, int r, int pos) {
    int rt = tot++;
    tree[rt] = tree[x] + 1, L[rt] = L[x], R[rt] = R[x];
    if(l < r) {
        int mid = (l + r) >> 1;
        if(pos <= mid) L[rt] = Update(L[x], l, mid, pos);
        else R[rt] = Update(R[x], mid + 1, r, pos);
    }
    return rt;
}

int Query(int rt1, int rt2, int l, int r, int ll, int rr) {
    if(ll <= l && r <= rr)
        return tree[rt2] - tree[rt1];
    int mid = (l + r) >> 1, res = 0;
    if(ll <= mid)
        res += Query(L[rt1], L[rt2], l, mid, ll, rr);
    if(mid + 1 <= rr)
        res += Query(R[rt1], R[rt2], mid + 1, r, ll, rr);
    return res;
}

inline LL S2(LL tn) {
    return tn * (tn - 1) / 2;
}

int main() {
    int a, l, r, d, u;
    scanf("%d%d", &n, &m);
    rot[0] = Build(1, n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a);
        rot[i] = Update(rot[i - 1], 1, n, a);
    }
    LL sum = S2(n), ans, tmp;
    for(int i = 1; i <= m; ++i) {
        scanf("%d%d%d%d", &l, &d, &r, &u);
        ans = sum - S2(l - 1) - S2(n - r);
        if(d - 1 >= 1) {
            tmp = Query(rot[0], rot[n], 1, n, 1, d - 1);
            ans -= S2(tmp);
            tmp = Query(rot[0], rot[l - 1], 1, n, 1, d - 1);
            ans += S2(tmp);
            tmp = Query(rot[r], rot[n], 1, n, 1, d - 1);
            ans += S2(tmp);
        }
        if(u + 1 <= n) {
            tmp = Query(rot[0], rot[n], 1, n, u + 1, n);
            ans -= S2(tmp);
            tmp = Query(rot[0], rot[l - 1], 1, n, u + 1, n);
            ans += S2(tmp);
            tmp = Query(rot[r], rot[n], 1, n, u + 1, n);
            ans += S2(tmp);
        }
        printf("%lld\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值