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

压制二元组的总价值

压制二元组的总价值

对于每一个 a i a_i ai, 看它能压制它前面的多少个元素, 那么它对总价值的贡献就是:

在a数组中:

  1. a i a_i ai压制了x个数, 贡献为: x ∗ i x*i xi
  2. a i a_i ai所压制的所有数在 a a a中的下标和为 y y y, 贡献为 − y -y y

树状数组来求:

  1. 为了快速求出 a i a_i ai压制了几个数, 记录 a i a_i ai前面的所有数在 b b b中的下标 m a p ( a [ i ] ) map(a[i]) map(a[i]),值 t r b [ m a p ( a [ i ] ) ] = 1 trb[map(a[i])]=1 trb[map(a[i])]=1表示它出现过,

​ 这样每次只需通过 s u m ( m a p [ a [ i ] ] ) sum(map[a[i]]) sum(map[a[i]])即可得出它前面已经出现了多少个数.

  1. 记录 a i a_i ai前面的所有数在b中的下标 m a p ( a [ i ] ) map(a[i]) map(a[i]), 值 t r a [ m a p ( a [ i ] ) ] tra[map(a[i])] tra[map(a[i])]为它在 a a a中的下标 i i i, 每次 s u m sum sum即可得出它所压制的所有数的下标和.
import java.io.*;
import java.util.*;public class Main {static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));static StreamTokenizer stmInput = new StreamTokenizer(br);static int N = 200010;static long tra[] = new long[N], trb[] = new long[N];static int a[] = new int[N], b[] = new int[N];static HashMap<Integer, Integer> map = new HashMap<>();static int n;public static int readInt() throws IOException {stmInput.nextToken();return (int) stmInput.nval;}public static int lowbit(int x){return x & -x;}public static void add(int x, int c, long tr[]){for (int i = x; i <= n; i += lowbit(i)) {tr[i] += c;}}public static long sum(int x, long tr[]){long res = 0;for (int i = x; i >= 1; i -= lowbit(i)) {res += tr[i];}return res;}public static void solve() throws IOException{n = readInt();for (int i = 1; i <= n; i++) {a[i] = readInt();}for (int i = 1; i <= n; i++) {b[i] = readInt();map.put(b[i], i);}long ans = 0;for (int i = 1; i <= n; i++) {// 1.ai压制了多少个数ans += i * sum(map.get(a[i]), tra);add(map.get(a[i]), 1, tra);// 2.被ai压制的所有数在a中的下标和ans -= sum(map.get(a[i]), trb);add(map.get(a[i]), i, trb);}pw.println(ans);}public static void main(String[] args) throws IOException {int T = 1;while(T-- != 0){solve();}pw.flush();pw.close();br.close();}}
http://www.lryc.cn/news/290307.html

相关文章:

  • 【习题】保存应用数据
  • Flask框架小程序后端分离开发学习笔记《5》简易服务器代码
  • “计算机视觉处理设计开发工程师”专项培训(第二期)
  • R语言学习case7:ggplot基础画图(核密度图)
  • Ubuntu18配置Docker
  • Keil/MDK平台 - 结构体成员指针注意事项
  • 一款超级好用的远程控制APP,你值得拥有
  • NumPy必知必会50例 | 18. 使用 NumPy 解决线性方程组:数学问题的实用解决方案
  • C/C++编码问题研究
  • 二刷代码随想录|Java版|回溯算法3|子集问题
  • mongodb config
  • pytorch 实现中文文本分类
  • 【MySQL】聚合函数和内置函数
  • python第五节:集合set(4)
  • 知识笔记(一百)———什么是okhttp?
  • Electron桌面应用实战:Element UI 导航栏橙色轮廓之谜与Bootstrap样式冲突解决方案
  • Nuget包缓存存放位置迁移
  • 键盘上Ins键的作用
  • css display 左右对齐 技巧
  • 【Linux操作系统】:Linux开发工具编辑器vim
  • Good Trip Codeforces Round 921 (Div. 2) 1925D
  • 推荐一款Linux、数据库、Redis、MongoDB统一管理平台!
  • TensorFlow2实战-系列教程6:迁移学习实战
  • 怎样开发adobe indesign插件,具体流程?
  • Docker 安装与基本操作
  • 译文带你理解Python的dataclass装饰器
  • 【C语言】实现程序的暂停
  • Hana SQL+正则表达式
  • 【笔记】顺利通过EMC试验(16-41)-视频笔记
  • Qlik Sense 调用NPrinting生成On-Demand报表