求调 TLE了一个点
查看原帖
求调 TLE了一个点
1369607
luckyqwq楼主2024/10/10 20:06
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, m, a[N], b[N], sum[N], dp[N], nex[N];
stack < int > stk;
bool f;
inline int read(){
    int x = 0, r = 1;
    char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') r = -1; c = getchar();}
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x * r;
}
signed main() {
//	freopen ("P6006_8.in", "r", stdin);
//	freopen ("P6006_8.ans", "w", stdout);
    std::ios::sync_with_stdio(0);

	cin >> n >> m;

	for (int i = 1;i <= n; ++ i)
	{
		cin >> a[i] >> b[i];
		nex[i] = -1;
		if (a[i] <= a[i - 1]) f = 1;
	}
    //printf("%d:\n", __LINE__);
	for (int i = 1;i <= n; ++ i) sum[i] = sum[i - 1] + b[i];
	if (!f)
	{
		while (m --)
		{
			int x, y;
			cin >> x >> y;
			if (y <= b[x])
			{
				cout << x << "\n";
			}
			else if (y > sum[n] - sum[x - 1])
			{
				cout << "0\n";
			}
			else
			{
				int l = x + 1, r = n;
				while (l <= r)
				{
					int mid = (l + r) >> 1;
					if (sum[mid] - sum[x - 1] < y) l = mid + 1;
					else r = mid - 1;
				}
				cout << l << "\n";
			}
		}
	}
	else
	{
		for(int i = n;i >= 1; -- i)
	    {
            while(stk.size() && a[stk.top()] <= a[i]) stk.pop();
            if(stk.size() == 0) nex[i] = 0;
            else nex[i] = stk.top();
            stk.push(i);
	    }
//		for(int i = 1;i <= n; ++ i) cout << nex[i] << " ";
//		cout << "\n";
		while (m --)
		{
			int l, r;
			cin >> l >> r;
			int s = 0, p = l, idx = 0;
			while (1)
			{
				if (p == 0)
                {
                    idx = 0;
                    break;
                }
				if (r - s <= b[p])
				{
					idx = p;
					break;
				}
				else
				{
					s += b[p];
					p = nex[p];
				}
			}
			cout << idx <<"\n";
		}
	}
	return 0;
}
//5 5
//1 2
//2 3
//3 4
//4 5
//5 6


//6 5
//4 10
//6 8
//3 5
//4 14
//10 9
//4 20
//1 25
//6 30
//5 8
//3 13
//2 8

2024/10/10 20:06
加载中...