小问题
查看原帖
小问题
480934
xqqQwQ_楼主2022/8/31 13:35

请各位大佬看一下我这个做法的复杂度是不是假了。

一直是TLE 60

如果真的假了能否说一下是哪里慢了

谢谢各位大佬

#include<bits/stdc++.h>
#define mid ((l+r)>>1)

using namespace std;

const int MAXN=5e5+5;

struct seg{
	int ls,rs,s;
};

deque<int> que[MAXN*2];
int rt[MAXN*3],cnt=0;
seg t[MAXN*20];
int fz[MAXN];

int n,q;

namespace io {
	const int __SIZE = (1 << 21) + 1;
	char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
	#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
	inline void gc (char &x) { x = Gc(); }
	inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
	inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
	inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
		for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) *s = __c; *s = 0; }
	template <class I> inline bool gi (I &x) { _eof = 0;
		for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
		for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
	template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;

void pushup(int num){
	t[num].s=t[t[num].ls].s+t[t[num].rs].s;
	return;
}

int add(int &num,int l,int r,int x){
	if(!num) num=++cnt;
	if(l==r){
		t[num].s++;
		return num;
	}
	if(x<=mid) add(t[num].ls,l,mid,x);
	else add(t[num].rs,mid+1,r,x);
	pushup(num);
	return num;
}

void update(int &num,int l,int r,int x,int k){
	if(!num) num=++cnt;
	if(l==r){
		t[num].s+=k;
		return;
	}
	if(x<=mid) update(t[num].ls,l,mid,x,k);
	else update(t[num].rs,mid+1,r,x,k);
	pushup(num);
	return;
}

int merge(int a,int b,int l,int r){
	if(!a) return b;
	if(!b) return a;
	if(l==r){
		t[a].s+=t[b].s;
		return a;
	} 
	t[a].ls=merge(t[a].ls,t[b].ls,l,mid);
	t[a].rs=merge(t[a].rs,t[b].rs,mid+1,r);
	pushup(a);
	return a; 
}

int query(int l,int r,int k,int t1){
	if(l==r) return l;
	int cnt1=0,cnt2=0;
	for(int i=1;i<=t1;i++){
		if(fz[i]!=0&&t[fz[i]].ls!=0) cnt1+=t[t[fz[i]].ls].s;
		if(fz[i]!=0&&t[fz[i]].rs!=0) cnt2+=t[t[fz[i]].rs].s;
	} 
//	cout<<l<<" "<<r<<" "<<cnt1<<" "<<cnt2<<" "<<k<<endl;  
	if(cnt1>k/2){
		for(int i=1;i<=t1;i++) fz[i]=t[fz[i]].ls;
		return query(l,mid,k,t1);
	}
	else if(cnt2>k/2){
		for(int i=1;i<=t1;i++) fz[i]=t[fz[i]].rs;
		return query(mid+1,r,k,t1);
	}
	else return -1;
}

int main(){
	gi(n),gi(q);
	for(int i=1;i<=n;i++){
		int k;
		gi(k);
		bool flag=0;
		while(k--){
			int a;
			gi(a);
			que[i].push_back(a);
			if(!flag) rt[i]=add(rt[i],1,n+q,a),flag=1;
			else update(rt[i],1,n+q,a,1);
		}
	}
	for(int i=1;i<=q;i++){
		int op;
		gi(op);
		if(op==1){
			int x,y;
			gi(x),gi(y);
			que[x].push_back(y);
			update(rt[x],1,n+q,y,1);
		}
		else if(op==2){
			int x;
			gi(x);
			update(rt[x],1,n+q,que[x].back(),-1);
			que[x].pop_back();
		}
		else if(op==3){
			int k;
			gi(k); 
			int tmp=0;
			for(int j=1;j<=k;j++){
				int x;
				gi(x);
				tmp+=que[x].size();
				fz[j]=rt[x];
			}
			cout<<query(1,n+q,tmp,k)<<endl; 
		}
		else{
			int x1,x2,x3;
			gi(x1),gi(x2),gi(x3);
			while(!que[x1].empty()) que[x3].push_back(que[x1].front()),que[x1].pop_front();
			while(!que[x2].empty()) que[x3].push_back(que[x2].front()),que[x2].pop_front();
			rt[x3]=merge(rt[x3],rt[x1],1,n+q);
			rt[x3]=merge(rt[x3],rt[x2],1,n+q);
		}
	}
} 
2022/8/31 13:35
加载中...