题解

想出70的大众分之后就弃疗了,正解有点神仙

就是首先有个比较显然的结论,就是要么是一直往左走,要么是走一步右边,然后一直往左走

根据这个可以结合RMQ写个70分的暴力

我们就考虑,最优的话显然是走一步左边就到了目标点,第二步才开始有分叉

假如我们先走了一步左边,然后就变成了,从\(L[x]\)开始走,下一步可以走到\([L[x],N]\)的所有点最小的转移点之前,之后再把后来走的点代价都加上1即可

这样的话,不管是一直走左边,还是走了一步右边再走了左边,情况都被包含了

这个时候考虑这个问题就比较简单了,可以使用倍增

\(f[i][j]\)表示\([i,n]\)内最小的\(l[x]\)的值

\(s[i][j]\)表示\(i\)走到\(f[i][j]\)内所有点的距离和

转移就是

\(f[i][j] = f[f[i][j - 1]][j - 1]\)

\(s[i][j] = s[i][j - 1] + s[f[i][j - 1]][j - 1] + 2^{j - 1} * (f[i][j - 1] - f[i][j])\)

查询两端前缀和,查的时候直接把\(x\)变成\(L[x]\)进行倍增即可

代码

  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pii pair<int,int>
  5. #define pdi pair<db,int>
  6. #define mp make_pair
  7. #define pb push_back
  8. #define enter putchar('\n')
  9. #define space putchar(' ')
  10. #define eps 1e-8
  11. #define mo 974711
  12. #define MAXN 300005
  13. //#define ivorysi
  14. using namespace std;
  15. typedef long long int64;
  16. typedef double db;
  17. template<class T>
  18. void read(T &res) {
  19. res = 0;char c = getchar();T f = 1;
  20. while(c < '0' || c > '9') {
  21. if(c == '-') f = -1;
  22. c = getchar();
  23. }
  24. while(c >= '0' && c <= '9') {
  25. res = res * 10 + c - '0';
  26. c = getchar();
  27. }
  28. res *= f;
  29. }
  30. template<class T>
  31. void out(T x) {
  32. if(x < 0) {x = -x;putchar('-');}
  33. if(x >= 10) {
  34. out(x / 10);
  35. }
  36. putchar('0' + x % 10);
  37. }
  38. int N,L[MAXN];
  39. int f[MAXN][20];
  40. int64 s[MAXN][20];
  41. void Init() {
  42. read(N);
  43. for(int i = 2 ; i <= N ; ++i) read(L[i]);
  44. f[N][0] = L[N];s[N][0] = N - L[N];
  45. for(int i = N - 1 ; i >= 1 ; --i) {
  46. f[i][0] = min(f[i + 1][0],L[i]);s[i][0] = i - f[i][0];
  47. }
  48. for(int j = 1 ; j <= 19 ; ++j) {
  49. for(int i = 1 ; i <= N ; ++i) {
  50. f[i][j] = f[f[i][j - 1]][j - 1];
  51. s[i][j] = s[i][j - 1] + s[f[i][j - 1]][j - 1] + 1LL * (f[i][j - 1] - f[i][j]) * (1 << j - 1);
  52. }
  53. }
  54. }
  55. int64 gcd(int64 a,int64 b) {
  56. return b == 0 ? a : gcd(b,a % b);
  57. }
  58. int64 Calc(int tar,int st) {
  59. if(tar >= L[st]) return st - tar;
  60. int64 res = st - L[st];st = L[st];
  61. int64 sum = 1;
  62. for(int j = 19 ; j >= 0 ; --j) {
  63. if(f[st][j] >= tar) {
  64. res += s[st][j];
  65. res += 1LL * sum * (st - f[st][j]);
  66. st = f[st][j];
  67. sum += 1 << j;
  68. }
  69. }
  70. res += 1LL * (sum + 1) * (st - tar);
  71. return res;
  72. }
  73. void Solve() {
  74. int Q;int l,r,x;
  75. read(Q);
  76. while(Q--) {
  77. read(l);read(r);read(x);
  78. int64 u = Calc(l,x) - Calc(r + 1,x),d = r - l + 1,g = gcd(u,d);
  79. u /= g;d /= g;
  80. out(u);putchar('/');out(d);enter;
  81. }
  82. }
  83. int main() {
  84. #ifdef ivorysi
  85. freopen("f1.in","r",stdin);
  86. #endif
  87. Init();
  88. Solve();
  89. }

