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

题解:P15539 [CCC 2026 J4] Snail Path

模拟题。

蜗牛爬过的方格总数不超过 \(M\times 20+1\le 4\times 10^6+1\)。我们对无限网格建系,设蜗牛初始位置是 \((0,0)\),上、下、左、右分别对应 \(y\) 轴正方向、\(y\) 轴负方向、\(x\) 轴负方向、\(x\) 轴正方向,使用哈希表(set)记录黏滑的方格,每次移动判断并进行统计即可。

时间复杂度 \(O(M\log M)\)(这里认为每次移动最多 \(20\) 步是常数)。

代码:

// Problem: P15539 [CCC 2026 J4] Snail Path
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P15539
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {uniform_int_distribution<int> dist(L, R);return dist(rnd);
}template<typename T> void chkmin(T& x, T y) {if(y < x) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}template<int mod>
inline unsigned int down(unsigned int x) {return x >= mod ? x - mod : x;
}template<int mod>
struct Modint {unsigned int x;Modint() = default;Modint(unsigned int x) : x(x) {}friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}friend Modint operator/(Modint a, Modint b) {return a * ~b;}friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}friend Modint operator~(Modint a) {return a ^ (mod - 2);}friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}friend Modint& operator++(Modint& a) {return a += 1;}friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}friend Modint& operator--(Modint& a) {return a -= 1;}friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}friend bool operator==(Modint a, Modint b) {return a.x == b.x;}friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};set<tuple<int, int>> S;
int n, x, y, cnt;int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);S.insert({x, y});for(cin >> n; n; --n) {char dir; int steps;cin >> dir >> steps;if(dir == 'N') {while(steps--) {++y;if(S.count({x, y})) ++cnt;S.insert({x, y});}}else if(dir == 'S') {while(steps--) {--y;if(S.count({x, y})) ++cnt;S.insert({x, y});}}else if(dir == 'W') {while(steps--) {--x;if(S.count({x, y})) ++cnt;S.insert({x, y});}}else {while(steps--) {++x;if(S.count({x, y})) ++cnt;S.insert({x, y});}}}cout << cnt << endl;return 0;
}
http://www.jsqmd.com/news/424188/

相关文章:

  • 2026年评价高的广安乘客电梯/成都扶手电梯值得信赖的厂家推荐 - 品牌宣传支持者
  • 情人节送的礼物口碑好的有哪些,价格贵吗 - myqiye
  • 探讨2026年农业用无人机模拟器选购,哪个口碑好 - 工业设备
  • 蒸汽教育背景 - 技研备忘录
  • 蒸汽教育靠谱吗 - 技研备忘录
  • 蒸汽教育老师专业吗 - 技研备忘录
  • 题解:P15538 [CCC 2026 J3] Creative Candy Consumption
  • OpenClaw:真正能帮你“干活”的智能助手,开启自动化新纪元
  • 蒸汽教育找工作怎么样 - 技研备忘录
  • 蒸汽教育怎么样 - 技研备忘录
  • 2026年合肥好用的燃气发电机组制造厂推荐,专业厂家大盘点 - 工业品网
  • 国产化OA系统如何集成WebUploader+PHP支持大文件分片版本回溯?
  • 盘点2026年防爆伺服电机厂家排名,哪家比较靠谱? - mypinpai
  • 告别低效繁琐!千笔AI,断层领先的降AI率软件
  • 果宝农业无花果基地:南京近郊的甜蜜采摘之旅 - 2026年企业推荐榜
  • 富文本编辑器处理Word公式的格式保留技巧?
  • 研究生收藏!抢手爆款的AI论文网站 —— 千笔AI
  • 2026年靠谱的橡塑隔热棉/开口自粘铝箔橡塑管实力厂家如何选 - 品牌宣传支持者
  • 前端如何实现Word图片批量粘贴且保留样式?
  • 芯片制造企业如何选择PDF转Word样式方案?
  • 赶deadline必备!断层领先的AI论文写作软件 —— 千笔·专业论文写作工具
  • 2026年靠谱的橡塑保温板/铝箔橡塑保温板品牌厂家哪家靠谱 - 品牌宣传支持者
  • C#导入CAD,dXF,圆心排序,圆心坐标可直接用于数控钻孔机,切割机调用,请认真看图,看坐标次序
  • 军工科研平台如何用WebUploader+PHP实现实验数据的分片加密续传?
  • 医院PACS系统如何用WebUploader+PHP解决DICOM影像分片传输卡顿?
  • 蒸汽教育好不好 - 技研备忘录
  • 网页编辑器导入微信公众号文章的样式适配方法?
  • 2026年质量好的防火快速门/自动快速门厂家选择指南 - 品牌宣传支持者
  • 信创编辑器支持哪些Word特殊格式导入?
  • Day05 : Gin响应渲染:JSON、XML、文件下载与自定义Writer