QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#194757#3857. Single-track railwayrealcomplex0#RE 0ms0kbC++142.5kb2023-09-30 22:11:592023-09-30 22:12:00

Judging History

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

  • [2023-09-30 22:12:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-09-30 22:11:59]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)2e5 + 10;
ll d[N];

struct Node{
    ll d1;
    ll d2;
    ll lazy1;
    ll lazy2;
};

Node T[N * 4];

void push(int node, int cl, int cr){
    T[node].d1 += T[node].lazy1;
    T[node].d2 += T[node].lazy2;
    if(cl != cr){
        T[node * 2].lazy1 += T[node].lazy1;
        T[node * 2 + 1].lazy1 += T[node].lazy1;
        T[node * 2].lazy2 += T[node].lazy2;
        T[node * 2 + 1].lazy2 += T[node].lazy2;
    }
    T[node].lazy1 = T[node].lazy2 = 0;
}

void update(int node, int cl, int cr, int tl, int tr, int typ, ll w){
    push(node, cl, cr);
    if(cr < tl || cl > tr) return;
    if(cl >= tl && cr <= tr){
        if(typ == 0) T[node].lazy1 = w;
        else T[node].lazy2 = w;
        push(node, cl, cr);
        return;
    }
    int mid = (cl + cr) / 2;
    update(node * 2, cl, mid, tl, tr, typ, w);
    update(node * 2 + 1, mid + 1, cr, tl, tr, typ, w);
}

pii get(int node, int cl, int cr, int id){
    push(node, cl, cr);
    if(cl == cr){
        return mp(T[node].d1, T[node].d2);
    }
    int mid = (cl + cr) / 2;
    if(mid >= id)
        return get(node * 2, cl, mid, id);
    else
        return get(node * 2 + 1, mid + 1, cr, id);
}

int n;

ll diff(ll x){
    return max(x,-x);
}

ll calc(){
    int l = 1;
    int r = n - 1;
    while(l < r){
        int mid = (l + r) / 2;
        pii ff = get(1, 1, n, mid + 1);
        if(ff.fi >= ff.se) r = mid;
        else l = mid + 1;
    }
    // l, l + 1
    pii A = get(1, 1, n, l);
    pii B = get(1, 1, n, l + 1);
    if(A.fi == A.se || B.fi == B.se) return 0ll;
    return diff(A.fi - B.se - d[l]);
}

int main(){
    fastIO;
    freopen("in.txt", "r", stdin);
    cin >> n;
    for(int i = 1; i <= n - 1; i ++ ){
        cin >> d[i];
        update(1, 1, n, i + 1, n, 0, d[i]);
        update(1, 1, n, 1, i, 1, d[i]);
    }
    cout << calc() << "\n";
    int q;
    cin >> q;
    int id;
    ll dd;
    for(int iq = 1; iq <= q; iq ++ ){
        cin >> id >> dd;
        update(1, 1, n, id + 1, n, 0, -d[id]);
        update(1, 1, n, 1, id, 1, -d[id]);
        d[id] = dd;
        update(1, 1, n, id + 1, n, 0, d[id]);
        update(1, 1, n, 1, id, 1, d[id]);
        cout << calc() << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
39
7
1 20
1 70
1 56
1 37
1 37
1 37
1 91

output:


result: