几道偏序问题(数据结构)

P3157 [CQOI2011]动态逆序对

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10;int root[N], ls[N << 8], rs[N << 8], sum[N << 8], cnt;int n, m, pos[N];inline int lowbit(int x) {return x & (-x);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++cnt;}sum[rt] += v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}int A[50], B[50], cnt1, cnt2;int query_sum(int l, int r, int L, int R) {if (l >= L && r <= R) {int ans = 0;for (int i = 1; i <= cnt1; i++) {ans -= sum[A[i]];}for (int i = 1; i <= cnt2; i++) {ans += sum[B[i]];}return ans;}int A1[50], B1[50];int mid = l + r >> 1, ans = 0;if (L <= mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = ls[B[i]];}ans += query_sum(l, mid, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}if (R > mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = rs[B[i]];}ans += query_sum(mid + 1, r, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}return ans;
}int get_sum(int l, int r, int L, int R) {if (L > R || l > r) {return 0;}cnt1 = cnt2 = 0;for (int j = l - 1; j; j -= lowbit(j)) {A[++cnt1] = root[j];}for (int j = r; j; j -= lowbit(j)) {B[++cnt2] = root[j];}return query_sum(1, n, L, R);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1, x; i <= n; i++) {scanf("%d", &x);pos[x] = i;for (int j = i; j <= n; j += lowbit(j)) {update(root[j], 1, n, x, 1);}}ll ans = 0;for (int i = 1; i <= n; i++) {int p = pos[i];ans += get_sum(1, p - 1, i + 1, n);ans += get_sum(p + 1, n, 1, i - 1);}ans >>= 1;for (int i = 1, x; i <= m; i++) {scanf("%d", &x);printf("%lld\n", ans);int p = pos[x];for (int j = p; j <= n; j += lowbit(j)) {update(root[j], 1, n, x, -1);}ans -= get_sum(1, p - 1, x + 1, n);ans -= get_sum(p + 1, n, 1, x - 1);}return 0;
}

P3810 三维偏序(陌上花开)

#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;struct Res {int x, y, z;Res(int _x = 0, int _y = 0, int _z = 0) : x(_x), y(_y), z(_z) {}
}a[N << 1];int n, m, ans[N];int root[N << 2], ls[N << 9], rs[N << 9], sum[N << 9], cnt;int A[50], cnt1;bool cmp(Res a, Res b) {return a.x < b.x;
}inline int lowbit(int x) {return x & (-x);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++cnt;}sum[rt] += v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}int query_sum(int l, int r, int L, int R) {if (l >= L && r <= R) {int ans = 0;for (int i = 1; i <= cnt1; i++) {ans += sum[A[i]];}return ans;}int mid = l + r >> 1, ans = 0, A1[50];if (L <= mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = ls[A[i]];}ans += query_sum(l, mid, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}}if (R > mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = rs[A[i]];}ans += query_sum(mid + 1, r, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z);}sort(a + 1, a + 1 + n, cmp);int last = 1;for (int i = 1; i <= n; i++) {for (int j = a[i].y; j <= m; j += lowbit(j)) {update(root[j], 1, m, a[i].z, 1);}if (i == n || a[i].x != a[i + 1].x) {for (int j = last; j <= i; j++) {cnt1 = 0;for (int k = a[j].y; k; k -= lowbit(k)) {A[++cnt1] = root[k];}int res = query_sum(1, m, 1, a[j].z);ans[res - 1]++;}last = i + 1;}}for (int i = 0; i < n; i++) {printf("%d\n", ans[i]);}return 0;
}

P3658 [USACO17FEB]Why Did the Cow Cross the Road III P

#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;struct Res {int x, y, z;Res(int _x = 0, int _y = 0, int _z = 0) : x(_x), y(_y), z(_z) {}bool operator < (const Res &t) const {return x < t.x;}
}a[N];int l[N], r[N], n, k;int root[N], ls[N << 8], rs[N << 8], sum[N << 8], cnt;int A[50], B[50], cnt1, cnt2;inline int lowbit(int x) {return x & (-x);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++cnt;}sum[rt] += v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}if (x > mid) {update(rs[rt], mid + 1, r, x, v);}
}int query_sum(int l, int r, int L, int R) {if (l >= L && r <= R) {int ans = 0;for (int i = 1; i <= cnt1; i++) {ans -= sum[A[i]];}for (int i = 1; i <= cnt2; i++) {ans += sum[B[i]];}return ans;}int mid = l + r >> 1, ans = 0, A1[50], B1[50];if (L <= mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = ls[B[i]];}ans += query_sum(l, mid, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}if (R > mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = rs[B[i]];}ans += query_sum(mid + 1, r, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}return ans;
}int get_sum(int l, int r, int L, int R) {if (l > r || L > R) {return 0;}cnt1 = cnt2 = 0;for (int i = l - 1; i; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = r; i; i -= lowbit(i)) {B[++cnt2] = root[i];}return query_sum(1, n, L, R);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &k);for (int i = 1, x; i <= n; i++) {scanf("%d", &x);l[x] = i;}for (int i = 1, x; i <= n; i++) {scanf("%d", &x);r[x] = i;}for (int i = 1; i <= n; i++) {a[i] = Res(l[i], r[i], i);}sort(a + 1, a + 1 + n);long long ans = 0;for (int i = 1; i <= n; i++) {ans += get_sum(a[i].y + 1, n, k + a[i].z + 1, n);ans += get_sum(a[i].y + 1, n, 1, a[i].z - k - 1);for (int j = a[i].y; j <= n; j += lowbit(j)) {update(root[j], 1, n, a[i].z, 1);}}printf("%lld\n", ans);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自学架构设计?帮你总结了 4 个方法

