ABC260 E - At Least One(双指针)

Posted Herio

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABC260 E - At Least One(双指针)相关的知识,希望对你有一定的参考价值。


ABC260 E - At Least One(双指针)

一开始想到two pointers 了,但是不知道怎么快速维护是否满足条件。

原来开一个vector,然后用cnt变量存当前达到的组数。然后只对变为的桶影响cnt。

因为每个

因此复杂度是:

#include <iostream>
#include <vector>
using namespace std;

int main()
int N, M;
cin >> N >> M;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) cin >> A[i] >> B[i];
vector<vector<int>> inv(M + 1);
for (int i = 0; i < N; i++)
inv[A[i]].push_back(i);
inv[B[i]].push_back(i);

vector<int> cnt(N), ans(M + 3);
int cnt_zero = N;
for (int i = 1, j = 1; i <= M;)
while (j <= M and cnt_zero != 0)
for (auto& x : inv[j])
if (cnt[x] == 0) cnt_zero--;
cnt[x]++;

j++;

if (cnt_zero != 0) break;
ans[j - i]++, ans[M + 1 - i + 1]--;
for (auto& x : inv[i])
cnt[x]--;
if (cnt[x] == 0) cnt_zero++;

i++;

for (int i = 1; i <= M; i++)
ans[i] += ans[i - 1];
cout << ans[i] << " \\n"[i == M];


根据双指针,我们可以枚举左端点,右端点其实我们只需要更新

这里 是所有左端点为以上是关于ABC260 E - At Least One(双指针)的主要内容,如果未能解决你的问题,请参考以下文章

ABC260 E - At Least One(双指针)

docker出现GPG error: At least one invalid signature was encountered 相关问题及解决方法

yarn出现“There are no scenarios ; must have at least one"

yarn出现“There are no scenarios ; must have at least one"

At least one JAR was scanned for TLDs yet contained no TLDs.

phpmailer报 You must provide at least one recipient email address.解决办法