莫队写出玄学了?在线求调
查看原帖
莫队写出玄学了?在线求调
389348
TuYaBoYu楼主2024/7/31 21:39

最开始交的代码如下,能过样例,测试点全红 (测试数据),敏锐观察,发现前 3 个数据比答案大 2 ,其他数据比答案大 1 ,百思不得其解,望大佬解惑:

#include <bits/stdc++.h>

#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
const int maxn = 150009;
int ans[maxn],c;
int g[500],arr[maxn],col[1000005];
int n,B,tot,m,qcnt,now;
template<typename T>void read(T& n)
{
    T f=1,k=0;char c=getchar();
    while(c>'9'||c<'0'){if(c=='-'){f=-1;}c=getchar();}
    while(c<='9'&&c>='0'){k=(k<<3)+(k<<1)+c-'0';c=getchar();}
    n=f*k;
}
  
template<typename T>void write(T x)
{
    if(x==0){putchar('0');return;}
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}

struct node {
	int l,r,id,t;
}q[maxn];

struct node2 {
	int pos,val;
}change[maxn];

bool cmp(node a,node b) {
	return (g[a.l]==g[b.l])?(((g[a.r]==g[b.r])?(a.t<b.t):(a.r<b.r))):(g[a.l]<g[b.l]);
}

void add(int x) {
	if(!col[x])
		++c;
	++col[x];
}

void del(int x) {
	--col[x];
	if(!col[x])
		--c;
}

signed main() {
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	B=pow(n,2.0/3.0);
	for(int i=1;i<=n;++i) {
		g[i]=(i-1)/B+1;
		cin>>arr[i];
	}
	char op;
	for(int i=1;i<=m;++i) {
		cin>>op;
		if(op=='Q') {
			++qcnt;
			cin>>q[qcnt].l>>q[qcnt].r;
			q[qcnt].id=qcnt;
			q[qcnt].t=now;
		}
		else {
			++now;
			cin>>change[now].pos>>change[now].val;
		}
	}
	sort(q+1,q+qcnt+1,cmp);
	int l=1,r=0,last=0;
	for(int i=1;i<=qcnt;++i) {
		while(l>q[i].l) add(arr[--l]);
		while(r<q[i].r) add(arr[++r]);
		while(l<q[i].l) del(arr[l++]);
		while(r>q[i].r) del(arr[r--]);
		while(last<q[i].t) {
			++last;
			if (change[last].pos>=l&&change[last].pos<=r) {
			    del(arr[change[last].pos]);
				add(change[last].val);
			}
	        swap(arr[change[last].pos], change[last].val);
		}
		while(last>q[i].t) {
			if (change[last].pos>=l&&change[last].pos<=r) {
		        del(arr[change[last].pos]);
				add(change[last].val);
		    }
			swap(arr[change[last].pos], change[last].val);
			--last;
		}
		ans[q[i].id]=c;
	}
	for(int i=1;i<=qcnt;++i)
		cout<<ans[i]<<'\n';
    return 0;
}
2024/7/31 21:39
加载中...