QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#89310#5669. Traveling in Jade Citykatoli#WA 2ms3464kbC++143.2kb2023-03-19 18:14:452023-03-19 18:14:46

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-19 18:14:46]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3464kb
  • [2023-03-19 18:14:45]
  • 提交

answer

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll=long long;

const int N=1e6+5;
int pre[2][N];
int vis[2][N];
int n,k,m,q;
struct LT{
	int tr[N];
	void add(int x,int v){
		for(;x<N;x+=-x&x)tr[x]+=v;
	}
	int qy(int x){
		int res=0;
		for(;x;x-=-x&x)res+=tr[x];
		return res;
	}
	int gao(int l,int r){
		return qy(r)-qy(l-1);
	}
}tr[2];
int check1(int u,int v){
	if(u==v)return 1;
	if(u>v)return tr[0].gao(1,n)==tr[0].gao(v,u-1);
	return tr[0].gao(u,v-1)==0;
}
int check2(int u,int v){
	if(u==v)return 1;
	auto [x,y]=minmax(u,v);
	return tr[1].gao(x,y-1)==0;
}
int gao1(int u,int v){
	if(u==v)return 0;
	if(u>v)return pre[0][n]-(pre[0][u-1]-pre[0][v-1]);
	return pre[0][v-1]-pre[0][u-1];
}
int gao2(int u,int v){
	if(u==v)return 0;
	auto [x,y]=minmax(u,v);
	return pre[1][y-1]-pre[1][x-1];
}
int solve(){
	int u,v;cin>>u>>v;
	if(u==v)return 0;
	if(u>v)swap(u,v);
	int ans=INT_MAX;
	if(v<=n){
		if(u>=k){
			if(check1(u,v))ans=min(ans,gao1(u,v));
			if(check1(v,u))ans=min(ans,gao1(v,u));
			if(check1(v,1)&&check1(k,u)&&check2(1,m+2))ans=min(ans,gao1(v,1)+gao1(k,u)+gao2(1,m+2));
		}else if(v<k){
			if(check1(u,v))ans=min(ans,gao1(u,v));
			if(check1(v,u))ans=min(ans,gao1(v,u));
			if(check1(1,u)&&check1(v,k)&&check2(1,m+2))ans=min(ans,gao1(1,u)+gao1(v,k)+gao2(1,m+2));
		}else{
			if(check1(u,v))ans=min(ans,gao1(u,v));
			if(check1(v,u))ans=min(ans,gao1(v,u));
			if(check1(v,1)&&check1(u,k)&&check2(1,m+2))ans=min(ans,gao1(v,1)+gao1(u,k)+gao2(1,m+2));
			if(check1(1,u)&&check1(k,v)&&check2(1,m+2))ans=min(ans,gao1(1,u)+gao1(k,v)+gao2(1,m+2));
		}
	}else if(u>n){
		u-=n-1,v-=n-1;
		if(check2(u,v))ans=min(ans,gao2(u,v));
		if(check2(u,1)&&check2(v,m+2)&&check1(1,k))ans=min(ans,gao2(u,1)+gao2(v,m+2)+gao1(1,k));
		if(check2(u,1)&&check2(v,m+2)&&check1(k,1))ans=min(ans,gao2(u,1)+gao2(v,m+2)+gao1(k,1));
	}else{
		v-=n-1;
		if(u>=k){
			if(check1(u,1)&&check2(1,v))ans=min(ans,gao1(u,1)+gao2(1,v));
			if(check1(k,u)&&check2(m+2,v))ans=min(ans,gao1(k,u)+gao2(m+2,v));
			if(check1(u,k)&&check2(m+2,v))ans=min(ans,gao1(u,k)+gao2(m+2,v));
			if(check1(1,u)&&check2(1,v))ans=min(ans,gao1(1,u)+gao2(1,v));
		}else{
			if(check1(u,1)&&check2(1,v))ans=min(ans,gao1(u,1)+gao2(1,v));
			if(check1(k,u)&&check2(m+2,v))ans=min(ans,gao1(k,u)+gao2(m+2,v));
			if(check1(u,k)&&check2(m+2,v))ans=min(ans,gao1(u,k)+gao2(m+2,v));
			if(check1(1,u)&&check2(1,v))ans=min(ans,gao1(1,u)+gao2(1,v));
		}
	}
	return ans;
}
signed main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k>>m>>q;
	for(int i=1;i<=n;++i)cin>>pre[0][i];
	for(int i=1;i<=n;++i)pre[0][i]+=pre[0][i-1];
	for(int i=1;i<=m+1;++i)cin>>pre[1][i];
	for(int i=1;i<=m+1;++i)pre[1][i]+=pre[1][i-1];
	while(q--){
		char op;cin>>op;
		if(op=='q'){
			int ans=solve();
			if(ans==INT_MAX)cout<<"impossiblen\n";
			else cout<<ans<<endl;
		}
		else if(op=='c'){
			int x;cin>>x;
			if(vis[0][x])vis[0][x]=0,tr[0].add(x,-1);
			else vis[0][x]=1,tr[0].add(x,1);
		}else{
			int x;cin>>x;++x;
			if(vis[1][x])vis[1][x]=0,tr[1].add(x,-1);
			else vis[1][x]=1,tr[1].add(x,1);
		}
	}
	return 0;
}
// init?
// var->0?
// infinite dfs?
// out of bound?
// max_element / min_element?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 3464kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossiblen
6

result:

wrong answer 4th lines differ - expected: 'impossible', found: 'impossiblen'