[HNOI2010] 弾飞绵羊
题目链接:##
题目分析:##
题外话:
我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出:
根号算法牛逼!!!
显然,这是一道LCT裸题,然而在下并不会LCT于是采用了分块瞎搞
对于每个点维护两个信息:跳出块的步数\(step[i]\)和跳出块的落点\(lo[i]\)
预处理时使用类似模拟的方法。每次只处理还未处理过的点,并且对于每次处理顺带将向后跳到过的点也处理掉,具体见代码。
对于两种操作:
- 查:利用记录的落点在大块上跳并累加答案,单次复杂度\(O(\sqrt{n})\)
- 修:分析得到,每个点维护的两个信息都只与其块内的信息有关,所以仅需要重构一遍修改点所在块即可,单次复杂度\(O(\sqrt{n})\)
总复杂度约为\(O(m\sqrt{n})\)
代码:##
#include<bits/stdc++.h>
#define N 200010
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c;
c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -f;
c = getchar();
}
while (isdigit(c)) {
cnt = cnt * 10 + c - '0';
c = getchar();
}
return cnt * f;
}
int q, pos[N], n, m, L[N], R[N], step[N], lo[N], sta[N], top, a[N], opr, x, k;
bool flag = false;
void pre_work() {
q = sqrt(n);
for (register int i = 1; i <= q; i++) {
L[i] = (i - 1) * q + 1;
R[i] = i * q;
}
if (R[q] < n) {
q++;
L[q] = R[q - 1] + 1;
R[q] = n;
}
for (register int i = 1; i <= q; i++)
for (register int j = L[i]; j <= R[i]; j++)
pos[j] = i;
for (register int i = 1; i <= n; i++) {
if(step[i]) continue;
flag = false;
sta[++top] = i; int now = i;
while(pos[now] == pos[now + a[now]]) {
if (step[now + a[now]]) {
flag = 1;
break;
} else {
now += a[now];
sta[++top] = now;
}
}
int total = top + 1;
while (top) {
if (!flag) {
lo[sta[top]] = now + a[now];
step[sta[top]] = total - top;
} else {
lo[sta[top]] = lo[now + a[now]];
step[sta[top]] = total - top + step[now + a[now]];
}
--top;
}
}
}
void change(int q,int k) {
int p = pos[q];
a[q] = k;
for (register int i = L[p]; i <= R[p]; i++) lo[i] = step[i] = 0;
for (register int i = L[p]; i <= R[p]; i++) {
if(step[i]) continue;
flag = false;
sta[++top] = i; int now = i;
while(pos[now] == pos[now + a[now]]) {
if (step[now + a[now]]) {
flag = 1;
break;
} else {
now += a[now];
sta[++top] = now;
}
}
int total = top + 1;
while (top) {
if (!flag) {
lo[sta[top]] = now + a[now];
step[sta[top]] = total - top;
} else {
lo[sta[top]] = lo[now + a[now]];
step[sta[top]] = total - top + step[now + a[now]];
}
--top;
}
}
}
int query(int i) {
int v = i;
int ans= 0;
while (pos[v]) {
ans += step[v];
v = lo[v];
}
return ans;
}
int main() {
n = read();
for (register int i = 1; i <= n; i++) a[i] = read();
m = read();
pre_work();
for (register int i = 1; i <= m; i++) {
opr = read(); x = read();
if (opr == 1) printf("%d\n", query(x + 1));
if (opr == 2) {
k = read();
change(x + 1, k);
}
}
return 0;
}
[HNOI2010] 弾飞绵羊的更多相关文章
- P3203 [HNOI2010]弹飞绵羊(LCT)
P3203 [HNOI2010]弹飞绵羊 LCT板子 用一个$p[i]$数组维护每个点指向的下个点. 每次修改时cut*1+link*1就解决了 被弹出界时新设一个点,权为0,作为终点表示出界点.其他 ...
- [HNOI2010] 弹飞绵羊 (分块)
[HNOI2010] 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上 ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- 「洛谷P3202」[HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- [Luogu P3203] [HNOI2010]弹飞绵羊 (LCT维护链的长度)
题面 传送门:洛谷 Solution 这题其实是有类似模型的. 我们先考虑不修改怎么写.考虑这样做:每个点向它跳到的点连一条边,最后肯定会连成一颗以n+1为根的树(我们拿n+1代表被弹出去了).题目所 ...
- P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?LCT?...FAQ orz
好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...
- P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?
好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...
- 洛谷P3203 [HNOI2010] 弹飞绵羊 [LCT]
题目传送门 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
随机推荐
- js中使用对象变量的两种方式
function Person(){ this.a=function(){ window.alert("a"); } this.b=function(){ window.alert ...
- html5常用模板下载网站
html5常用模板下载网站 开创者素材.站长素材.模板之家 推荐葡萄家园素材网,他们网页模板栏目有个HTML模板,很多静态源码.应该是你所需要的. html静态页面模板 还是服饰素材啊 朋友 设计云 ...
- Linux下查看端口占用情况
用启动服务的账号登录,然后运行命令: lsof -i:<端口号> 也可使用命令: netstat -apn|grep <端口号> 找到进程号以后,再使用以下命令查看详细信息: ...
- ES BM25 TF-IDF相似度算法设置——
Pluggable Similarity Algorithms Before we move on from relevance and scoring, we will finish this ch ...
- 使用TortoiseGit同步代码到github远程仓库
1.clone github上的代码仓库的URL 可以用HTTPS,SSH, or Subversion 2.同步push 到远程仓库时 要用 SSH地址,同生成SSH private key ,在g ...
- POJ1195Mobile phones (从二维树状数组到cdq分治)
Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...
- UOJ_21_【UR #1】缩进优化_数学
UOJ_21_[UR #1]缩进优化_数学 题面:http://uoj.ac/problem/21 最小化$\sum\limits{i=1}^{n}a[i]/x+a[i]\;mod\;x$ =$\su ...
- <十六>UML核心视图动态视图之状态图
一:状态图 --->状态图显示一个状态机. --->状态机用于对模型元素的动态性进行建模.更具体地说,就是对系统行为中受事件驱动的方面进行建模. --->通常使用状态图来说明业务角色 ...
- 【Lintcode】094.Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- 【LeetCode】047. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...