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

(应该写的比较清晰)D2. Max Sum OR (Hard Version)

自己写的时候码力不足,比较痛苦,但是没找到写的简单清晰的代码可以参考
于是憋了一下午写了一份,供他人参考

#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
#include<map>
#include<array>
#include<algorithm>
#include<stack>
#include<bitset>
#include<set>
using namespace std;
#define ffp(x,y,z) for(int (x)=(y);(x)<=(z);(x++))
#define ll long long int
#define q_ read()
#define pii pair<int,int>
const ll MOD = 998244353;
const ll lINF = 0x3f3f3f3f3f3f3f3f;
const int iINF = 0x3f3f3f3f;
inline ll read()
{ll x = 0, f = 1;char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();return x * f;
}void solve()
{ll l = q_;ll r = q_;//递归求解//从大到小来填数字ll base = l-1;vector<ll>ans(r - base +1, 0);ffp(i, 1, r - base){ans[i] = i + base;}auto find = [&](auto&& find, ll L, ll R, ll bit)->void{if (R <= L) { return; }//查看bit是否在L和R之内 数字为连续,即判断,当前位在L到R是否符合一半一半的特征/* ll va = 1ll <<bit;while(check(L,R,bit) != 1){bit--;}va += 1ll << bit;*/ll va = 0;for (ll i = 32; i >= 0; i--){ll b = 1ll << i;if (((b & L) == 0) && ((b & R) != 0)){va |= b;break;}if (((b & L) != 0) && ((b & R) != 0)){va |= b;}}//查看左边和右边的数量ll cnt = min(R - va + 1,va - L);ffp(i, 1, cnt){swap(ans[va + i - 1 - base], ans[va - i - base]);}find(find, L, va - cnt - 1, bit - 1);find(find, va + cnt, R, bit - 1);};find(find, l, r, 33);ll res = 0;ffp(i, 1, r - l + 1){res += (ans[i] | (i + base));} cout << res << endl;ffp(i, 1, r - l + 1){cout << ans[i] << ' ';}cout << endl;return;
}int main()
{int t = 1;t = q_;while (t--){solve();}return 0;
}
http://www.zskr.cn/news/9793.html

相关文章:

  • Linux运维
  • day001
  • # Xilnx FPGA 资源结构
  • 借助S参数测量评估电容器阻抗第 2 部分
  • 实战:Android 自定义菊花加载框(带超时自动消失) - 教程
  • 超级恶心的题面 [USACO21OPEN] Portals G
  • 昆仑通态触摸屏保存参数到内部存储器并读取的方法成都控制器开发提供
  • 使用reCAPTCHA提升WordPress网站安全性 - 指南
  • LaTeX入门:10分钟掌握核心用法 - 详解
  • Codeforces 2127 D(图论,组合数学,DFS,分类讨论)
  • 每日报告-关于本学期的计划
  • 若依前后端分离版本二次开发(一 搭建开发环境,新建模块)
  • 每日博客
  • STM32HAL 飞快入门(十九):UART 编程(二)—— 中断方式实现收发及局限分析
  • 详细介绍:uniapp | u-waterfall实现瀑布流商品列表(支持筛选查询)
  • 负载分析和排查六
  • 6月6日证书 - 工信部人才交流中心PostgreSQL中级PGCP高级PGCM认证
  • 【下一款产品】
  • # MySQL索引结构发展历史:从B树到B+树的演进之路
  • 通过ML.Net调用Yolov5的Onnx模型
  • 元宇宙与零售业变革:沉浸式体验重构消费全链路 - 指南
  • c# 反射动态添加Attribute
  • MyBatis-Plus 全方位深度指南:从入门到精通
  • Stm32学习过程笔记
  • 【9.24 直播】集群数据管理实战:时序数据库 IoTDB 数据分区、同步与备份详解
  • 01_进程与线程
  • 第六届医学人工智能国际学术会议(ISAIMS 2025)
  • redis 6.0 多线程
  • docker 常用命令与端口映射
  • linux重启mysql服务,几种常见的方法