Friends

首先确定第 m 大的是谁, 建出字典树之后二分去check, 找到第 m 大之后, 在跑一次字典树去统计总和。

为什么这个要取模啊, 卡了我半天。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 5e4 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}const int LOG = 30;int n, a[N];
LL m;
LL sum, num;struct Trie {int ch[N * LOG][2], cnt[N * LOG];int f[N * LOG][30];int tot;void init() {tot = 1;}void ins(int x) {int u = 1;for(int i = LOG - 1; i >= 0; i--) {cnt[u]++;for(int j = LOG - 1; j >= 0; j--) f[u][j] += (x >> j & 1);if(!ch[u][x >> i & 1]) ch[u][x >> i & 1] = ++tot;u = ch[u][x >> i & 1];}for(int j = LOG - 1; j >= 0; j--) f[u][j] += (x >> j & 1);cnt[u]++;}PLL query(int x, int k, int op) {int u = 1;int now = 0;LL ans = 0;LL sum = 0;for(int i = LOG - 1; i >= 0 && u; i--) {if(x >> i & 1) {if(now + (1 << i) >= k) {ans += cnt[ch[u][0]];if(op) {for(int j = LOG - 1; j >= 0; j--) {if(x >> j & 1) sum += 1LL * (1 << j) * (cnt[ch[u][0]] - f[ch[u][0]][j]);else sum += 1LL * (1 << j) * f[ch[u][0]][j];}}u = ch[u][1];} else {u = ch[u][0];now += 1 << i;}} else {if(now + (1 << i) >= k) {ans += cnt[ch[u][1]];if(op) {for(int j = LOG - 1; j >= 0; j--) {if(x >> j & 1) sum += 1LL * (1 << j) * (cnt[ch[u][1]] - f[ch[u][1]][j]);else sum += 1LL * (1 << j) * f[ch[u][1]][j];}}u = ch[u][0];} else {u = ch[u][1];now += 1 << i;}}}if(now == k) ans += cnt[u], sum += 1LL * cnt[u] * now;return mk(ans, sum);}
} trie;bool check(int x, int op) {sum = 0; num = 0;for(int i = 1; i <= n; i++) {PLL tmp = trie.query(a[i], x, op);num += tmp.fi; sum += tmp.se;}num /= 2; sum /= 2;return num >= m;
}int main() {trie.init();scanf("%d%lld", &n, &m);for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i <= n; i++) trie.ins(a[i]);int low = 0, high = (1 << 30) - 1, up = 0;while(low <= high) {int mid = low + high >> 1;if(check(mid, 0)) up = mid, low = mid + 1;else high = mid - 1;}check(up, 1);sum -= 1LL * (num - m) * up;printf("%lld", sum % mod);return 0;
}/*
*/

转载于:https://www.cnblogs.com/CJLHY/p/11051151.html

Codeforces 241B Friends 字典树相关推荐

  1. CodeForces - 888G Xor-MST(贪心+字典树+最小生成树)

    题目链接:点击查看 题目大意:给出 nnn 个点,任意两个点之间的边权为 ai⊕aja_i\oplus a_jai​⊕aj​,求最小生成树 题目分析:去年多校写过一样的模型,再拿出来写一遍回顾一下:牛 ...

  2. CodeForces - 1476E Pattern Matching(字典树+拓扑)

    题目链接:点击查看 题目大意:给出 nnn 个模式串和 mmm 个匹配串,题目要求输出一种模式串的排列方式,使得 mmm 个模式串从头开始匹配的话,可以匹配到相应的模式串 模式串的长度不超过 444, ...

  3. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  4. 【CodeForces】445B A Lot of Games 字典树博弈

    传送门:[CodeForces]445B  A Lot of Games 题目大意:两人一起构造一个串,每人每次向串的末尾放一个字母,必须保证放了这个字母后能够成所给的N个串的前缀,如果某个人不能放时 ...

  5. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n,表示字符串集合.给定k,表示进行了k次游戏,然后是n个字符串.每局开始,字符串为空串,然后两人轮流在末尾追加 ...

  6. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  7. Codeforces 861D - Polycarp's phone book 字典树/hash

    输入7e4个字符串,要求每个串提取一个子串来唯一表示 4s题可以hash暴力水过,大体思路就是把所有子串map自己的母串,过程中如果这个子串已有hash值就标-1 然后枚举map元素,维护最小化一下就 ...

  8. CodeForces - 456D A Lot of Games(字典树+博弈)

    题目链接:点击查看 题目大意:给出n个字符串,现在有两个人玩一个游戏,游戏规则是两人轮流构造同一个字符串,每次可以向末尾添加一个字母,必须保证添加字母后的字符串是n个字符串其中之一的前缀,不能操作者算 ...

  9. CodeForces - 706D Vasiliy's Multiset(字典树删除操作)

    题目链接:点击查看 题目大意:给出一个正整数n,初始时有一个multiset,接下来有n次操作,实现所有操作,对于每个操作,会有三种形式: + x:向集合中添加一个 x - x:从集合中删掉一个 x ...

最新文章

  1. 深入解析String#intern
  2. Perl中state()和localtime()函数
  3. 电大2007计算机机考专科试题,中央电大2007-2008学年度第一学期期末考试计算机网络专业计算机网络试题2008年1月...
  4. Jython调用不包含第三方库的python脚本
  5. PL/SQL 语言 一
  6. DevExperience(1710)
  7. 关于maven pom
  8. Ajax.Responders
  9. 源码部署Apache和shell脚本安装
  10. 双目测距(一)--图像获取与单目标定
  11. java 设置内存参数_Java虚拟机内存参数设置
  12. python把excel填充到网页_Python获取某网页数据并写入excel
  13. 游戏IP手册:游戏IP的内涵元素
  14. USB HUB控制晶片介绍
  15. 让我摘下星星送给你_摘下星星送给你摘下月亮送给你是哪首歌的歌词
  16. 史上最全软件测试工程师常见的面试题总结(九)【多测师】
  17. 报错:Misplaced alignment tab character 的解决办法以及参考文献的书写方式、There were undefined citations.解决办法
  18. vscode 主题 One Dark Pro
  19. sin和soi区别_FinFET和FD SOI的比较?
  20. dsp控制buck电路电流环推倒与pi调节器设计

热门文章

  1. import python settings from_python settings 中通过字符串导入模块
  2. C++ Licence认证用于项目开发和设备认证
  3. 深入理解java虚拟机章节_深入理解java虚拟机-第六章
  4. mysql system账户密码忘记了_MySQL数据库root账户密码忘记两种处理方法(保有效)...
  5. cas4.0 mysql_【SSO单点系列】:CAS4.0 CAS整合SpringMVC+MyBatis实现数据库校验(04)
  6. 指针写字符比较c语言,利用指针进行字符串大小比较出现的问题
  7. 电脑合上盖子不锁屏_笔记本电脑合上盖子或台式机离开后黑屏是“休眠”还是“睡眠”...
  8. nodejs mysql 同步_NodeJS实现同步的方法
  9. 中的枚举属性函数_对于 JavaScript 中循环之间的技术差异分析
  10. 在Python中查找字符串长度