题解:AT_abc426_c Upgrade Required

· · 题解

解题思路

\sum_{k = f}^{x_{i}} E_{k}

时间复杂度分析

本题的时间限制是 2s,数据限制是 2 \le N \le 10^{6}, 1 \le Q \le 2 \times 10^{5}。

经过优化的代码时间复杂度为 O(N + Q),可以通过此题目。

代码实现

/*
思路:
注意到序列  ∀版本 <= xi,那么都会移动到 yi,所以每次找 <= xi 的值时,找 x_i-1 + 1往后找即可

然后每次查找的时候,前缀和记录,然后对于那个电脑版本清零,然后 yi += 前缀和

输出前缀和

Furry qwq!
*/
#include<bits/stdc++.h>
#define Furry return 0;
using namespace std;
int n, last_x = 0;
int furry[1000010];
void solve(int x, int y) {
    int qz = 0;
    for(int i = last_x + 1; i <= x; i++) {
        qz += furry[i];
        furry[i] = 0;
    }
    last_x = max(last_x, x);
    furry[y] += qz;
    cout <<qz <<endl;
    return;
}
inline int read() {
    int x = 0, f = 1;
    char c = getchar_unlocked();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar_unlocked();}
    while(c <= '9' && c >= '0') {x = x * 10 + c - '0'; c = getchar_unlocked();}
    return x * f;
}
signed main() {
    n = read();
    for(int i = 1; i <= n; i++) {
        furry[i] = 1;
    }
    int ask = read();
    while(ask--) {
        int x = read(), y = read();
        solve(x, y);
    }
    Furry
}