桶维护 | #不点两面(hard version)#

不点两面(hard version)

https://www.nowcoder.com/practice/c49e65f6b73c491ab96840cbd230b2f1

直接用两个维护即可, 算法时间复杂度

#include <bits/stdc++.h>

#define x first
#define y second
#define all(x) x.begin(), x.end()

using namespace std;
using i128 = __int128;
using u128 = unsigned __int128;

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e5 + 10, MOD = 998244353;
const int INF = 1e9;
const LL LL_INF = 1e18;
const LD EPS = 1e-8;
const int dx4[] = {-1, 0, 1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};

istream &operator>>(istream &is, i128 &val) {
    string str;
    is >> str;
    val = 0;
    bool flag = false;
    if (str[0] == '-') flag = true, str = str.substr(1);
    for (char &c: str) val = val * 10 + c - '0';
    if (flag) val = -val;
    return is;
}

ostream &operator<<(ostream &os, i128 val) {
    if (val < 0) os << "-", val = -val;
    if (val > 9) os << val / 10;
    os << static_cast<char>(val % 10 + '0');
    return os;
}

void solve() {
    int m, q;
    cin >> m >> q;
    vector<int> a(m + 1), cnt(m + 1);

    int ans = 0;
    while (q--) {
        int op, v;
        cin >> op >> v;
        if (op == 1) {
            int i = v - 3;
            int j = v + 3;
            if (i >= 1) {
                cnt[i]++;
                if (!a[i]) ans++, a[i] = 1;
            }
            if (j <= m) {
                cnt[j]++;
                if (!a[j]) ans++, a[j] = 1;
            }
            cout << ans << '\n';
        }
        else {
            int i = v - 3, j = v + 3;
            if (i >= 1) {
                cnt[i]--;
                if (cnt[i] == 0) {
                    a[i] = 0;
                    ans--;
                }
            }
            if (j <= m) {
                cnt[j]--;
                if (cnt[j] == 0) {
                    a[j] = 0;
                    ans--;
                }
            }
            cout << ans << '\n';
        }
    }
}

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

    int T = 1;
    while (T--) solve();
    cout << fixed << setprecision(15);

    return 0;
全部评论

相关推荐

05-16 21:54
已编辑
门头沟学院 前端工程师
蓝曦111:我也是25届,这是第二次被裁了,毕业没到一年就失业两次,两次都是公司问题。第一家才转正一个月,跟我说公司拿不到项目结款没办法,赔了一个月;第二个公司连工资都发不出来了,赔偿更别想了,我算是认命了,这条路也不知道能走多远走多久,不过生活还是要继续的,走一步看一步吧
当你面对裁员会如何?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务