分块TLE
查看原帖
分块TLE
1050664
_Ubuntu_楼主2024/3/30 21:56
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10,INF=0x3f3f3f3f;
#define ll long long

const int K = 9;

int st[N];
int en[N];

int f[N];
//f[i]:表示在i这个位置最少要跳多少次能跳出这个块
int top[N];
//top[i]:表示跳出 i 这个块最后到达那个位置
int a[N];

int main()
{
	ios::sync_with_stdio(false) , cin.tie(0);

	int n;
	cin >> n;

	for(int i=0; i<n; i++)	cin >> a[i];

	for(int i=0; i<n; i++)		en[i>>K] = i;

	for(int i=n-1; i>=0; i--)	st[i>>K] = i;

	for(int i=n-1; i>=0; i--)
	{
		if(i + a[i] > en[i>>K])
		{
			f[i] = 1 , top[i] = i + a[i];
		}else{
			f[i] = 1 + f[i+a[i]] , top[i] = top[i+a[i]];
		}
	}

	int q;
	cin >> q;

	while(q--)
	{
		int op, x;
		cin >> op >> x;
		if(op==1)
		{
			int ans = 0;
			while(x < n)
			{
				ans += f[x] , x = top[x];
			}
			cout << ans << '\n';
		}else{
			int y;
			cin >> y;
			a[x] = y;
			for(int i=en[x>>K]; i>=st[i>>K]; i--)
				if(i + a[i] > en[i>>K])
				{
					f[i] = 1 , top[i] = i + a[i];
				}else{
					f[i] = 1 + f[i+a[i]] , top[i] = top[i+a[i]];
				}
		}
	}

	return 0;
}
2024/3/30 21:56
加载中...