从编程思维到架构思维的升级&#xff0c;是工作 3、5 年的程序员遇到的第一个槛&#xff0c;特别是当你准备晋升考核时。我有个哥们&#xff0c;技术和业务都很不错&#xff0c;腾讯 T2.3 升 T3.1&#xff0c;就卡在了架构设计这部分。架构这个事儿&#xff0c;不像算法和代码&…

如何在东八区的计算机上获取美国时间

既可以用旧API&#xff08;JDK8之前&#xff09;&#xff0c;也可以使用新API。以下用旧API为例&#xff1a; 在Java语言中&#xff0c;可以通过java.util.Calendar类取得一个本地时间或者指定时区的时间实例&#xff0c;如下&#xff1a; // 取得本地时间&#xff1a; Calen…

Wannafly挑战赛24 无限手套(生成函数)

无限手套 每种宝石的生成函数为∑n≥0xn(ain2bin1)对其进行化简∑n≥0xn∑n≥0binxn∑n≥0ain2xn11−xbix(1−x)2aix(1x)(1−x)3最后答案∏i1m((ai−bi1)x2(aibi−2)x1)(1−x)3m每种宝石的生成函数为\sum_{n \geq 0} x ^ n(a_i n ^ 2 b_i n 1)\\ 对其进行化简\sum_{n \geq 0}…

.NET Core 3.0及ASP.NET Core 3.0 前瞻

前几天微软发布了 .NET Core 3.0 Preview 9 &#xff0c;这是.NET Core 3.0 最后一个预览版。[翻译] .NET Core 3.0 Preview 9 发布.NET Core 3.0 正式发布将在.NET Conf 上发布&#xff0c;.NET Conf 时间是9月23日至25日。Visual Studio 2019 16.3预览版3和Visual Studio for…

有了Unicode为啥还需要UTF-8

有了Unicode为啥还需要UTF-8 要回答这个问题&#xff0c;需要吃透“编码”的概念&#xff0c;刚好看到大神阮一峰写的文章&#xff1a;字符编码笔记&#xff1a;ASCII&#xff0c;Unicode 和 UTF-8 抄录如下&#xff0c;便于查找&#xff1a; 一、ASCII 码 我们知道&#xf…

2019ICPC西安邀请赛 E. Tree(树剖 + 线段树)

Tree 给定一棵树&#xff0c;节点有点权&#xff0c;然后有三种操作&#xff1a; 一、修改1−>s1->s1−>s的路径上的点权与ttt进行按位或。 二、修改1−>s1->s1−>s的路径上的点权与ttt进行按位与。 三、查询1−>s1->s1−>s的路径上的点权异或和…

升职却不加薪,为什么我还觉得老板说的挺有道理

前几天晚上&#xff0c;DevOps交流群里&#xff0c;有人抛出这样一个观点&#xff1a;如果有人来找我加薪&#xff0c;我一定告诉他我要给他升职&#xff0c;因为升职是免费的&#xff0c;加薪可是真的要花钱。但是我也会许诺他&#xff0c;如果他能把那个团队搞好&#xff0c;…

GBK、GB2312、GB18030

GBK、GB2312都是编码方式&#xff0c;都是双字节字符集&#xff0c;GB18030编码是一二四字节变长编码 GB&#xff1a;国标&#xff0c;国家标准 BIG5&#xff1a; 支持繁体中文 GB2312&#xff1a; 支持简体中文&#xff08;GBK的子集&#xff09; 共收录 6763 个汉字 GBK&…

2019 ICPC 南京 F. Paper Grading(字典树dfs序上树套树)

Paper Grading 题意&#xff1a;给定nnn个字符串&#xff0c;有两种操作&#xff1a; 一、给定i,ji, ji,j&#xff0c;交换第iii个跟第jjj个字符串。 二、给定 str &#xff0c;k,l,rk, l, rk,l,r&#xff0c;问你在区间[l,r][l, r][l,r]中的字符&#xff0c;与 str 至少有k…

