#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;
}
Codeforces Round #433 C - Boredom
最新推荐文章于 2022-11-22 23:15:14 发布