已过,但为何RE on 46
查看原帖
已过,但为何RE on 46
965685
yyrwlj楼主2024/11/6 12:58

RE on 46代码

#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
const int N = 200005;
struct Que{
    int id, l, r, t;
}q[N];
struct Upd{
    int pos, c;
}c[N];
int a[N], tmp[N], ans[N], sq;
int cnt[N], cnt2[N];
int n, m;
inline int get(int i)
{
    return i / sq;
}
bool cmp(Que A,Que B)
{
    int la = get(A.l), lb = get(B.l);
    int ra = get(A.r), rb = get(B.r);
    if (la != lb)
        return la < lb;
    if (ra != rb)
        return ra < rb;
    return A.t < B.t;
}
inline void add(int x)
{
	cnt2[cnt[x]] --;
	cnt[x] ++;
	cnt2[cnt[x]] ++;
}
inline void del(int x)
{
    cnt2[cnt[x]] --;
    cnt[x] --;
    cnt2[cnt[x]] ++;
}
inline int query()
{
	int res = 1;
	while (cnt2[res])
		res ++;
	return res;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]), tmp[i] = a[i];
    sq = ceil(pow(n, 2.0 / 3.0));
    int cq = 0, cc = 0, tot = n;
    for (int i=1;i<=m;i++)
    {
        int op, a, b;
        scanf("%d%d%d", &op, &a, &b);
        if (op == 1)
        {
            cq ++;
            q[cq] = {cq, a, b, cc};
        }
        else
            c[++cc] = {a, b}, tmp[++ tot] = b;
    }
    sort(tmp + 1, tmp + tot + 1);
    for (int i=1;i<=n;i++)
    	a[i] = lower_bound(tmp + 1, tmp + tot + 1, a[i]) - tmp;
    for (int i=1;i<=cc;i++)
    	c[i].c = lower_bound(tmp + 1, tmp + tot + 1, c[i].c) - tmp;
    sort(q + 1, q + cq + 1, cmp);
    for (int i=1,l=1,r=0,last=0;i<=cq;i++)
    {
        int L = q[i].l, R = q[i].r;
        while (r < R)
            add(a[++ r]);
        while (R < r)
            del(a[r --]);
        while (l < L)
            del(a[l ++]);
        while (L < l)
            add(a[-- l]);
        while (last < q[i].t)
        {
            last ++;
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
        }
        while (last > q[i].t)
        {
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
            last --;
        }
        ans[q[i].id] = query();
    }
    for (int i=1;i<=cq;i++)
        printf("%d\n", ans[i]);
    return 0;
}

AC代码

#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
const int N = 200005;
struct Que{
    int id, l, r, t;
}q[N];
struct Upd{
    int pos, c;
}c[N];
int a[N], tmp[N], ans[N], sq;
int cnt[N], cnt2[N];
int n, m;
inline int get(int i)
{
    return i / sq;
}
bool cmp(Que A,Que B)
{
    int la = get(A.l), lb = get(B.l);
    int ra = get(A.r), rb = get(B.r);
    if (la != lb)
        return la < lb;
    if (ra != rb)
        return ra < rb;
    return A.t < B.t;
}
inline void add(int x)
{
	if (cnt[x] >= 1)
		cnt2[cnt[x]] --;
	cnt[x] ++;
	if (cnt[x] >= 1)
		cnt2[cnt[x]] ++;
}
inline void del(int x)
{
	if (cnt[x] >= 1)
    	cnt2[cnt[x]] --;
    cnt[x] --;
    if (cnt[x] >= 1)
	    cnt2[cnt[x]] ++;
}
inline int query()
{
	int res = 1;
	while (cnt2[res])
		res ++;
	return res;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]), tmp[i] = a[i];
    sq = ceil(pow(n, 2.0 / 3.0));
    int cq = 0, cc = 0, tot = n;
    for (int i=1;i<=m;i++)
    {
        int op, a, b;
        scanf("%d%d%d", &op, &a, &b);
        if (op == 1)
        {
            cq ++;
            q[cq] = {cq, a, b, cc};
        }
        else
            c[++cc] = {a, b}, tmp[++ tot] = b;
    }
    sort(tmp + 1, tmp + tot + 1);
    for (int i=1;i<=n;i++)
    	a[i] = lower_bound(tmp + 1, tmp + tot + 1, a[i]) - tmp;
    for (int i=1;i<=cc;i++)
    	c[i].c = lower_bound(tmp + 1, tmp + tot + 1, c[i].c) - tmp;
    sort(q + 1, q + cq + 1, cmp);
    for (int i=1,l=1,r=0,last=0;i<=cq;i++)
    {
        int L = q[i].l, R = q[i].r;
        while (r < R)
            add(a[++ r]);
        while (R < r)
            del(a[r --]);
        while (l < L)
            del(a[l ++]);
        while (L < l)
            add(a[-- l]);
        while (last < q[i].t)
        {
            last ++;
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
        }
        while (last > q[i].t)
        {
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
            last --;
        }
        ans[q[i].id] = query();
    }
    for (int i=1;i<=cq;i++)
        printf("%d\n", ans[i]);
    return 0;
}
2024/11/6 12:58
加载中...