四川第十六届ACM - A. Reverse Pairs Coloring

四川第十六届ACM - A. Reverse Pairs Coloring
链接

j l s    天下第一,听鸽鸽的讲解    a n d    看其    c o d e    受到启发 {\color{Red}jls\;天下第一,听鸽鸽的讲解\; and\; 看其 \;code\; 受到启发} jls天下第一,听鸽鸽的讲解and看其code受到启发

分析

  • 任何一个连通块的左上角是唯一的。我们其实求的就是这个左上角的个数。
  • 存在一个颜色可以作为左上角,那么 p o s [ c o l o r ] ≥ p o s [ c o l o r − 1 ] + 1 pos[color] \ge pos[color-1] + 1 pos[color]pos[color1]+1 。因为 p o s [ c o l o r ] < p o s [ c o l o r − 1 ] pos[color] < pos[color- 1] pos[color]<pos[color1] 的话一定会和 c o l o r − 1 color-1 color1 连在一起。注意是当遍历到区间 [ p o s [ c o l o r − 1 ] + 1 , p o s [ c o l o r ] − 1 ] [pos[color - 1] + 1,pos[color] -1] [pos[color1]+1,pos[color]1] ,此时 p o s [ c o l o r ] pos[color] pos[color] 才有贡献。
  • 对于不满足分析 2 2 2 的都无贡献。

思路

  • f f f 表示有贡献的颜色插入的位置,形如: f [ i ] . p u s h f[i].push f[i].push_ b a c k ( j ) back(j) back(j) i i i 表示行, j j j 表示该颜色的下标。意为在第 i i i 行插入颜色 c o l o r [ j ] color[j] color[j]
  • 当遍历超出某个颜色的贡献区间,应当删去。
  • 对于没有贡献的颜色,可以考虑不插入,也可以像 j l s jls jls 一样。我的 c o d e code code 是考虑不插入,那也就不删除。对有贡献的颜色标记一下即可。 j l s jls jls 的操作,不满足分析 2 2 2 ,一定小于 p o s [ c o l o r − 1 ] pos[color-1] pos[color1] ,所以对于不满足的,可以直接加在该不满足颜色所在的那一行,即取 m i n min min 操作,又因为删除是即时的,所以不会影响答案。

在这里插入图片描述

在这里插入图片描述

  • 查询即查询区间 [ [ [上一行颜色 + 1 +1 +1,当前行颜色 ] ] ] (右边界其实应该是,当前行颜色 − 1 -1 1 但其实没影响),考虑当前行颜色大于上一行颜色的情况,那么小于上一行颜色的染色区域一定和上一行染色区域属于一个连通块,没有贡献。当前行颜色小于上一行颜色的情况也没有贡献。

m y    c o d e my\; code mycode

//苍白的话语刺痛着耳朵

#include <bits/stdc++.h>

using i64 = long long;

template <class T>
struct Fenwick {
    const int N;
    std::vector<T> f;
    Fenwick(int _) : N(_), f(_ + 10) {}

    void add(int x, int v) {
        for (int i = x; i <= N; i += (i bitand -i)) {
            f[i] += v;
        }
    }

    i64 query(int x) {
        if (x > N or x <= 0) {
            return 0;
        }

        i64 res = 0;
        for (int i = x; i; i -= (i bitand -i)) {
            res += f[i];
        }
        return res;
    }

    i64 get_sum(int l, int r) {
        if (r < l) {
            return 0LL;
        }

        return query(r) - query(l - 1);
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    Fenwick<int> Fen(n);

    std::vector<int> a(n + 1), pos(n + 1);
    for (int i = 1; i <= n; ++i) {
        std::cin >> a[i];
        pos[a[i]] = i;
    }

    std::vector<int> f[n + 1];
    std::vector<bool> vis(n + 1);
    for (int i = 1; i <= n; ++i) {
        int t = pos[a[i] - 1] + 1;
        if (i >= t) {
            f[t].push_back(i);
            vis[a[i]] = true;
        }
    }

    i64 ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (auto j : f[i]) {
            Fen.add(a[j], 1);
        }
        if (vis[a[i]]) {
            Fen.add(a[i], -1);
        }
        ans += Fen.get_sum(i == 1 ? 1 : a[i - 1] + 1, a[i]);
    }
    std::cout << ans;

    return 0;
}

//

j l s ′ s    c o d e jls's\;code jlsscode

#include <bits/stdc++.h>

using i64 = long long;
template <typename T>
struct Fenwick {
    int n;
    std::vector<T> a;
    
    Fenwick(int n_ = 0) {
        init(n_);
    }
    
    void init(int n_) {
        n = n_;
        a.assign(n, T{});
    }
    
    void add(int x, const T &v) {
        for (int i = x + 1; i <= n; i += i & -i) {
            a[i - 1] = a[i - 1] + v;
        }
    }
    
    T sum(int x) {
        T ans{};
        for (int i = x; i > 0; i -= i & -i) {
            ans = ans + a[i - 1];
        }
        return ans;
    }
    
    T rangeSum(int l, int r) {
        return sum(r) - sum(l);
    }
    
    int select(const T &k) {
        int x = 0;
        T cur{};
        for (int i = 1 << std::__lg(n); i; i /= 2) {
            if (x + i <= n && cur + a[x + i - 1] <= k) {
                x += i;
                cur = cur + a[x - 1];
            }
        }
        return x;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<int> a(n), inva(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i]--;
        inva[a[i]] = i;
    }
    
    std::vector<std::vector<int>> f(n);
    for (int i = 0; i < n; i++) {
        int t = std::min(i, a[i] == 0 ? 0 : inva[a[i] - 1] + 1);
        f[t].push_back(i);
    }
    
    i64 ans = 0;
    
    Fenwick<int> fen(n);
    for (int i = 0; i < n; i++) {
        for (auto j : f[i]) {
            fen.add(a[j], 1);
        }
        fen.add(a[i], -1);
        ans += std::max(0, fen.rangeSum(i == 0 ? 0 : a[i - 1] + 1, a[i]));
    }
    std::cout << ans << "\n";
    
    return 0;
}
  • 19
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Heredy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值