Machine Learning(CF940F+待修改莫队)

Posted dillonh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Machine Learning(CF940F+待修改莫队)相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/problemset/problem/940/F

题目:

技术分享图片

技术分享图片

题意:求次数的mex,mex的含义为某个集合(如{1,2,4,5})第一个为出现的非负数(3),注意是次数,而不是某个元素的mex。

思路:这一题数据太大,所以我们首先得进行一次离散化。用一个num2来记录每个次数出现次数,num1来记录次数出现次数,最后用一个for循环来求出mex。

代码实现如下:

  1 #include <set>
  2 #include <map>
  3 #include <queue>
  4 #include <stack>
  5 #include <cmath>
  6 #include <bitset>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 
 16 typedef long long ll;
 17 typedef unsigned long long ull;
 18 
 19 #define bug printf("*********
");
 20 #define FIN freopen("in.txt", "r", stdin);
 21 #define debug(x) cout<<"["<<x<<"]" <<endl;
 22 #define IO ios::sync_with_stdio(false),cin.tie(0);
 23 
 24 const double eps = 1e-8;
 25 const int mod = 1e9 + 7;
 26 const int maxn = 1e5 + 7;
 27 const double pi = acos(-1);
 28 const int inf = 0x3f3f3f3f;
 29 const ll INF = 0x3f3f3f3f3f3f3f3f;
 30 
 31 inline int read() {//读入挂
 32     int ret = 0, c, f = 1;
 33     for(c = getchar(); !(isdigit(c) || c == -); c = getchar());
 34     if(c == -) f = -1, c = getchar();
 35     for(; isdigit(c); c = getchar()) ret = ret * 10 + c - 0;
 36     if(f < 0) ret = -ret;
 37     return ret;
 38 }
 39 
 40 int n, q, block, idq, idc, x, y;
 41 int a[maxn], num1[2 * maxn], num2[2 * maxn];
 42 vector<int> v;
 43 
 44 struct query {
 45     int l, r, id, t, ans;
 46     bool operator < (const query& x) const {
 47         if((l - 1) / block != (x.l - 1) / block) {
 48             return l < x.l;
 49         }
 50         if((r - 1) / block != (x.r - 1) / block) {
 51             return r < x.r;
 52         }
 53         return t < x.t;
 54     }
 55 }ask[maxn];
 56 
 57 struct modify {
 58     int p, pre, val;
 59 }myf[maxn];
 60 
 61 int get_id(int x) {
 62     return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
 63 }
 64 
 65 void add(int x) {
 66     num1[num2[x]]--;
 67     num2[x]++;
 68     num1[num2[x]]++;
 69 }
 70 
 71 void del(int x) {
 72     num1[num2[x]]--;
 73     num2[x]--;
 74     num1[num2[x]]++;
 75 }
 76 
 77 int main() {
 78     //FIN;
 79     num1[0] = 1e8;
 80     n = read();
 81     q = read();
 82     block = 2000;
 83     for(int i = 1; i <= n; i++) {
 84         a[i] = read();
 85         v.push_back(a[i]);
 86     }
 87     int nw = 0;
 88     for(int i = 1; i <= q; i++) {
 89         int op;
 90         op = read();
 91         if(op == 1) {
 92             x = read();
 93             y = read();
 94             idq++;
 95             ask[idq].l = x, ask[idq].r = y;
 96             ask[idq].id = idq;
 97             ask[idq].t = nw;
 98         } else {
 99             x = read();
100             y = read();
101             idc++;
102             nw++;
103             myf[idc].p = x;
104             myf[idc].pre = a[x];
105             myf[idc].val = y;
106             a[x] = y;
107             v.push_back(y);
108         }
109     }
110     sort(v.begin(), v.end());
111     v.erase(unique(v.begin(), v.end()), v.end());
112     sort(ask + 1, ask + idq + 1);
113     for(int i = 1; i <= n; i++) {
114         a[i] = get_id(a[i]);
115     }
116     for(int i = 1; i <= idc; i++) {
117         myf[i].pre = get_id(myf[i].pre);
118         myf[i].val = get_id(myf[i].val);
119     }
120     int tmp = nw, r = 0, l = 1;
121     for(int i = 1; i <= idq; i++) {
122         int res = 1;
123         while(r > ask[i].r) {
124             del(a[r--]);
125         }
126         while(r < ask[i].r) {
127             add(a[++r]);
128         }
129         while(l > ask[i].l) {
130             add(a[--l]);
131         }
132         while(l < ask[i].l) {
133             del(a[l++]);
134         }
135         while(tmp < ask[i].t) {
136             tmp++;
137             if(myf[tmp].p >= l && myf[tmp].p <= r) {
138                 del(myf[tmp].pre);
139                 add(myf[tmp].val);
140             }
141             a[myf[tmp].p] = myf[tmp].val;
142         }
143         while(tmp > ask[i].t) {
144             if(myf[tmp].p >= l && myf[tmp].p <= r) {
145                 del(myf[tmp].val);
146                 add(myf[tmp].pre);
147             }
148             a[myf[tmp].p] = myf[tmp].pre;
149             tmp--;
150         }
151         while(num1[res] > 0) res++;
152         ask[ask[i].id].ans = res;
153     }
154     for(int i = 1; i <= idq; i++) {
155         printf("%d
", ask[i].ans);
156     }
157     return 0;
158 }

 

以上是关于Machine Learning(CF940F+待修改莫队)的主要内容,如果未能解决你的问题,请参考以下文章

CF940F Machine Learning

[CF940F]Machine Learning

题解CF940F Machine Learning

CF940F Machine Learning(带修莫队)

F. Machine Learning (带修改莫队)

Codeforces Round #466 (Div. 2) F - Machine Learning