QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211549#4619. Fast Bubble Sortyiyiyi#AC ✓167ms16196kbC++141.7kb2023-10-12 18:13:412023-10-12 18:13:42

Judging History

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

  • [2023-10-12 18:13:42]
  • 评测
  • 测评结果:AC
  • 用时:167ms
  • 内存:16196kb
  • [2023-10-12 18:13:41]
  • 提交

answer

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<set> 
#define ll long long
#define lowbit(x) x&(-x)
#define mp make_pair
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define forE(i,x) for(int i=head[x];i;i=nxt[i])
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int maxn=2e5+5;
const int mod=998244353;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+(c-'0');
		c=getchar();
	}
	return x*f;
}
int T;
int a[maxn];
pii q[maxn];
vector<int> e[maxn];
int st[maxn],top;
int now[maxn],tp,ans[maxn],las[maxn];
signed main()
{
	T=read();
	while(T--)
	{
		int n=read(),m=read();
		top=0;tp=0;
		rep(i,1,n) a[i]=read(),e[i].clear();
		las[n]=n;
		per(i,n-1,1) 
		{
			if(a[i]>a[i+1]) las[i]=i;
			else las[i]=las[i+1];
		}
		rep(i,1,m) {int x=read(),y=read();q[i]=mp(x,y);}
		rep(i,1,m) e[q[i].fi].push_back(i);
		st[++top]=n+1;a[n+1]=1e9;
		now[++tp]=n+1;
		per(i,n,1)
		{
			while(top&&a[st[top]]<a[i]) --top;
			if(st[top]!=i+1) 
			{
				while(tp&&now[tp]<st[top]) --tp;
				if(now[tp]!=st[top]) now[++tp]=st[top];	
			}
			st[++top]=i;
			for(auto j:e[i])
			{
				int R=q[j].second;
				int l=1,r=tp,res=tp+1;
				while(l<=r)
				{
					int mid=(l+r)/2;
					if(now[mid]<=R) r=mid-1,res=mid;
					else l=mid+1; 
				}
				ans[j]=tp-res+1;
				int p=i;if(res<=tp) p=now[res];
				if(las[p]<R) ans[j]++;
			}
		}				
		rep(i,1,m) printf("%lld\n",ans[i]);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 167ms
memory: 16196kb

input:

10
100000 100000
94437 49936 62522 1166 33452 90618 91436 81173 99242 37280 46125 44869 52811 45694 11550 29857 48941 56538 35057 40907 79925 70785 29658 19044 32804 11428 4127 92197 12680 61636 27200 56790 83389 80340 54411 15308 38952 78085 81397 37554 44186 33581 35662 73331 84991 32044 69012 822...

output:

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

result:

ok 1000000 lines