跳到正文

题解

题解:P12608 骷髅打金服

tyt0701约 16 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 16 分钟
题目
P12608

版本

  1. v2当前
  2. v1

我真服了这个把时间调成1秒的入,真的是要卡了吗?Undead2008 我真服了 啊啊啊啊啊啊啊啊啊

代码
     代码↓如下
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace io
{
	const int size = (1 << 20) + 1;
	char buf[size], *pp1 = buf, *pp2 = buf;
	char buffer[size];
	int op1 = -1;
	const int op2 = size - 1;
	struct FastIO
	{
		inline char readchar()
		{
			return pp1 != pp2 ? *pp1++ : pp1 == (pp2 = (pp1 = buf) + fread(buf, 1, size - 1, stdin)) ? EOF
																							   : *pp1++;
		}
// #ifndef ONLINE_JUDGE
// #define readchar getchar
// #endif
		inline char rc()
		{
			char c=readchar();
			while(c<32 || c==127) c=readchar();
			return c;
		}
		inline void flush()
		{
			fwrite(buffer, 1, op1 + 1, stdout), op1 = -1;
		}
		inline void writechar(const char &x)
		{
			if (op1 == op2)
				flush();
			buffer[++op1] = x;
		}
		inline int readint()
		{
			int s = 1, x = 0;
			char c = readchar();
			while (c <= 32)
			{
				c = readchar();
			}
			if (c == '-')
			{
				s = -1, c = readchar();
			}
			while ('0' <= c && c <= '9')
			{
				x = x * 10 + c - '0';
				c = readchar();
			}
			return x * s;
		}
		inline int readuint()
		{
			int x = 0;
			char c = readchar();
			while (c <= 32)
			{
				c = readchar();
			}
			while ('0' <= c && c <= '9')
			{
				x = x * 10 + c - '0';
				c = readchar();
			}
			return x;
		}
		inline void writeint(int x)
		{
			if (x < 0)
			{
				writechar('-'), x = -x;
			}
			char s[24];
			int n = 0;
			while (x || !n)
			{
				s[n++] = '0' + x % 10, x /= 10;
			}
			while (n--)
			{
				writechar(s[n]);
			}
		}
		inline void writeuint(unsigned x)
		{
			char s[24];
			int n = 0;
			while (x || !n)
			{
				s[n++] = '0' + x % 10, x /= 10;
			}
			while (n--)
			{
				writechar(s[n]);
			}
		}
		inline long long readlonglong()
		{
			long long x = 0, s = 1;
			char c = readchar();
			while (c <= 32)
			{
				c = readchar();
			}
			if (c == '-')
			{
				s = -1, c = readchar();
			}
			while ('0' <= c && c <= '9')
			{
				x = x * 10 + c - '0';
				c = readchar();
			}
			return x * s;
		}
		inline unsigned long long readulonglong()
		{
			unsigned long long x = 0;
			char c = readchar();
			while (c <= 32)
			{
				c = readchar();
			}
			while ('0' <= c && c <= '9')
			{
				x = x * 10 + c - '0';
				c = readchar();
			}
			return x;
		}
		inline void writelonglong(long long x)
		{
			if (x < 0)
			{
				writechar('-'), x = -x;
			}
			char s[25];
			int n = 0;
			while (x || !n)
			{
				s[n++] = '0' + x % 10, x /= 10;
			}
			while (n--)
			{
				writechar(s[n]);
			}
		}
		inline void writeulonglong(unsigned long long x)
		{
			char s[25];
			int n = 0;
			while (x || !n)
			{
				s[n++] = '0' + x % 10, x /= 10;
			}
			while (n--)
			{
				writechar(s[n]);
			}
		}
		inline int readstring(char *s)
		{
			int cnt = 0;
			char c = readchar();
			while (c <= 32)
			{
				c = readchar();
			}
			while (c > 32)
			{
				*s++ = c, ++cnt;
				c = readchar();
			}
			return *s = 0, cnt;
		}
		inline void writestring(const char *s)
		{
			while (*s)
			{
				writechar(*s++);
			}
		}
		inline double readdouble()
		{
			int zheng = 0, fuhao = 1;
			double xiao = 0, chushu = 10;
			char c;
			while ((c = readchar()) < '0' || c > '9')
			{
				if (c == '-')
				{
					fuhao = -1;
				}
			}
			while (c >= '0' && c <= '9')
			{
				zheng = zheng * 10 + c - '0';
				c = readchar();
			}
			if (c != '.')
			{
				return fuhao * zheng;
			}
			while ((c = readchar()) >= '0' && c <= '9')
			{
				xiao += (c - '0') / chushu;
				chushu *= 10;
			}
			return fuhao * (zheng + xiao);
		}
	} io;
#define readc io::io.rc
#define writec io::io.writechar
#define read io::io.readint
#define readu io::io.readuint
#define write io::io.writeint
#define writeu io::io.writeuint
#define readl io::io.readlonglong
#define readlu io::io.readulonglong
#define writel io::io.writelonglong
#define writelu io::io.writeulonglong
#define reads io::io.readstring
#define writes io::io.writestring
#define readd io::io.readdouble
#define flush io::io.flush
} using namespace io;
mt19937_64 rnd(0);
int allcnt1=0,allcnt2=0;
const int len=18;
struct Hash_table1
{
	int tot=0,head[1<<len],ans[1000010],nxt[1000010];
	int var[1000010];
	int ttot=0,cl[1000010];
	void insert(int x,int y)
	{
		++allcnt1;
		int wz=x&((1<<len)-1);
		for(int i=head[wz]; i!=0; i=nxt[i])
		{
			if(var[i]==x) return ans[i]+=y,void();
		}
		var[++tot]=x,ans[tot]=y,nxt[tot]=head[wz],head[wz]=tot;
		cl[++ttot]=wz;
	}
	int get_ans(int x)
	{
		++allcnt2;
		int wz=x&((1<<len)-1);
		for(int i=head[wz]; i!=0; i=nxt[i])
		{
			if(var[i]==x) return ans[i];
		}
		return 0;
	}
	void clear()
	{
		for(int i=1; i<=ttot; ++i) head[cl[i]]=0;
		tot=ttot=0;
	}
} hsh1;
int n,a[1000010];
int qz[1000010];
int cnt[1000010];
int fir[1000010];
int p1[1000010],q1[1000010],p3[1000010];
int p2[1000010],q2[1000010],p4[1000010],q4[1000010];
int ans=0;
int b[1000010],vis[1000010];
int var[1000010]; int tot,head[1000010],nxt[1000010];
void solve1(int l,int r)
{
	int tot=0;
	for(int i=l; i<=r; ++i)
	{
		if(vis[a[i]]==0) vis[a[i]]=++tot;
		b[i]=vis[a[i]];
	}
	for(int i=l; i<=r; ++i) vis[a[i]]=0;
	int cnt[110];
	for(int i=l; i<=r; ++i)
	{
		int ax=0,ccnt=0;
		memset(cnt,0,sizeof(cnt));
		for(int j=i; j<=r; ++j)
		{
			ax=max(ax,++cnt[b[j]]),ccnt+=(cnt[b[j]]==1);
			ans+=(ax*ccnt==j-i+1);
		}
	}
}
void solve2(int l,int r)
{
	int mid=l+r>>1;
	int tot=0;
	for(int i=l; i<=r; ++i)
	{
		if(vis[a[i]]==0) vis[a[i]]=++tot;
		b[i]=vis[a[i]];
	}
	for(int i=l; i<=r; ++i) vis[a[i]]=0;
	int sum1=0,sum2=0,sum3=0,sum4=0,ax=0,div=0;
	for(int i=mid+1; i<=r; ++i)
	{
		sum1+=qz[b[i]],sum4-=qz[b[i]];
		if(++cnt[b[i]]==1) fir[b[i]]=i,sum2+=qz[b[i]],sum4+=qz[b[i]]*ax;
		while(sum1>(div+1)*sum2) ++div;
		while(sum1<=div*sum2) --div;
		sum3+=qz[b[i]];
		if(cnt[b[i]]==ax) sum3-=qz[b[i]]*cnt[b[i]];
		else if(cnt[b[i]]==ax+1) ax=cnt[b[i]],sum3=sum1-qz[b[i]]*cnt[b[i]],sum4+=sum2;
		p1[i]=sum2^((div+1)*sum2-sum1),p2[i]=sum1;
		p3[i]=ax^sum3,p4[i]=sum4;
	}
	for(int i=mid+1; i<=r; ++i) --cnt[b[i]];
	sum1=sum2=sum4=ax=div=0;
	int nowr=r,axwz=0;
	tot=0;
	for(int i=mid; i>=l; --i)
	{
		sum1+=qz[b[i]];
		if(++cnt[b[i]]==1) sum2+=qz[b[i]],sum4+=qz[b[i]]*ax;
		while(sum1>=(div+1)*sum2) ++div;
		while(sum1<div*sum2) --div;
		sum4-=qz[b[i]];
		if(cnt[b[i]]==ax && fir[b[i]]!=0) nowr=min(nowr,fir[b[i]]-1);
		else if(cnt[b[i]]==ax+1)
		{
			ax=cnt[b[i]];
			sum4+=sum2;
			nowr=(fir[b[i]]==0?r:fir[b[i]]-1);
		}
		q1[i]=sum2^(sum1-div*sum2),q2[i]=sum4;
		if(nowr!=mid)
		{
			var[++tot]=ax^sum4,nxt[tot]=head[nowr],head[nowr]=tot;
			axwz=max(axwz,nowr);
		}
		q4[i]=sum1;
	}
	for(int i=mid; i>=l; --i) --cnt[b[i]];
	hsh1.clear();
	for(int i=mid+1; i<=axwz; ++i)
	{
		hsh1.insert(p3[i],1);
		for(int j=head[i]; j!=0; j=nxt[j]) ans+=hsh1.get_ans(var[j]);
	}
	for(int i=mid+1; i<=r; ++i) fir[b[i]]=head[i]=0;
	// return;
	hsh1.clear();
	for(int i=mid+1; i<=r; ++i) hsh1.insert(p1[i],1);
	for(int i=l; i<=mid; ++i) ans+=hsh1.get_ans(q1[i]);
	hsh1.clear();
	for(int i=mid+1; i<=r; ++i) hsh1.insert(p2[i],1);
	for(int i=l; i<=mid; ++i) ans+=hsh1.get_ans(q2[i]);
	hsh1.clear();
	for(int i=mid+1; i<=r; ++i) hsh1.insert(p4[i],1);
	for(int i=l; i<=mid; ++i) ans+=hsh1.get_ans(q4[i]);
}
int l,r; pair<int,int> q[1000010];
signed main()
{
	int t=read(); while(t--)
	{
		// n=4e5;
		// for(int i=1; i<=n; ++i) a[i]=rnd()%n+1;
		n=read();
		for(int i=1; i<=n; ++i) a[i]=read();
		for(int i=1; i<=n; ++i) qz[i]=rnd()%(int)9e12;
		ans=0;
		l=1,q[r=1]={1,n};
		while(l<=r)
		{
			pair<int,int> frn=q[l++];
			if(frn.second-frn.first+1<=100) solve1(frn.first,frn.second);
			else
			{
				solve2(frn.first,frn.second);
				int mid=frn.first+frn.second>>1;
				q[++r]={frn.first,mid},q[++r]={mid+1,frn.second};
			}
		}
		write(ans),writec('\n');
	}
	// cout<<allcnt1<<' '<<allcnt2<<'\n';
	return flush(),0;
}
原文