QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#106294#6229. 排列xiaoyaowudi100 ✓214ms17460kbC++173.6kb2023-05-17 11:10:002023-05-17 11:10:01

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-17 11:10:01]
  • 评测
  • 测评结果:100
  • 用时:214ms
  • 内存:17460kb
  • [2023-05-17 11:10:00]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <numeric>
constexpr int N(200010),inf(1.01e9);
namespace T
{
	struct _z
	{
		int mn,mnc,tg;
	}z[N<<2];
	void add(int u,int v)
	{
		z[u].mn+=v;z[u].tg+=v;
	}
	void pd(int u)
	{
		if(z[u].tg) add(u<<1,z[u].tg),add(u<<1|1,z[u].tg),z[u].tg=0;
	}
	void pu(int u)
	{
		if(z[u<<1].mn<z[u<<1|1].mn) z[u].mn=z[u<<1].mn,z[u].mnc=z[u<<1].mnc;
		else if(z[u<<1].mn>z[u<<1|1].mn) z[u].mn=z[u<<1|1].mn,z[u].mnc=z[u<<1|1].mnc;
		else z[u].mn=z[u<<1].mn,z[u].mnc=z[u<<1].mnc+z[u<<1|1].mnc;
	}
	void build(int u,int cl,int cr)
	{
		if(cl==cr){z[u].mn=inf;z[u].mnc=1;return;}int mid((cl+cr)>>1);
		build(u<<1,cl,mid);build(u<<1|1,mid+1,cr);pu(u);
	}
	void upd(int u,int cl,int cr,int ql,int qr,int v)
	{
		if(cl>=ql && cr<=qr){add(u,v);return;}else if(cl!=cr) pd(u);int mid((cl+cr)>>1);
		if(ql<=mid) upd(u<<1,cl,mid,ql,qr,v);if(qr>mid) upd(u<<1|1,mid+1,cr,ql,qr,v);pu(u);
	}
}
long long slv(int a[],int n)
{
	T::build(1,1,n);static int smx[N],smn[N];int tmx(0),tmn(0);
	long long ans(0);
	for(int i(1);i<=n;++i)
	{
		while(tmn && a[smn[tmn]]>a[i])
		{
			T::upd(1,1,n,smn[tmn-1]+1,smn[tmn],a[smn[tmn]]);
			--tmn;
		}
		smn[++tmn]=i;
		T::upd(1,1,n,smn[tmn-1]+1,smn[tmn],-a[smn[tmn]]);
		while(tmx && a[smx[tmx]]<a[i])
		{
			T::upd(1,1,n,smx[tmx-1]+1,smx[tmx],-a[smx[tmx]]);
			--tmx;
		}
		smx[++tmx]=i;
		T::upd(1,1,n,smx[tmx-1]+1,smx[tmx],a[smx[tmx]]);
		T::upd(1,1,n,i,i,-inf+i);
		if(T::z[1].mn==i) ans+=T::z[1].mnc;
	}
	return ans;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n,k;std::cin>>n>>k;
	static int a[N],vs[N],b[N];for(int i(1);i<=k;++i) std::cin>>a[i],vs[i]=a[i];std::sort(vs+1,vs+k+1);
	for(int i(1);i<=k;++i) b[i]=std::lower_bound(vs+1,vs+k+1,a[i])-vs;vs[k+1]=n+1;
	if(!k) std::iota(a+1,a+n+1,1);
	else
	{
		int pos(k);
		static int ls[N],rs[N];int cnt(1);int bl(b[k]),br(b[k]);ls[1]=-b[k];rs[1]=b[k];
		// std::cerr<<vs[bl]<<" "<<vs[br]<<std::endl;
		for(int i(k-1);i;--i)
		{
			bl=std::min(bl,b[i]);br=std::max(br,b[i]);
			if(br-bl==k-i) ++cnt,ls[cnt]=-bl,rs[cnt]=br/*,std::cerr<<vs[bl]<<" "<<vs[br]<<std::endl*/;
		}
		struct _q
		{
			bool rev;
			int pos,st,ed,v;
		};
		static _q qs[N];int q(0);
		auto chk=[](int l,int r)->bool{return vs[r]-vs[l]==r-l;};
		for(int i(b[k]);i<=k;++i)
		{
			int l(std::lower_bound(rs+1,rs+cnt+1,i)-rs),r(std::upper_bound(rs+1,rs+cnt+1,i)-rs);
			if(l==r) qs[++q]=(_q){false,l-1,vs[i]+1,vs[i+1]-1,-i};
			else
			{
				int mx(0),mp(0);
				for(int j(l),t;j<r;j=t)
				{
					for(t=j;t<r && chk(-ls[t],-ls[j]);++t);
					int cur(t-j);
					if(j>l && chk(-ls[j],-ls[j-1]-1)) ++cur;
					if(cur>mx)
					{
						mx=cur;
						mp=t-1;
					}
				}
				qs[++q]=(_q){false,mp,vs[i]+1,vs[i+1]-1,mx};
			}
		}
		for(int i(b[k]);i;--i)
		{
			int l(std::lower_bound(ls+1,ls+cnt+1,-i)-ls),r(std::upper_bound(ls+1,ls+cnt+1,-i)-ls);
			if(l==r) qs[++q]=(_q){true,l-1,vs[i]-1,vs[i-1]+1,-N+i};
			else
			{
				int mx(0),mp(0);
				for(int j(l),t;j<r;j=t)
				{
					for(t=j;t<r && chk(rs[j],rs[t]);++t);
					int cur(t-j);
					if(j>l && chk(rs[j-1]+1,rs[j])) ++cur;
					if(cur>mx)
					{
						mx=cur;
						mp=t-1;
					}
				}
				qs[++q]=(_q){true,mp,vs[i]-1,vs[i-1]+1,mx};
			}
		}
		std::sort(qs+1,qs+q+1,[](auto a,auto b)->bool{if(a.pos!=b.pos) return a.pos<b.pos;return a.v>b.v;});
		for(int i(1);i<=q;++i)
		{
			if(qs[i].rev)
			{
				for(int j(qs[i].st);j>=qs[i].ed;--j) a[++pos]=j;
			}
			else
			{
				for(int j(qs[i].st);j<=qs[i].ed;++j) a[++pos]=j;
			}
		}
	}
	std::cout<<slv(a,n)<<"\n";
	for(int i(1);i<=n;++i) std::cout<<a[i]<<(" \n"[i==n]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 2ms
memory: 3372kb

input:

10 9
2 5 10 8 1 4 9 7 6

output:

12
2 5 10 8 1 4 9 7 6 3

result:

ok correct!

Test #2:

score: 0
Accepted
time: 2ms
memory: 3332kb

input:

10 6
4 3 2 1 5 7

output:

32
4 3 2 1 5 7 6 8 9 10

result:

ok correct!

Test #3:

score: 0
Accepted
time: 2ms
memory: 3480kb

input:

10 4
10 5 7 8

output:

30
10 5 7 8 9 6 4 3 2 1

result:

ok correct!

Test #4:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

9 3
2 3 4

output:

38
2 3 4 5 6 7 8 9 1

result:

ok correct!

Subtask #2:

score: 10
Accepted

Test #5:

score: 10
Accepted
time: 0ms
memory: 3360kb

input:

20 14
6 1 4 3 2 10 5 20 17 16 13 18 15 12

output:

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

result:

ok correct!

Test #6:

score: 0
Accepted
time: 2ms
memory: 3364kb

input:

20 5
19 16 8 7 10

output:

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

result:

ok correct!

Subtask #3:

score: 10
Accepted

Test #7:

score: 10
Accepted
time: 0ms
memory: 3372kb

input:

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

output:

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

result:

ok correct!

Test #8:

score: 0
Accepted
time: 1ms
memory: 3368kb

input:

22 14
3 18 12 11 7 1 8 4 14 9 22 21 16 5

output:

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

result:

ok correct!

Subtask #4:

score: 10
Accepted

Test #9:

score: 10
Accepted
time: 131ms
memory: 11196kb

input:

199899 1
79400

output:

10412404949
79400 79401 79402 79403 79404 79405 79406 79407 79408 79409 79410 79411 79412 79413 79414 79415 79416 79417 79418 79419 79420 79421 79422 79423 79424 79425 79426 79427 79428 79429 79430 79431 79432 79433 79434 79435 79436 79437 79438 79439 79440 79441 79442 79443 79444 79445 79446 79447 ...

result:

ok correct!

Subtask #5:

score: 10
Accepted

Test #10:

score: 10
Accepted
time: 194ms
memory: 15612kb

input:

191393 191393
32224 3810 113151 150094 136298 14214 128374 5493 40926 95388 13836 108772 80691 142140 98735 77447 47803 161246 117751 68781 68971 47977 96955 84464 105119 76918 59979 19121 179774 121202 185018 68697 26220 118151 188665 166116 184189 124353 23802 62039 22066 142966 171539 104019 1189...

output:

191398
32224 3810 113151 150094 136298 14214 128374 5493 40926 95388 13836 108772 80691 142140 98735 77447 47803 161246 117751 68781 68971 47977 96955 84464 105119 76918 59979 19121 179774 121202 185018 68697 26220 118151 188665 166116 184189 124353 23802 62039 22066 142966 171539 104019 118900 2254...

result:

ok correct!

Test #11:

score: 0
Accepted
time: 190ms
memory: 16036kb

input:

200000 200000
3 2 1 33 35 12 36 17 14 19 6 10 21 20 5 13 28 9 15 25 27 7 26 34 32 37 30 24 4 11 23 29 18 8 22 31 16 44 41 39 40 38 43 42 53 58 72 67 51 50 76 47 61 49 77 73 54 55 69 74 65 48 62 75 59 57 64 70 68 60 45 52 71 66 56 63 46 86 104 89 97 106 87 90 103 95 92 109 108 102 105 80 88 84 100 98...

output:

21330771
3 2 1 33 35 12 36 17 14 19 6 10 21 20 5 13 28 9 15 25 27 7 26 34 32 37 30 24 4 11 23 29 18 8 22 31 16 44 41 39 40 38 43 42 53 58 72 67 51 50 76 47 61 49 77 73 54 55 69 74 65 48 62 75 59 57 64 70 68 60 45 52 71 66 56 63 46 86 104 89 97 106 87 90 103 95 92 109 108 102 105 80 88 84 100 98 110 ...

result:

ok correct!

Subtask #6:

score: 10
Accepted

Test #12:

score: 10
Accepted
time: 208ms
memory: 15312kb

input:

185493 185493
164138 100544 43516 166466 20210 73092 6885 134912 135326 146721 166053 126249 173180 143077 7740 63990 108333 144740 79182 183947 95106 159595 97281 77456 17553 108730 7747 128355 70374 68823 126350 73005 89833 130930 104817 88271 37412 5136 92049 7100 100916 103685 27328 98227 130349...

output:

185496
164138 100544 43516 166466 20210 73092 6885 134912 135326 146721 166053 126249 173180 143077 7740 63990 108333 144740 79182 183947 95106 159595 97281 77456 17553 108730 7747 128355 70374 68823 126350 73005 89833 130930 104817 88271 37412 5136 92049 7100 100916 103685 27328 98227 130349 18526 ...

result:

ok correct!

Test #13:

score: 0
Accepted
time: 180ms
memory: 15520kb

input:

184857 184857
36 29 19 31 22 28 5 35 6 2 21 14 10 25 13 23 24 7 33 3 15 27 4 1 20 11 18 30 34 32 8 17 9 26 16 12 65 45 53 58 47 54 61 39 71 56 38 51 77 52 62 67 70 75 59 46 76 40 72 79 68 49 48 78 43 69 55 64 37 66 74 41 57 63 50 73 80 42 44 60 95 100 99 96 93 98 105 87 85 86 97 101 90 94 89 103 92 ...

output:

18143719
36 29 19 31 22 28 5 35 6 2 21 14 10 25 13 23 24 7 33 3 15 27 4 1 20 11 18 30 34 32 8 17 9 26 16 12 65 45 53 58 47 54 61 39 71 56 38 51 77 52 62 67 70 75 59 46 76 40 72 79 68 49 48 78 43 69 55 64 37 66 74 41 57 63 50 73 80 42 44 60 95 100 99 96 93 98 105 87 85 86 97 101 90 94 89 103 92 102 8...

result:

ok correct!

Subtask #7:

score: 10
Accepted

Test #14:

score: 10
Accepted
time: 82ms
memory: 8228kb

input:

94962 47652
17 10 11 21 5 12 20 1 13 19 66 114 38 79 83 49 108 104 112 94 44 24 63 105 67 85 70 100 73 39 72 91 96 71 99 28 88 95 106 55 42 65 110 60 97 35 81 76 54 47 141 130 172 189 137 121 171 183 180 196 166 147 133 194 175 165 193 144 119 146 205 122 145 118 149 126 206 199 138 195 207 129 148 ...

output:

147773
17 10 11 21 5 12 20 1 13 19 66 114 38 79 83 49 108 104 112 94 44 24 63 105 67 85 70 100 73 39 72 91 96 71 99 28 88 95 106 55 42 65 110 60 97 35 81 76 54 47 141 130 172 189 137 121 171 183 180 196 166 147 133 194 175 165 193 144 119 146 205 122 145 118 149 126 206 199 138 195 207 129 148 188 1...

result:

ok correct!

Subtask #8:

score: 10
Accepted

Test #15:

score: 10
Accepted
time: 190ms
memory: 12848kb

input:

200000 70130
3 6 13 15 5 76 59 104 89 42 60 73 51 115 46 105 26 31 45 116 35 108 111 88 41 50 91 32 48 69 20 114 87 29 117 53 74 96 19 94 34 55 18 72 100 170 147 121 132 169 122 171 160 128 129 164 123 124 168 138 120 153 139 142 173 141 133 148 126 125 174 175 190 198 183 197 178 192 185 176 193 21...

output:

451494
3 6 13 15 5 76 59 104 89 42 60 73 51 115 46 105 26 31 45 116 35 108 111 88 41 50 91 32 48 69 20 114 87 29 117 53 74 96 19 94 34 55 18 72 100 170 147 121 132 169 122 171 160 128 129 164 123 124 168 138 120 153 139 142 173 141 133 148 126 125 174 175 190 198 183 197 178 192 185 176 193 219 202 ...

result:

ok correct!

Subtask #9:

score: 10
Accepted

Test #16:

score: 10
Accepted
time: 194ms
memory: 13688kb

input:

200000 111432
12 35 21 17 9 43 25 36 47 38 10 42 28 13 32 24 3 46 26 48 49 5 7 34 27 50 37 1 40 53 54 52 68 107 82 101 99 74 79 88 104 96 67 100 94 80 56 66 105 91 102 61 55 106 57 86 87 70 76 75 65 71 89 58 112 110 150 111 132 153 137 141 152 114 122 125 113 146 121 118 115 133 171 205 199 226 176 ...

output:

284008
12 35 21 17 9 43 25 36 47 38 10 42 28 13 32 24 3 46 26 48 49 5 7 34 27 50 37 1 40 53 54 52 68 107 82 101 99 74 79 88 104 96 67 100 94 80 56 66 105 91 102 61 55 106 57 86 87 70 76 75 65 71 89 58 112 110 150 111 132 153 137 141 152 114 122 125 113 146 121 118 115 133 171 205 199 226 176 202 214...

result:

ok correct!

Test #17:

score: 0
Accepted
time: 190ms
memory: 14860kb

input:

195884 62064
5 195883 6 195880 195882 195878 195881 195876 8 195865 195866 195873 195859 195862 195868 195854 11 195851 10 18 195849 25 195839 195830 195836 195847 195840 30 28 195831 38 195828 32 195827 37 43 39 52 195819 195817 195811 53 195806 195799 195798 195797 195793 195779 56 195783 195794 5...

output:

592935
5 195883 6 195880 195882 195878 195881 195876 8 195865 195866 195873 195859 195862 195868 195854 11 195851 10 18 195849 25 195839 195830 195836 195847 195840 30 28 195831 38 195828 32 195827 37 43 39 52 195819 195817 195811 53 195806 195799 195798 195797 195793 195779 56 195783 195794 58 62 6...

result:

ok correct!

Subtask #10:

score: 10
Accepted

Test #18:

score: 10
Accepted
time: 199ms
memory: 15584kb

input:

200000 180419
41 16 42 49 52 27 6 22 7 24 12 20 23 30 13 1 40 38 11 35 5 43 19 46 18 29 39 4 17 31 44 10 47 25 15 9 36 50 53 37 34 33 32 48 55 14 26 8 45 2 104 93 82 102 57 91 98 81 79 84 65 69 97 83 64 76 101 105 96 89 72 78 85 86 74 92 90 58 103 67 75 100 70 99 87 61 60 107 88 80 62 59 71 95 106 7...

output:

228165
41 16 42 49 52 27 6 22 7 24 12 20 23 30 13 1 40 38 11 35 5 43 19 46 18 29 39 4 17 31 44 10 47 25 15 9 36 50 53 37 34 33 32 48 55 14 26 8 45 2 104 93 82 102 57 91 98 81 79 84 65 69 97 83 64 76 101 105 96 89 72 78 85 86 74 92 90 58 103 67 75 100 70 99 87 61 60 107 88 80 62 59 71 95 106 77 115 1...

result:

ok correct!

Test #19:

score: 0
Accepted
time: 214ms
memory: 17460kb

input:

196542 192884
4015 68810 47545 70278 196343 25868 16040 103198 150508 130222 192293 1148 88773 150912 49343 132154 140164 193876 122474 117922 173775 56014 184072 109551 52544 117341 77408 122145 55476 176029 2985 137183 77861 12484 108489 44578 134648 87137 74111 3074 81632 81673 191887 76403 51555...

output:

196638
4015 68810 47545 70278 196343 25868 16040 103198 150508 130222 192293 1148 88773 150912 49343 132154 140164 193876 122474 117922 173775 56014 184072 109551 52544 117341 77408 122145 55476 176029 2985 137183 77861 12484 108489 44578 134648 87137 74111 3074 81632 81673 191887 76403 51555 17946 ...

result:

ok correct!