UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196740#2909. numberlyp12191001864ms70820kbC++112.7kb2023-10-30 21:00:502023-10-30 21:00:51

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int B=1e5,N=5e5+5;
int q;
ll a[N],b[N];
int f[4][73][100005];
inline ll read()
{
	char C=getchar();
	ll ANS=0,F=1;
	while (C<'0'||C>'9')
	{
		if (C=='-') F=-1;
		C=getchar();
	}
	while (C>='0'&&C<='9')
	{
		ANS=ANS*10+(C-'0');
		C=getchar();
	}
	return ANS*F;
}
int s[N];
inline int sum(ll x)
{
	ll u=x;
	if (x<N&&s[x]) return s[x];
	int res=0;
	while (x) res+=x%10,x/=10;
	if (u<N) return s[u]=res;
	return res;
}
void init()
{
	for (int i=0;i<4;i++)
	{
		int bas=pow(10,i+2);
		for (int j=0;j<=72;j++)
		{
			for (int k=bas-1;k>=0;k--)
			{
				int v=k+j+sum(k);
				if (v>=bas) f[i][j][k]=v%bas;
				else f[i][j][k]=f[i][j][v];
			}
		}
	}
}
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
vector<pii>t[N];

int fa[N],l[N],r[N],dis[N];
bool del[N];
pii v[N];
int find(int x)
{
	if (fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
int merge(int x,int y)
{
    if (!x||!y) return x+y;
    if (v[y]<v[x]) swap(x,y);
    r[x]=merge(r[x],y);
    if (dis[l[x]]<dis[r[x]]) swap(l[x],r[x]);
    dis[x]=dis[r[x]]+1;
    return x;
}
void erase(int x)
{
	del[x]=1;
	fa[l[x]]=fa[r[x]]=fa[x]=merge(l[x],r[x]);
    l[x]=r[x]=dis[x]=0;
}
int rt[N],nrt[N];
int MERGE(int x,int y)
{
	if (!x||!y) return x+y;
	x=find(x),y=find(y);
	fa[x]=fa[y]=merge(x,y);
	return fa[x];
}

int main()
{
	// freopen("number.in","r",stdin);
	// freopen("number.out","w",stdout);
	q=read();
	init();
	for (int i=1;i<=q;i++)
	{
		a[i]=read(),b[i]=read();
		if (a[i]/B==b[i]/B) continue;
		ll x=a[i]/B,y=a[i]%B;
		y=f[3][sum(x)][y],x++;
		a[i]=x*B+y;
		if (a[i]/B==b[i]/B) continue;
		t[x].push_back(mp((int)y,i));
		v[i].fi=b[i]/B,v[i].se=i;
	}
	dis[0]=-1;
	for (int i=1;i<=q;i++) fa[i]=i;
	//for (int i=1;i<=q;i++) cout << a[i] << endl;
	for (ll i=0;i<=99999;i++)
	{
		for (pii j:t[i])
		{
			rt[j.fi]=MERGE(rt[j.fi],j.se);
		}
		for (int j=0;j<100;j++) nrt[j]=0;
		for (int j=0;j<100;j++)
		{
			if (rt[j]==0) continue;
			while (1)
			{
				int x=find(rt[j]);
				if (!x) break;
				if (v[x].fi<=i)
				{
					a[v[x].se]=i*B+j;
					erase(x);
				}
				else break;
			}
			int x=find(rt[j]);
			int y=f[3][sum(i)][j];
			nrt[y]=MERGE(nrt[y],x);
		}
		for (int j=0;j<100;j++) rt[j]=nrt[j];
	}
	//for (int i=1;i<=q;i++) cout << a[i] << endl;
	for (int i=1;i<=q;i++)
	{
		for (int j=2;j>=0;j--)
		{
			int bas=pow(10,j+2);
			while (a[i]/bas!=b[i]/bas) 
			{
				a[i]=a[i]/bas*bas+bas+f[j][sum(a[i]/bas)][a[i]%bas];
			}
		}
		while (a[i]<=b[i]) a[i]+=sum(a[i]);
		printf("%lld\n",a[i]);
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 301ms
memory: 59548kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines

Test #2:

score: 0
Accepted
time: 490ms
memory: 59548kb

input:

500000
92 99927
119 99453
481 99268
29 99908
267 99547
835 99500
955 99099
734 99774
306 99883
729 9...

output:

99941
99454
99274
99941
99555
99520
99112
99775
99900
99657
99978
100010
99545
99245
99775
99907
997...

result:

ok 500000 lines

Subtask #2:

score: 25
Accepted

Test #3:

score: 25
Accepted
time: 366ms
memory: 65388kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines

Subtask #3:

score: 25
Accepted

Test #4:

score: 25
Accepted
time: 37ms
memory: 49872kb

input:

50
4587480273 4587480273
428862505 500400481
6920415626 7358620174
7787875953 7787884613
4542304779 ...

output:

4587480321
500400482
7358620210
7787884620
4542307848
4676070172
909798356
3555627285
9508855574
511...

result:

ok 50 lines

Subtask #4:

score: 30
Accepted

Test #5:

score: 30
Accepted
time: 670ms
memory: 70820kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines