欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

洛谷P4396 [AHOI2013]作业(树套树)

程序员文章站 2022-11-22 08:30:25
题意 "题目链接" Sol 为什么一堆分块呀。。三维数点不应该是套路离线/可持久化+树套树么。。 亲测树状数组套权值线段树可过 复杂度$O(nlog^2n)$,空间$O(nlogn)$(离线) cpp include define Pair pair define MP(x, y) make_pai ......

题意

题目链接

sol

为什么一堆分块呀。。三维数点不应该是套路离线/可持久化+树套树么。。

亲测树状数组套权值线段树可过

复杂度\(o(nlog^2n)\),空间\(o(nlogn)\)(离线)

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 4e5 + 10, ss = 1e7 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, a[maxn], pre[maxn], las[maxn], lim = 1e5, tot;
pair ans[maxn];
#define lb(x) (x & (-x))
struct bit {
    int t[maxn];
    void add(int x, int v) {
        x++;
        while(x <= lim) t[x] += v, x += lb(x); 
    }
    int sum(int x) {
        x++;
        int ans = 0;
        while(x) ans += t[x], x -= lb(x);
        return ans;
    }
    int query(int x, int y) {return sum(y) - sum(x - 1);}
}q1;
struct query {
    int k, a, b, id, p;
    bool operator < (const query &rhs) const {
        return k < rhs.k;   
    }
}q[maxn];
int root[ss], sum[ss], ls[ss], rs[ss], cnt;
void update(int k) {
    sum[k] = sum[ls[k]] + sum[rs[k]];
}
void insert(int &k, int l, int r, int p, int v) {
    if(!k) k = ++cnt;
    if(l == r) {sum[k]++; return ;}
    int mid = l + r >> 1;
    if(p <= mid) insert(ls[k], l, mid, p, v);
    else insert(rs[k], mid + 1, r, p, v);
    update(k);
}
int query(int k, int l, int r, int ql, int qr) {
    if(!k) return 0;
    if(ql <= l && r <= qr) return sum[k];
    int mid = l + r >> 1;   
    if(ql > mid) return query(rs[k], mid + 1, r, ql, qr);
    else if(qr <= mid) return query(ls[k], l, mid, ql, qr);
    else return query(ls[k], l, mid, ql, qr) + query(rs[k], mid + 1, r, ql, qr);
}
void add(int x, int v) {
    x++;
    while(x <= lim) insert(root[x], 0, lim, v, 1), x += lb(x); 
}
int query(int x, int a, int b) {
    x++;
    int ans = 0;
    while(x) ans += query(root[x], 0, lim, a, b), x -= lb(x);
    return ans;
}
void solve() {
    int x = 0;
    for(int i = 1; i <= tot; i++) {
        while(x < q[i].k) 
            q1.add(a[++x], 1), add(pre[x], a[x]);
        ans[abs(q[i].id)].fi += (q[i].id / (abs(q[i].id))) * q1.query(q[i].a, q[i].b);
        ans[abs(q[i].id)].se += (q[i].id / (abs(q[i].id))) * query(q[i].p, q[i].a, q[i].b);
    }
}
signed main() {
    n = read(); m = read();
    for(int i = 1; i <= n; i++) {
        a[i] = read();
        pre[i] = las[a[i]]; las[a[i]] = i;
    }
    for(int i = 1; i <= m; i++) {
        int l = read(), r = read(), a = read(), b = read();
        q[++tot].k = l - 1; q[tot].a = a; q[tot].b = b; q[tot].id = -i; q[tot].p = l - 1;
        q[++tot].k = r;     q[tot].a = a; q[tot].b = b; q[tot].id = i;  q[tot].p = l - 1;
    }
    sort(q + 1, q + tot + 1); 
    solve();
    for(int i = 1; i <= m; i++) printf("%d %d\n", ans[i].fi, ans[i].se);
    return 0;
}