QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103890 | #5669. Traveling in Jade City | Robert_JYH# | TL | 11ms | 51648kb | C++17 | 4.4kb | 2023-05-07 19:41:37 | 2023-05-07 19:41:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
string no="impossible";
int n,k,m,q;
const int maxn=2e6+10;
vector<pair<int,int>> ve[maxn];
const int inf =1e18;
bool ok[maxn];
struct kkk{
int x,len;
bool operator<(kkk k1)const{
return len>k1.len;
}
};
int t1[maxn],t2[maxn];
struct Treearray{
int c[1000010],n;
void set_n(int _n){
n=_n;
for(int i=1;i<=n;i++)c[i]=0;
}
int lowbit(int x){
return x&(-x);
}
void update(int i,int k){ //在i位置加上k
while(i <= n){
c[i] += k;
i += lowbit(i);
}
}
int getsum(int i){ //求A[1 - i]的和
int res = 0;
while(i > 0){
res += c[i];
i -= lowbit(i);
}
return res;
}
};
Treearray tri1,tri2;
int sum1[maxn],sum2[maxn];
int dis[maxn];
void addedge(int x,int y,int w){
// cout<<"x="<<x<<" y="<<y<<" w="<<w<<"\n";
ve[x].emplace_back(y,w);
ve[y].emplace_back(x,w);
}
signed main()
{
// ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>k>>m>>q;
for(int i=1;i<=n;i++){
int w;cin>>w;
sum1[i]=w;
sum1[i]+=sum1[i-1];
}
for(int i=1;i<=m+1;i++){
int w;cin>>w;
sum2[i]=w;
sum2[i]+=sum2[i-1];
}
for(int i=1;i<=n+m;i++)dis[i]=inf;
tri1.set_n(n);
tri2.set_n(m+1);
while(q--){
char c;cin>>c;
if(c=='q'){
vector<int> in;
int x,y;
cin>>x>>y;
in.push_back(1);
in.push_back(k);
in.push_back(x);
in.push_back(y);
if(x>y)swap(x,y);
function<void(int)> w1 = [&](int x){
if(x>1&&x<k){
if(tri1.getsum(x-1)==0){
addedge(1,x,sum1[x-1]);
}
if(tri1.getsum(k-1)-tri1.getsum(x-1)==0){
addedge(x,k,sum1[k-1]-sum1[x-1]);
}
}
};
w1(x);w1(y);
function<void(int)> w2 = [&](int x){
if(x>k&&x<=n){
if(tri1.getsum(x-1)-tri1.getsum(k-1)==0){
addedge(k,x,sum1[x-1]-sum1[k-1]);
}
if(tri1.getsum(n)-tri1.getsum(x-1)==0){
addedge(x,1,sum1[n]-sum1[x-1]);
}
}
};
w2(x);w2(y);
function<void(int)> w3 = [&](int x){
if(x>n&&x<=n+m){
if(tri2.getsum(x-n)==0){
addedge(1,x,sum2[x-n]);
}
if(tri2.getsum(m+1)-tri2.getsum(x-n)==0){
addedge(x,k,sum2[m+1]-sum2[x-n]);
}
}
};
w3(x);w3(y);
if(((x>1&&x<k&&y>1&&y<k)||(x>k&&x<=n&&y>k&&y<=n))&&tri1.getsum(y-1)-tri1.getsum(x-1)==0){
addedge(x,y,sum1[y-1]-sum1[x-1]);
}
if(x>n&&x<=n+m&&y>n&&y<=n+m&&tri2.getsum(y-n)-tri1.getsum(x-n)){
addedge(x,y,sum2[y-n]-sum2[x-n]);
}
if(tri1.getsum(k-1)==0)addedge(1,k,sum1[k-1]);
if(tri1.getsum(n)-tri1.getsum(k-1)==0)addedge(1,k,sum1[n]-sum1[k-1]);
if(tri2.getsum(m+1)==0)addedge(1,k,sum2[m+1]);
priority_queue<kkk,vector<kkk>> qu;
qu.push(kkk{x,0});
while(!qu.empty()){
kkk k1=qu.top();qu.pop();
// cout<<k1.x<<" "<<k1.len<<"\n";
if(dis[k1.x]>k1.len){
dis[k1.x]=k1.len;
}
else continue;
for(auto [to,len]:ve[k1.x])if(dis[to]==inf){
qu.push(kkk{to,len+k1.len});
}
}
if(dis[y]==inf)cout<<no<<"\n";
else cout<<dis[y]<<"\n";
for(auto it:in){
ve[it].clear();
dis[it]=inf;
}
}
if(c=='c'){
int id;cin>>id;
tri1.update(id,-t1[id]);
t1[id]=1-t1[id];
tri1.update(id,t1[id]);
}
if(c=='x'){
int id;cin>>id;
id++;
tri2.update(id,-t2[id]);
t2[id]=1-t2[id];
tri2.update(id,t2[id]);
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 8ms
memory: 51648kb
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 impossible 6
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 11ms
memory: 50408kb
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 impossible 6
result:
ok 5 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1000000 999999 1000000 1000000 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...
output:
177406400 122264600 70328400 impossible impossible impossible impossible impossible impossible impossible impossible impossible 29295200 impossible 22163200 impossible impossible impossible 11422200 impossible 62579800 impossible 35339400 impossible 20157200 impossible impossible impossible impossib...