当前位置: 首页 > news >正文

AtCoder 265G 线段树

题意

传送门 AtCoder 265G 012 Inversion

题解

直接维护逆序对数量比较困难,考虑到元素值域很小,直接将不同数值对解耦进行维护。具体而言,线段树维护区间 0 , 1 , 2 0,1,2 0,1,2 的数量,以及满足 i < j i<j i<j a [ i ] = x , a [ j ] = 1 a[i]=x,a[j]=1 a[i]=x,a[j]=1 的数对数量 n u m [ x ] [ y ] num[x][y] num[x][y]。总时间复杂度 O ( d 2 n log ⁡ n ) O(d^2n\log n) O(d2nlogn),其中, d d d 是数组取值的规模。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct ST {struct LzNode {vector<int> p;LzNode() : p(3) {reset();}void reset() {iota(p.begin(), p.end(), 0);}void update(vector<int> &f) {vector<int> tmp(3);for (int i = 0; i < 3; ++i) {tmp[i] = f[p[i]];}swap(tmp, p);}};struct Node {vector<int> cnt;vector<vector<ll>> num;Node() : cnt(3), num(3, vector<ll>(3)) {}Node operator+(const Node &o) {Node res;for (int i = 0; i < 3; ++i) {res.cnt[i] = cnt[i] + o.cnt[i];}for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {res.num[i][j] = num[i][j] + o.num[i][j];}}for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {res.num[i][j] += (ll)cnt[i] * o.cnt[j];}}return res;}void update(vector<int> &p) {Node res;for (int i = 0; i < 3; ++i) {res.cnt[p[i]] += cnt[i];}for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {res.num[p[i]][p[j]] += num[i][j];}}swap(*this, res);}};vector<Node> dat;vector<LzNode> lz;ST(vector<int> &a) {int n = a.size();int k = 1;while (k < n) {k *= 2;}k *= 2;dat = vector<Node>(k);lz = vector<LzNode>(k);function<void(int, int, int)> init = [&](int p, int l, int r) {if (r - l == 1) {dat[p].cnt[a[l]] += 1;return;}int m = (l + r) / 2;int chl = p * 2 + 1, chr = p * 2 + 2;init(chl, l, m);init(chr, m, r);dat[p] = dat[chl] + dat[chr];};init(0, 0, n);}void pushdown(int p, int l, int r) {int chl = p * 2 + 1, chr = p * 2 + 2;auto &f = lz[p].p;lz[chl].update(f);lz[chr].update(f);dat[chl].update(f);dat[chr].update(f);lz[p].reset();}void change(int a, int b, vector<int> &f, int p, int l, int r) {if (r <= a || b <= l) {return;}if (a <= l && r <= b) {lz[p].update(f);dat[p].update(f);return;}int m = (l + r) / 2;int chl = p * 2 + 1, chr = p * 2 + 2;pushdown(p, l, r);change(a, b, f, chl, l, m);change(a, b, f, chr, m, r);dat[p] = dat[chl] + dat[chr];}Node query(int a, int b, int p, int l, int r) {if (r <= a || b <= l) {return Node();}if (a <= l && r <= b) {return dat[p];}int m = (l + r) / 2;int chl = p * 2 + 1, chr = p * 2 + 2;pushdown(p, l, r);return query(a, b, chl, l, m) + query(a, b, chr, m, r);}
};
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, q;cin >> n >> q;vector<int> a(n);for (int i = 0; i < n; ++i) {cin >> a[i];}ST tr(a);while (q--) {int op;cin >> op;if (op == 1) {int l, r;cin >> l >> r;l -= 1;auto nd = tr.query(l, r, 0, 0, n);ll res = 0;for (int i = 0; i < 3; ++i) {for (int j = 0; j < i; ++j) {res += nd.num[i][j];}}cout << res << '\n';} else {int l, r;vector<int> b(3);cin >> l >> r;l -= 1;for (int i = 0; i < 3; ++i) {cin >> b[i];}tr.change(l, r, b, 0, 0, n);}}return 0;
}
http://www.lryc.cn/news/173413.html

相关文章:

  • 通俗易懂了解大语言模型LLM发展历程
  • Vim - 快速插入C语言函数注释模板
  • Leetcode171. Excel 表列序号
  • 自主设计,模拟实现 RabbitMQ - 实现 拒绝/否定 应答机制
  • 在github上设置不同分支,方便回滚
  • 【Elsevier旗下】JCR2/3区,最快25天录用!计算机与娱乐、教育、游戏、新媒体均可
  • TSINGSEE视频AI智能分析技术:水泥厂安全生产智能监管解决方案
  • Whisper + NemoASR + ChatGPT 实现语言转文字、说话人识别、内容总结等功能
  • 795. 区间子数组个数
  • Request method ‘GET‘ not supported,不支持GET形式访问
  • 数据结构与算法(C语言版)P2---线性表之顺序表
  • AI写文章软件-怎么选择不同的AI写文章软件
  • VSCode远程连接服务器报错:Could not establish connection to
  • openssl 用法整理 —— 筑梦之路
  • Mac安装SPSS 26(含安装包)
  • uniapp存值和取值方法
  • Apache Beam 2.50.0发布,该版本包括改进功能和新功能
  • 华为云云耀云服务器 L 实例评测|配置教程 + 用 Python 简单绘图
  • 栈的简单应用(利用Stack进行四则混合运算)(JAVA)
  • Python---异常
  • 视频编解码器H.264和H265有什么区别?
  • 网络安全进阶学习第十六课——业务逻辑漏洞介绍
  • 华为OD:跳房子I
  • C语言自定义类型详解(1)结构体知识汇总
  • 小程序中如何查看会员的访问记录
  • SpringCloud Alibaba - Sentinel
  • 内存泄漏,内存溢出,抽象类和接口,netstat、ping、ifconfig的区别
  • TensorFlow安装 ,在原本的虚拟环境下配置Tensorflow.
  • 如何使用HTML, CSS和JavaScript开发一个浏览器打字游戏:从零到一的详细步骤与完整代码教程
  • 安卓玩机搞机----不用刷第三方官改固件即可享受“高级设置”的操作 ChiMi安装使用步骤