QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19341#1877. Matryoshka DollsJohnAlfnovWA 53ms15332kbC++203.6kb2022-01-29 13:34:242022-05-06 04:50:58

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 04:50:58]
  • 评测
  • 测评结果:WA
  • 用时:53ms
  • 内存:15332kb
  • [2022-01-29 13:34:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace file_read{
    namespace input_file_io{
        char ib[1<<26],*ip1=ib,*ip2=ib;
        inline char gc(){
            return (ip1==ip2&&(ip2=(ip1=ib)+fread(ib,1,1<<24,stdin)),
			ip1==ip2?EOF:*ip1++);
        }
        inline int read(){
            int x=0;
            char c=gc();
            while(c<'0'||c>'9'){
            	c=gc();
			}
            while(c>='0'&&c<='9'){
                x=(x<<3)+(x<<1)+(c^'0');
                c=gc();
            }
            return x;
        }
        inline auto read2(){
        	long long x=0;
            char c=gc();
            while(c<'0'||c>'9'){
            	c=gc();
			}
            while(c>='0'&&c<='9'){
                x=(x<<3)+(x<<1)+(c^'0');
                c=gc();
            }
            return x;
		}
    };
    namespace output_file_io{
        char ob[1<<26],*op=ob;
        inline void pc(char c){
            *op++=c;
        }
        void write(auto x){
            if(x<0){
                pc('-');
                x=-x;
            }
            if(x==0)pc('0');
            static int number_stack[40];
            int total_count=0;
            while(x)number_stack[++total_count]=x%10,x/=10;
            while(total_count){
                pc(number_stack[total_count]+'0');
                --total_count;
            }
        }
        inline void final_write(){
            fwrite(ob,op-ob,1,stdout);
        }
    };
    using namespace input_file_io;
    using namespace output_file_io;
};
using namespace file_read;
inline int Abs(int x){
	return x>0?x:-x;
}
int L[500005],R[500005];
int nxt[500005],pre[500005];
int a[500005],b[500005];
int bl[500005];
long long an[500005];
struct apple{
	int wz,l,r;
	bool operator<(const apple &other)const{
		if(bl[l]!=bl[other.l])return l<other.l;
		return r<other.r;
	}
}e[500005];
struct syz{
	int wz,zz,zx;
	syz(int wz=0,int zz=0,int zx=0):
		wz(wz),zz(zz),zx(zx){}
};
int n,m;
int aa[500005],bb[500005];
long long ans;
inline int calc(int x,int y){
	if(x!=0&&y!=0&&x!=n+1&&y!=n+1)return Abs(x-y);
	return 0;
}
stack<syz>vc;
void del(int x,int bh){
	if(bh==1){
		vc.emplace(syz(pre[x],1,x));
		vc.emplace(syz(nxt[x],0,x));
	}
	ans-=calc(b[x],b[pre[x]]);
	ans-=calc(b[x],b[nxt[x]]);
	ans+=calc(b[pre[x]],b[nxt[x]]);
	nxt[pre[x]]=nxt[x];
	pre[nxt[x]]=pre[x];
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i)a[i]=read(),b[a[i]]=i;
	b[0]=0,b[n+1]=n+1;
	for(int i=1;i<=n;++i)aa[i]=a[i];
	int S=sqrt(n+0.5);
	int SS=(n-1)/S+1;
	for(int i=1;i<SS;++i){
		L[i]=(i-1)*S+1,R[i]=i*S;
		for(int j=L[i];j<=R[i];++j)bl[j]=i;
	}
	L[SS]=(SS-1)*S+1,R[SS]=n;
	for(int j=L[SS];j<=R[SS];++j)bl[j]=SS;
	for(int i=1;i<=m;++i){
		e[i].wz=i;
		e[i].l=read();
		e[i].r=read();
	}
	sort(e+1,e+m+1);
	int ll=1,rr=0;
	for(int i=0;i<=n+1;++i){
		pre[i]=0;
		nxt[i]=n+1;
	}
	int wz=m;
	aa[0]=0,aa[n+1]=n+1;
	for(int i=SS;i>=1;--i){
		ll=L[i],rr=n;
		sort(aa+L[i],aa+R[i]+1);
		merge(aa+L[i],aa+R[i]+1,aa+R[i]+1,aa+n+1,bb+L[i]);
		for(int j=L[i];j<=n;++j)aa[j]=bb[j],bb[j]=0;
		int ss=aa[L[i]-1];
		aa[L[i]-1]=0;
		ans=0;
		for(int j=L[i];j<=n;++j){
			pre[aa[j]]=aa[j-1];
			ans+=calc(b[aa[j-1]],b[aa[j]]);
			nxt[aa[j]]=aa[j+1];
		}
		while(wz>=1&&bl[e[wz].l]==i){
			while(rr>e[wz].r)del(a[rr--],0);
			while(ll<e[wz].l)del(a[ll++],1);
			an[e[wz].wz]=ans;
			--wz;
			while(vc.size()){
				auto at=vc.top();
				if(at.zz==1)nxt[at.wz]=at.zx;
				else pre[at.wz]=at.zx;
				vc.pop();
			}
			ll=L[i];
		}
		aa[L[i]-1]=ss;
	}
	for(int i=1;i<=m;++i)write(an[i]),pc('\n');
	final_write();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 13832kb

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:

7
5
3
1
0

result:

ok 5 number(s): "7 5 3 1 0"

Test #2:

score: 0
Accepted
time: 4ms
memory: 13828kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 53ms
memory: 15332kb

input:

100000 1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...

output:

4999950000

result:

ok 1 number(s): "4999950000"

Test #4:

score: 0
Accepted
time: 5ms
memory: 13972kb

input:

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

output:

36

result:

ok 1 number(s): "36"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 13840kb

input:

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

output:

-12
4
34
9
20
3
-6
15
-6
-5

result:

wrong answer 1st numbers differ - expected: '7', found: '-12'