细节之中自有天地,整洁成就卓越代码

溪源 | 长沙.NET技术社区开篇我们总是很容易就能写出满足某个特定功能的代码&#xff0c;却很难写出优雅代码。又最欣赏那些优雅的代码&#xff0c;因为优雅代码更能体现一个开发者的积累。就像写一篇散文&#xff0c;有的就像初学者不得其门而入&#xff0c;遣词造句都非常困难…

#2693. jzptab

jzptab ∑i1n∑j1mlcm(i,j)∑i1n∑j1mijgcd⁡(i,j)∑d1nd∑i1nd∑j1mdij[gcd⁡(i,j)1]∑d1nd∑k1ndk2μ(k)∑i1nkdi∑j1mkdjTkd,f(n)∑i1ni∑T1nf(nT)f(mT)(T∑k∣Tμ(k)k)设g(n)n∑d∣nμ(d)d先令g(n)g(n)ng(1)1,g(p)μ(1)μ(p)p1−p,g(pk,k≥2)1−p同时是积性函数&#xff0c…

UTF8、UTF16、UTF32区别

UTF8、UTF16、UTF32都是unicode字符集的字符编码。 UTF意思是unicode转换格式&#xff08;Unicode transform format&#xff09;&#xff0c;出现UTF8、UTF16、UTF32是出于要在内存中存储字符的目的而对unicode字符编号进行编码。 UTF8、UTF16、UTF32区别&#xff1a;&#…

URL编解码、Big Endian和Little Endian

一、Endian的起源 在各种计算机体系结构中&#xff0c;对于字节、字等的存储机制有所不同&#xff0c;因而引发了计算机通信领域中一个很重要的问题&#xff0c;即通信双方交流的信息单元&#xff08;比特、字节、字、双字等等&#xff09;应该以什么样的顺序进行传送。如果不达…

一次业务网关用ASP.NET Core 2.1重构的小结

前言对于API网关&#xff0c;业界貌似对它进行下划分&#xff0c;有下面几个分类/场景。面向Web App面向Mobile App面向Partner OpenAPI面向Partner ExternalAPI其他。。。在18年8月份的时候&#xff0c;有幸用.NET Core 2.1重构了一个对外的业务网关项目&#xff0c;这个项目的…

导数卷积 (NTT)

导数卷积 有f(x)∑i0n−1aixi求g(x)∑i0n−1f(i)(x)f(n−i−1)(x)∑i0n−1∑j0n−1aij(ij)!j!∑k0n−1ak(n−i−1)(k(n−i−1))!k!设F(n)ann!我们单独求解g(x)的第m项&#xff0c;有gm∑i0n−1∑j1d(F(ij)∗F(n−i−1d−j))(1j!∗1(d−j)!)设H(n)1n!有f(x) \sum_{i 0} ^{n - …

Java如何解决乱码问题

java在字符串中统一用Unicode表示。 对于任意一个字符串&#xff1a;String string “测试字符串”; 如果源文件是GBK编码&#xff0c;操作系统默认环境编码也为GBK&#xff0c;那么编译的时候&#xff0c;JVM将按照GBK编码将字节数组解析为字符&#xff0c;然后将字符转换为…

推荐几个华为,字节跳动、蚂蚁金服等大佬的公众号

每一个公众号都是一个特色的图书馆&#xff0c;为我们的学习提供优质的服务&#xff0c;珍贵的资源&#xff0c;耐心看完&#xff0c;认真选择适合自己的良师益友吧。Python爱好者社区Python爱好者社区&#xff0c;这里有分类整理好的历史优秀文章数千篇供你学习&#xff0c;内…

HDU 6061 RXD and functions(NTT)

RXD and functions 首先是有一个结论&#xff0c;对多项式做任意多次 transformation &#xff0c;其结果跟做一次 transformation Tr(f,∑i1mai)Tr(f, \sum\limits_{i 1} ^{m} a_i)Tr(f,i1∑m​ai​)的结果是一样的&#xff0c;所以我们约定a−∑i1maia -\sum\limits_{i 1…

Java语法糖

Java中语法糖原理、解语法糖 语法糖&#xff1a;switch 支持 String 与枚举、泛型、自动装箱与拆箱、方法变长参数、枚举、内部类、条件编译、 断言、数值字面量、for-each、try-with-resource、Lambda表达式、 先Mark&#xff0c;需要后续补齐、 参考&#xff1a; https://w…

使用Ingress来负载分发微服务

目录 使用Ingress来负载分发微服务 Demo规划 准备Demo并完成部署 创建部署&#xff08;Deployment&#xff09;资源 创建服务&#xff08;Service&#xff09;资源 创建Ingress资源并配置转发规则 使用Ingress来负载分发微服务NodePort Service存在太多缺陷&#xff0c;不适合…