设为首页 加入收藏

TOP

洛谷P4396 [AHOI2013]作业(树套树)
2019-02-08 16:07:55 】 浏览:33
Tags:洛谷 P4396 AHOI2013 作业

题意

题目链接

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;
}

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇bzoj1565 植物大战僵尸 下一篇bzoj3144 切糕

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }