ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

洛谷 P1903 [国家集训队] 数颜色 / 维护队列 & 带修莫队相关

2022-06-11 22:34:02  阅读:128  来源: 互联网

标签:MN 洛谷 int belong P1903 ++ last now 国家集训队


洛谷P1903 [国家集训队] 数颜色 / 维护队列 & 带修莫队相关

[洛谷P1903 [国家集训队] 数颜色 / 维护队列]([P1903 国家集训队] 数颜色 / 维护队列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn))

  1. 带修莫队\(block = pow(n, 2.0 / 3.0)\)。

  2. [奇技淫巧](题解 P1903 【【模板】分块/带修改莫队(数颜色)】 - attack 的博客 - 洛谷博客 (luogu.com.cn))

    void upd(int now, int p) {
        if (c[now].pos >= q[p].l && c[now].pos <= q[p].r) {
            cnt[a[c[now].pos]]--;
            if (cnt[a[c[now].pos]] == 0) sum--;
            cnt[c[now].val]++;
            if (cnt[c[now].val] == 1) sum++;
        }
        swap(c[now].val, a[c[now].pos]);
    }
    
    1. 往后挪 先加再扔进函数 ,往前挪 先扔进函数再加约等于 一个\(add\) 一个\(del\)。

      while (now < q[i].last) upd(++now, i);
              while (now > q[i].last) upd(now--, i);
      

AC Code

#include <bits/stdc++.h>

using namespace std;

const int MN = 2e6 + 10;

int n, m, block, q_, c_, sum;
int a[MN], belong[MN], cnt[MN], ans[MN];
struct Query {
    int l, r, last, id;
    bool operator < (const Query &x) const {
        return belong[l] != belong[x.l] ? belong[l] < belong[x.l] : belong[r] != belong[x.r] ? belong[r] < belong[x.r] : last < x.last;
    }
} q[MN];
struct Change {
    int pos, val;
} c[MN];

void add(int x) {
    cnt[x]++;
    sum += cnt[x] == 1;
}

void del(int x) {
    cnt[x]--;
    sum -= cnt[x] == 0;
}

void upd(int now, int p) {
    if (c[now].pos >= q[p].l && c[now].pos <= q[p].r) {
        cnt[a[c[now].pos]]--;
        if (cnt[a[c[now].pos]] == 0) sum--;
        cnt[c[now].val]++;
        if (cnt[c[now].val] == 1) sum++;
    }
    swap(c[now].val, a[c[now].pos]);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m; block = pow(n, 2.0 / 3.0);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        belong[i] = (i - 1) / block + 1;
    }
    for (int i = 1; i <= m; ++i) {
        char op;
        cin >> op;
        if (op == 'Q') {
            cin >> q[++q_].l >> q[q_].r;
            q[q_].last = c_;
            q[q_].id = q_;
        }
        else cin >> c[++c_].pos >> c[c_].val;
    }
    sort(q + 1, q + 1 + m);
    for (int i = 1, l = 1, r = 0, now = 0; i <= m; ++i) {
        while (l > q[i].l) add(a[--l]);
        while (r < q[i].r) add(a[++r]);
        while (l < q[i].l) del(a[l++]);
        while (r > q[i].r) del(a[r--]);
        while (now < q[i].last) upd(++now, i);
        while (now > q[i].last) upd(now--, i);
        ans[q[i].id] = sum;
    }
    for (int i = 1; i <= q_; ++i) cout << ans[i] << '\n';
    return 0;
}

标签:MN,洛谷,int,belong,P1903,++,last,now,国家集训队
来源: https://www.cnblogs.com/zjsqwq/p/16366999.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有