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

P4155 学习笔记

省流:不会做可以去看标签,做法全在标签里,一个不多一个不少

题目传送门

很容易想到开个结构体存每个士兵的奔袭区间和初始编号,顺便把排序函数写里面

struct node {int idx, l, r;bool operator < (const node &b) const { // const 不 const 的不重要,要加就加两个,不加就都别加return l < b.l;}
} a[maxn << 1];

这题是个环,可以复制一遍在原数组后面方便操作。

就是

n2 = n;
for (int i = 1; i <= n; i++){++n2;a[n2] = a[i]; // 这个是整体赋值,实在不行你可以 a[n2].idx = a[i].idxa[n2].l = a[i].l + m;a[n2].r = a[i].r + m;
}

这个 n2 看不懂的话可以这么写:

for (int i = 1; i <= n; i++){a[i + n] = a[i];a[i + n].l = a[i].l + m;a[i + n].r = a[i].r + m;
}

然后我们开始倍增,类似 DP 一样

\[go[i][j] = go[go[i][j - 1]][j - 1] \]

于是:

void init() {int nxt = 1;for (int i = 1; i <= n * 2; i++) {while (nxt <= n * 2 && a[nxt].l <= a[i].r)nxt++;go[i][0] = nxt - 1;} // 类似 DP 的初始化for (int i = 1; (1 << i) <= n; i++)for (int j = 1; j <= n * 2; j++)go[j][i] = go[go[j][i - 1]][i - 1];
}

求答案:

void getans(int x) {int len = a[x].l + m, cur = x, ans = 1;for (int i = log2(maxn); i >= 0; i--) {int pos = go[cur][i];if (pos && a[pos].r < len)ans += (1 << i), cur = pos;}res[a[x].idx] = ans + 1;
}

最后整合到一起就行了。

code
/*
1.MLE?
2.array size enough?
3.long long?
4.overflow long long?
5.multiple task cleaned?
6.freopen?
7.TLE?
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <queue>
#include <string>
#include <cstring>
#include <set>
#include <bitset>
#include <unordered_set>
#include <vector>
#include <deque>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <list>
#include <array>
#include <iterator>
#include <cmath>
#include <new>
#include <random>
#include <cfloat>
#include <cstdlib>
#include <climits>
#include <numeric>
#include <complex>
#include <ctime>
#include <chrono>
#include <thread>
#include <mutex>
#include <future>
#include <exception>
#include <stdexcept>
#include <cstdint>
#include <cassert>
#include <stack>
#include <cctype>
#define DEBUG
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;using ll = long long;
using ull = unsigned long long;
using lb = long double;constexpr int mod = 998244353;
constexpr int maxn = 200005;int n, m;int go[maxn << 1][20], res[maxn];struct node {int idx, l, r;bool operator < (const node &b) const {return l < b.l;}
} a[maxn << 1];void init() {int nxt = 1;for (int i = 1; i <= n * 2; i++) {while (nxt <= n * 2 && a[nxt].l <= a[i].r)nxt++;go[i][0] = nxt - 1;}for (int i = 1; (1 << i) <= n; i++)for (int j = 1; j <= n * 2; j++)go[j][i] = go[go[j][i - 1]][i - 1];
}void getans(int x) {int len = a[x].l + m, cur = x, ans = 1;for (int i = log2(maxn); i >= 0; i--) {int pos = go[cur][i];if (pos && a[pos].r < len)ans += (1 << i), cur = pos;}res[a[x].idx] = ans + 1;
}int main() {fast;cin >> n >> m;for (int i = 1; i <= n; i++) {cin >> a[i].l >> a[i].r;a[i].idx = i;if (a[i].r < a[i].l)a[i].r += m;}sort (a + 1, a + n + 1);for (int i = 1; i <= n; i++) {a[i + n].idx = a[i].idx;a[i + n].l = a[i].l + m;a[i + n].r = a[i].r + m;}init();for (int i = 1; i <= n; i++)getans(i);for (int i = 1; i <= n; i++)cout << res[i] << ' ';return 0;
}
http://www.jsqmd.com/news/371541/

相关文章:

  • 《构建之法》第三章读后感
  • 26.2.11
  • Linux - 网络命令(基础且实用)
  • springboot社区老年中心活动管理系统vue
  • 深入探讨大数据领域Kafka的消息队列监控
  • AI副业:用国产“小龙”Kimi 2.5快速开发小游戏
  • vue springboot星巴克咖啡店管理系统
  • c#变长关键字和参数默认值
  • springboot广府传统文化交互旅游文创商城平台vue可视化大屏
  • springboot求职与招聘系统vue-企业资料上传审核_x2puw7vb
  • 分词器(Tokenizer)-sentencepiece(把训练语料中的字符自动组合成一个最优的子词(subword)集合。) - 教程
  • GPT-5.3和Claude 4.6打架,我却在偷偷用“向量引擎”造核弹?OpenClaw/opencode配置保姆级教程(内含福利)
  • springboot-vue蔬菜水果商城批发系统的设计与实现
  • 工业级串口防粘包状态机的完整 C# 实现,适用于工控机上位机场景
  • YOLO26涨点改进| 全网独家创新、特征融合改进篇 | TGRS 2025顶刊| 引入MROD -YOLO的 MSIA多尺度迭代聚合模块,强化语义特征之间交互,提升复杂环境中小目标检测,多模态融合
  • springboot墓园墓地管理系统vue
  • python vue基于Django的医院管理系统
  • 干测绘的嘴真严啊!测绘转码人数占20.53%,背后原因揭秘→
  • mindcraft玩了4小时评价
  • 基于Python的热门游戏推荐系统的设计与实现源码文档部署文档代码讲解等
  • nodejs基于Vue技术的营养食品搭配分享系统
  • 机器学习中的逻辑回归:从理论到实践
  • php+vue新疆数字证书认证政府中心网站建设
  • 3款降AI工具实测对比,最便宜的效果竟然最好
  • 知网AIGC检测怎么查?从注册到看懂报告完整教程
  • 基于Python的电商用户购买行为数据分析系统源码文档部署文档代码讲解等
  • 基于Python的电商用户行为分析系统源码文档部署文档代码讲解等
  • mindcraft部署
  • 微信小程序基于Android的垂钓渔具销售商城 钓鱼钓友交流平台的设计与实现
  • 比话降AI和嘎嘎降AI哪个好?花了100块实测对比告诉你