跳到正文

题解

P12646【升序】题解

Fwxyz约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年11月18日
收录
2026年9月29日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P12646

版本

  1. v1当前

解题思路

注意到每次一个数乘 2 可能会导致后面的一段需要乘 2,并且一定是连续在这个点后面的一段。

具体的,可以令 cic_i 表示需要让 aia_i 和 ai+1a_{i+1} 满足不降的次数,如果 ai>ai+1a_i>a_{i+1},则为让 ai≤ai+1a_i\le a_{i+1} 的操作次数的相反数。

设 sl,rs_{l,r} 为 ∑i=lrci\sum\limits_{i=l}^r{c_i}

那么对于每个点,就可以将问题转化为求 ∑i=lrmax⁡(0,sl,i)\sum\limits_{i=l}^r\max({0,s_{l,i}}),即每个点如果前面乘的 2 不够让 ai>ai+1a_i> a_{i+1},那么它前面乘的 2 就不用继续往后乘,否则就需要给 aia_i 乘上前面的 2 的个数加上当前的 cic_i。

我们考虑如果没有与 0 取 max⁡\max,那么贡献可以直接算出,即 sl,r×(r+1)−∑i=lri×cis_{l,r}\times(r+1)-\sum\limits_{i=l}^r{i\times c_i}。

现在我们考虑处理与 0 取 max⁡\max。我们会发现每一次取 0,后面一个数必然是这一段的开头至少 2 倍,所以直接每次跳 0 的段的复杂度为 log⁡V\log V。关于跳小于 0 的段,直接单调栈维护,每次 O(1)\mathcal{O}(1) 跳即可。

温馨提示:对于当前的下一个小于 0 的位置是下一个位置,直接跳过着一个位置即可。

具体实现

C++
#include <bits/stdc++.h>
using i64 = long long;
using ui64 = unsigned long long;
using namespace std;
namespace Fastio
{
	const int L = 1 << 20;
	inline char gc ()
	{
		static char buf[L], *l = buf, *r = buf;
		if (l == r)
			r = (l = buf) + fread (buf, 1, L, stdin);
		return (l == r) ? EOF : *l ++;
	}
	char obuf[L], *p = obuf;
	inline void pc (char c)
	{
		if (p == obuf + L)
		fwrite (obuf, 1, L, stdout),
			p = obuf;
		*p ++ = c;
	}
	inline void flush ()
		{fwrite (obuf, 1, p - obuf, stdout);}
	struct Reader
	{
		template <typename T>
		inline Reader& operator >> (T& x)
		{
			x = 0;
			short f = 1;
			char c = gc ();
			while (! isdigit (c))
			{
				if (c == '-')
					f = -1;
				c = gc ();
			}
			while (isdigit (c))
				x = (x << 3) + (x << 1) + (c ^ '0'),
				c = gc ();
			x *= f;
			return *this;
		}
		Reader (){}
	}cin;
	struct Writer
	{
		template <typename T>
		inline Writer& operator << (T x)
		{
			if (x < 0)
				pc ('-'),
				x = -x;
			static short tp = 0, s[40];
			do
				s[++ tp] = x % 10,
				x /= 10;
			while (x);
			while (tp)
				pc (s[tp --] + '0');
			return *this;
		}
		inline Writer& operator << (const char* s)
		{
			int i = 0;
			while (s[i])
				pc (s[i ++]);
			return *this;
		}
		inline Writer& operator << (char c)
		{
			pc (c);
			return *this;
		}
		Writer (){}
		~Writer (){flush();}
	}cout;
	void ReadStr (string &s)
	{
		s = "\0";
		char c = gc ();
		while (c == ' ' || c == '\n' || c == '\r')
			c = gc ();
		while (c != ' ' && c != '\n' && c != '\r' && c != EOF)
			s += c,
			c = gc ();
	}
}
#define cin Fastio::cin
#define cout Fastio::cout
#define ReadStr Fastio::ReadStr
using namespace std;
const int N = 3e5 + 5;
int T = 1, n, q, a[N], p[N], nxt[N], st[N], tp;
i64 s[N], s1[N];
i64 calc (int l, int r)
	{return (r + 1) * (s[r] - s[l - 1]) - s1[r] + s1[l - 1];}
void solve ()
{
	cin >> n >> q;
	for (int i = 1; i <= n; ++ i)
		cin >> a[i];
	bool fl = 1;
	for (int i = 1; i < n; ++ i)
	{
		int x = min (a[i], a[i + 1]), y = max (a[i], a[i + 1]);
		while (x <= y)
			++ p[i],
			x <<= 1;
		-- p[i];
		if (a[i] < a[i + 1])
			p[i] *= -1;
		else
			p[i] += (x >> 1) != y;
		s[i] = s[i - 1] + p[i];
		s1[i] = s1[i - 1] + 1ll * i * p[i];
	}
	for (int i = n - 1; i >= 0; -- i)
	{
		while (tp && s[st[tp]] >= s[i])
			-- tp;
		nxt[i] = (tp ? st[tp] : n);
		st[++ tp] = i;
	}
	while (q --)
	{
		int l, r;
		i64 res = 0;
		cin >> l >> r;
		while (l < r)
		{
			int j = min (nxt[l - 1] - 1, r - 1);
			if (l <= j)
				res += calc (l, j);
			else
				j = l;
			l = j + 1;
		}
		cout << res << '\n';
	}
}
signed main ()
{
	while (T --)
		solve ();
	return 0;
}
原文