无线基佬求助
查看原帖
无线基佬求助
226485
柳苏明楼主2020/7/7 08:27

无限T和RE,不知道为什么,跪求大佬帮助QAQ

#include <cstdio>
#include <cctype>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#define R register int
#define DEBUG_ARRAY(Arr,Length) for(R i(1);i<=Length;i++) write(Arr[i]," \n"[i==Length]);
using std::vector;

namespace quick {
#define tp template<typename Type>
	namespace in {
		inline char getc() {
			static char buf[1<<21],*p1=buf,*p2=buf;
			return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
		}
		inline int read(char *s) {
			*s=getc();
			while(isspace(*s)) {*s=getc();if(*s==EOF) return 0;}
			while(!isspace(*s)&&*s!=EOF) {s++;*s=getc();}
			*s='\0'; return 1;
		}
		tp inline int read(Type &x) {
			x=0;bool k=false;char c=getc();
			while(!isdigit(c)) {k|=(c=='-');c=getc();if(c==EOF) return 0;}
			while(isdigit(c)) {x=(x<<1)+(x<<3)+(c^48);c=getc();}
			x*=(k?-1:1); return 1;
		}
		template <typename Type,typename... Args>
		inline int read(Type &t,Args &...args) {
			int res=0;
			res+=read(t);res+=read(args...);
			return res;
		}
	}
	using in::read;
	namespace out {
		char buf[1<<21];int p1=-1;const int p2=(1<<21)-1;
		inline void flush() {
			fwrite(buf,1,p1+1,stdout);
			p1=-1;
		}
		inline void putc(const char &c) {
			if(p1==p2) flush();
			buf[++p1]=c;
		}
		inline void write(char *s) {
			while(*s!='\0') putc(*s),s++;
		}
		tp inline void write(Type x) {
			static char buf[30];int p=-1;
			if(x<0) {putc('-');x=-x;}
			if(x==0) putc('0');
			else for(;x;x/=10) buf[++p]=x%10+48;
			for(;p!=-1;p--) putc(buf[p]);
		}
		inline void write(char c) {putc(c);}
		template <typename Type,typename... Args>
		inline void write(Type t,Args ...args) {
			write(t);write(args...);
		}
	}
	using out::write;
	using out::flush;
	tp inline Type max(const Type a,const Type b) {
		if(a<b) return b;
		return a;
	}
	tp inline Type min(const Type a,const Type b) {
		if(a<b) return a;
		return b;
	}
	tp inline void swap(Type &a,Type &b) {
		a^=b^=a^=b;
	}
	tp inline Type abs(const Type a) {
		return a>=0?a:-a;
	}
#undef tp
}
using namespace quick;

const int maxn=6e5+50,maxq=800;
int n,m,a[maxn],pos[maxn],cnt;
int f[maxq],g[maxq][maxq],belong[maxn],size,blocks;
vector<int> b,v[maxn];

int main(void) {
#ifndef ONLINE_JUDGE
	freopen("yunoIII.in","r",stdin);
#endif
	b.push_back(-1);
	read(n,m);size=sqrt(n);
	blocks=ceil((double)n/size);
	for(R i(1);i<=n;i++) {
		read(a[i]);
		b.push_back(a[i]);
	}
	std::sort(b.begin(),b.end());
	b.erase(std::unique(b.begin(),b.end()),b.end());
	cnt=b.size()-1;
	//DEBUG_ARRAY(a,n);
	//DEBUG_ARRAY(b,cnt);
	for(R i(1);i<=n;i++) {
		a[i]=std::lower_bound(b.begin(),b.end(),a[i])-b.begin();
		v[a[i]].push_back(i);
		pos[i]=v[a[i]].size()-1;
	}
	//DEBUG_ARRAY(a,n);
	for(R i(1);i<=blocks;i++) {
		static int times[maxn],mode;
		memset(times,0,sizeof times);mode=0;
		for(R j(size*(i-1)+1);j<=min(n,size*i);j++) {
			times[a[j]]++;belong[j]=i;
			mode=max(mode,times[a[j]]);
		}
		f[i]=g[i][i]=mode;
	}
	for(R i(1);i<=blocks;i++)
		for(R j(i+1);j<=blocks;j++)
			g[i][j]=max(g[i][j-1],f[j]);
	//write(size,' ' ,blocks,'\n');
	//for(R i(1);i<=blocks;i++) DEBUG_ARRAY(g[i],blocks);
	for(R i(1);i<=m;i++) {
		static int l,r,ans=0,times[maxn];
		read(l,r);l^=ans;r^=ans;ans=0;
		if(belong[l]==belong[r]) {
			memset(times,0,sizeof times);
			for(R j(l);j<=r;i++) {
				times[a[j]]++;
				ans=max(ans,times[a[j]]);
			}
		}
		else {
			ans=g[belong[l]+1][belong[r]];
			for(R j(l);j<=belong[l]*size;j++)
				while(pos[j]+ans<(int)v[a[j]].size()&&v[a[j]][pos[j]+ans]<=r) ans++;
			for(R j((belong[r]-1)*size+1);j<=r;j++)
				while(pos[j]-ans>=0&&v[a[j]][pos[j]-ans]>=l) ans++;
		}
		write(ans,'\n');
	}
	flush();
	return 0;
}

2020/7/7 08:27
加载中...