QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#287767#5669. Traveling in Jade CityngonhatminWA 1ms3476kbC++206.2kb2023-12-20 23:02:342023-12-20 23:02:35

Judging History

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

  • [2023-12-20 23:02:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3476kb
  • [2023-12-20 23:02:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

//#define int long long
#define fi first
#define se second
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)

typedef pair<int,int> pii;

const int mod = 1e9 + 7;
const int oo = 1e9;
const int N = 2e6 + 2;

int n, m, k, q;
int c[N], w[N], bit1[N], bit2[N];
bool dd1[N], dd2[N];

void update1(int i, int ts){
    while(i <= n+1){
        bit1[i] += ts;
        i += (i & (-i));
    }
}

int query1(int i){
    int res = 0;
    while(i > 0){
        res += bit1[i];
        i -= (i & (-i));
    }
    return res;
}

void update2(int i, int ts){
    while(i <= m+1){
        bit2[i] += ts;
        i += (i & (-i));
    }
}

int query2(int i){
    int res = 0;
    while(i > 0){
        res += bit2[i];
        i -= (i & (-i));
    }
    return res;
}

int cal1(int x, int y){
    return c[max(x,y)] - c[min(x,y)];
}

int cal2(int x, int y){
    return w[max(x,y)] - w[min(x,y)];
}

/// result: kc tu x den y
/// cnt   : kc tu 1 den k
/// cnt1  : kc tu 1 den x
/// cnt2  : kc tu k den x
/// cnt3  : kc tu 1 den y
/// cnt4  : kc tu k den y

void solve1(int x, int y){
    int pos1, pos2, sum, all = query1(n+1), kk = query1(k);
    int result = oo, cnt = oo, cnt1 = oo, cnt2 = oo, cnt3 = oo, cnt4 = oo;

    pos1 = min(x,y); pos2 = max(x,y); sum = query1(pos2) - query1(pos1);
    if (sum == 0) result = min(result,cal1(x,y));
    if (all - sum == 0) result = min(result,c[n+1] - cal1(x,y));

    if (query2(m+1) == 0) cnt = min(cnt,w[m+1]);
    if (kk == 0) cnt = min(cnt,cal1(1,k));
    if (all - kk == 0) cnt = min(cnt,cal1(k,n+1));

    if (query1(x) == 0) cnt1 = min(cnt1,cal1(1,x));
    if (all - query1(x) == 0) cnt1 = min(cnt1,c[n+1] - cal1(1,x));

    pos1 = min(x,k); pos2 = max(x,k); sum = query1(pos2) - query1(pos1);
    if (sum == 0) cnt2 = min(cnt2,cal1(x,k));
    if (all - sum == 0) cnt2 = min(cnt2,c[n+1] - cal1(x,k));

    if (query1(y) == 0) cnt3 = min(cnt3,cal1(1,y));
    if (all - query1(y) == 0) cnt3 = min(cnt3,c[n+1] - cal1(1,y));

    pos1 = min(y,k); pos2 = max(y,k); sum = query1(pos2) - query1(pos1);
    if (sum == 0) cnt4 = min(cnt4,cal1(y,k));
    if (all - sum == 0) cnt4 = min(cnt4,c[n+1] - cal1(y,k));

    result = min({result,cnt1 + cnt + cnt4,cnt2 + cnt + cnt3});
    if (result == oo) cout << "impossible" << '\n';
    else cout << result << '\n';
}

void solve2(int x, int y){
    int pos1, pos2, sum, all = query1(n+1), kk = query1(k);
    int result = oo, cnt1 = oo, cnt2 = oo, cnt3 = oo, cnt4 = oo;

    /// cnt1  : kc tu 1 den x
    /// cnt2  : kc tu k den x
    /// cnt3  : kc tu 1 den y
    /// cnt4  : kc tu k den y

    if (query1(x) == 0) cnt1 = min(cnt1,cal1(1,x));
    if (all - query1(x) == 0) cnt1 = min(cnt1,c[n+1] - cal1(1,x));

    pos1 = min(x,k); pos2 = max(x,k); sum = query1(pos2) - query1(pos1);
    if (sum == 0) cnt2 = min(cnt2,cal1(x,k));
    if (all - sum == 0) cnt2 = min(cnt2,c[n+1] - cal1(x,k));

    int tmp1 = oo, tmp2 = oo;
    if (query2(y) == 0) tmp1 = cnt3 = min(tmp1,cal2(0,y));
    if (query2(m+1) - query2(y) == 0) tmp2 = cnt4 = min(tmp2,w[m+1] - cal2(0,y));

    if (kk == 0) cnt3 = min(cnt3,tmp2 + cal1(1,k)), cnt4 = min(cnt4,tmp1 + cal1(1,k));
    if (all - kk == 0) cnt3 = min(cnt3,tmp2 + c[n+1] - cal1(1,k)), cnt4 = min(cnt4,tmp2 + c[n+1] - cal1(1,k));

    result = min({result,cnt1 + cnt3,cnt2 + cnt4});
    if (result == oo) cout << "impossible" << '\n';
    else cout << result << '\n';
}

void solve3(int x, int y){
    int pos1, pos2, sum, tmp1, tmp2, all = query1(n+1), kk = query1(k);
    int result = oo, cnt1 = oo, cnt2 = oo, cnt3 = oo, cnt4 = oo;

    /// cnt1  : kc tu 1 den x
    /// cnt2  : kc tu k den x
    /// cnt3  : kc tu 1 den y
    /// cnt4  : kc tu k den y

    pos1 = min(x,y); pos2 = max(x,y);
    if (query2(y) - query2(x) == 0) result = min(result,cal2(x,y));

    tmp1 = oo, tmp2 = oo;
    if (query2(x) == 0) tmp1 = cnt1 = min(tmp1,cal2(0,x));
    if (query2(m+1) - query2(x) == 0) tmp2 = cnt2 = min(tmp2,w[m+1] - cal2(0,x));

    if (kk == 0) cnt1 = min(cnt1,tmp2 + cal1(1,k)), cnt2 = min(cnt2,tmp1 + cal1(1,k));
    if (all - kk == 0) cnt1 = min(cnt1,tmp2 + c[n+1] - cal1(1,k)), cnt2 = min(cnt2,tmp1 + c[n+1] - cal1(1,k));

    tmp1 = oo, tmp2 = oo;
    if (query2(y) == 0) tmp1 = cnt3 = min(tmp1,cal2(0,y));
    if (query2(m+1) - query2(y) == 0) tmp2 = cnt4 = min(tmp2,w[m+1] - cal2(0,y));

    if (kk == 0) cnt3 = min(cnt3,tmp2 + cal1(1,k)), cnt4 = min(cnt4,tmp1 + cal1(1,k));
    if (all - kk == 0) cnt3 = min(cnt3,tmp2 + c[n+1] - cal1(1,k)), cnt4 = min(cnt4,tmp2 + c[n+1] - cal1(1,k));

    result = min({result,cnt1 + cnt3,cnt2 + cnt4});

    if (result == oo) cout << "impossible" << '\n';
    else cout << result << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("input.inp","r")){
        freopen("input.inp","r",stdin);
        freopen("output.out","w",stdout);
    }
    if (fopen("TRAIN.inp","r")){
        freopen("TRAIN.inp","r",stdin);
        freopen("TRAIN.out","w",stdout);
    }

    cin >> n >> k >> m >> q;
    FOR(i,2,n+1) cin >> c[i];
    FOR(i,1,m+1) cin >> w[i];

    FOR(i,2,n+1) c[i] += c[i-1];
    FOR(i,1,m+1) w[i] += w[i-1];

    FOR(i,1,q){
        char type; int x, y;
        cin >> type;
        if (type == 'q'){
            cin >> x >> y;
            if (x > y) swap(x,y);
            if (1 <= x && x <= n){
                if (1 <= y && y <= n) solve1(x,y);
                else solve2(x,y-n);
            }
            else solve3(x-n,y-n);
        }
        if (type == 'c'){
            cin >> x; x++;
            if (!dd1[x]){
                dd1[x] = 1;
                update1(x,1);
            }
            else{
                dd1[x] = 0;
                update1(x,-1);
            }
        }
        if (type == 'x'){
            cin >> x; x++;
            if (!dd2[x]){
                dd2[x] = 1;
                update2(x,1);
            }
            else{
                dd2[x] = 0;
                update2(x,-1);
            }
        }
    }
}










Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3476kb

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
-1294967296
6

result:

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