【LOJ】#6435. 「PKUSC2018」星际穿越的更多相关文章

  1. LOJ #6435. 「PKUSC2018」星际穿越(倍增)

    题面 LOJ#6435. 「PKUSC2018」星际穿越 题解 参考了 这位大佬的博客 这道题好恶心啊qwq~~ 首先一定要认真阅读题目 !! 注意 \(l_i<r_i<x_i\) 这个条 ...

  2. LOJ 6435 「PKUSC2018」星际穿越——DP+倍增 / 思路+主席树

    题目:https://loj.ac/problem/6435 题解:https://www.cnblogs.com/HocRiser/p/9166459.html 自己要怎样才能想到怎么做呢…… dp ...

  3. loj#6435. 「PKUSC2018」星际穿越(倍增)

    题面 传送门 题解 我们先想想,在这个很特殊的图里该怎么走最短路 先设几个量,\(a_i\)表示\([a_i,i-1]\)之间的点都和\(i\)有边(即题中的\(l_i\)),\(l\)表示当前在计算 ...

  4. #6435. 「PKUSC2018」星际穿越

    考场上写出了70分,现在填个坑 比较好写的70分是这样的:(我考场上写的贼复杂) 设\(L(i)=\min_{j=i}^nl(j)\) 那么从i开始向左走第一步能到达的就是\([l(i),i-1]\) ...

  5. 「PKUSC2018」星际穿越 (70分做法)

    5371: [Pkusc2018]星际穿越 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 27  Solved: 11[Submit][Status] ...

  6. 「PKUSC2018」星际穿越(倍增)

    倍增好题啊! 我们我们预处理 \(f[x][i]\) 表示 \(x\) 点最左到达的端点,\(sum[x][i]\) 表示 \(x\) 点最左到达的端点时 \(f[x][i]\sim x\) 的答案, ...

  7. 「PKUSC2018」星际穿越

    传送门 Solution  倍增 Code  #include <bits/stdc++.h> #define reg register #define ll long long usin ...

  8. LOJ #6436. 「PKUSC2018」神仙的游戏(字符串+NTT)

    题面 LOJ #6436. 「PKUSC2018」神仙的游戏 题解 参考 yyb 的口中的长郡最强选手 租酥雨大佬的博客 ... 一开始以为 通配符匹配 就是类似于 BZOJ 4259: 残缺的字符串 ...

  9. LOJ #6432. 「PKUSC2018」真实排名(组合数)

    题面 LOJ #6432. 「PKUSC2018」真实排名 注意排名的定义 , 分数不小于他的选手数量 !!! 题解 有点坑的细节题 ... 思路很简单 , 把每个数分两种情况讨论一下了 . 假设它为 ...

随机推荐

  1. SSH & Git

    SSH基本用法 SSH服务详解 work with git branch some tips for git setup and git config git and github ssh servi ...

  2. 【转】在windows中使用Intellij Idea时选择自定义的64位JVM

    原文地址:https://www.iflym.com/index.php/code/201404190001.html 本文英文原文自:https://intellij-support.jetbrai ...

  3. python 获取自身ip

    原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  4. package.json浅谈

    相信很多小伙伴都见过各种各样的Node.js项目,而里面都有一个名为package.json的文件,而这个文件究竟是干什么的呢? 简单的来说,这个文件就是对整个项目的各种情况的配置(也是介绍),下面给 ...

  5. UnicodeDecodeError gbk codec can't decode byte in position illegal multibyte sequence

    UnicodeDecodeError:'gbk' codec can't decode byte in position : illegal multibyte sequence 觉得有用的话,欢迎一 ...

  6. disabled属性对form表单向后台传值的影响

    在form表单里,如果对input加入disabled="disabled"或disabled="true"等属性,form表单提交的时候,就不会传值到后台. ...

  7. 数据库类型与JDBC TYPE 和Java类型对应关系

    https://blog.csdn.net/seelye/article/details/40105969

  8. bzoj千题计划174:bzoj1800: [Ahoi2009]fly 飞行棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=1800 圆上两条直径构成矩形的对角线 #include<cstdio> using nam ...

  9. 无锁并发框架Disruptor学习入门

    刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...

  10. inux系统用户名和全名有什么区别

    问:linux系统安装完毕,进入系统,创建用户的时候,要填入用户名和全名,请问用户名和全名有什么区别,登录的时候,是用户名还是全名? ================================= ...