답안 #210085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210085 2020-03-16T14:20:04 Z ToMmyDong Two Antennas (JOI19_antennas) C++11
0 / 100
52 ms 17528 KB
#include <bits/stdc++.h>
using namespace std;
#define int ll
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,n) for(int i=1;i<=n;++i)
#define SZ(i) int(i.size())
#define eb emplace_back
#define ALL(i) i.begin(),i.end()
#define X first
#define Y second
#ifdef tmdd
#define IOS()
#define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
template<typename It> ostream& _printRng(ostream &os,It bg,It ed)
{
    os<<"{";
    for(It it=bg;it!=ed;it++) {
        os<<(it==bg?"":",")<<*it;
    }
    os<<"}";
    return os;
}
template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());}
template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#define pary(...)
#endif

const int MAXN = 100005;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n, q;
int h[MAXN], a[MAXN], b[MAXN];
int ans[MAXN];
vector<pii> qry[MAXN];

int seg[MAXN*4], mn[MAXN*4], mx[MAXN*4], tag[MAXN*4];

int get (int o) {
    return seg[o];
}

int getMx (int o) {
    return max(mx[o], tag[o]);
}

void pull (int o) {
    mn[o] = max(mn[o<<1], mn[o<<1|1]);
    seg[o] = max({seg[o], seg[o<<1], seg[o<<1|1]});
    seg[o] = max(seg[o], mx[o]+mn[o]);
}

void upd (int o) {
    seg[o] = max(seg[o], max(tag[o],mx[o]) + mn[o]);
}

void push (int o) {
    if (tag[o] != -INF) {
        tag[o<<1] = max(tag[o<<1], tag[o]);
        tag[o<<1|1] = max(tag[o<<1|1], tag[o]);
        upd(o<<1);
        upd(o<<1|1);

        mx[o] = max(mx[o], tag[o]);
        tag[o] = -INF;

        pull(o);
    }
}

void build () {
    REP (i, MAXN*4) {
        seg[i] = -INF;
        mn[i] = -INF;
        mx[i] = -INF;
        tag[i] = -INF;
    }
}

void segC (int x, int val, int o, int nL, int nR) {
    if (x >= nR || x < nL) {
        return;
    } else if (nL == x && x + 1 == nR) {
        mn[o] = val;
        mx[o] = -INF;
        tag[o] = -INF;
    } else {
        push(o);
        int nM = (nL + nR) >> 1;
        segC(x, val, o<<1, nL, nM);
        segC(x, val, o<<1|1, nM, nR);
        pull(o);
    }
}

void segM (int qL, int qR, int val, int o, int nL, int nR) {
    if (qL >= nR || qR <= nL || qL >= qR) {
        return;
    } else if (qL <= nL && nR <= qR) {
        tag[o] = max(tag[o], val);
        upd(o);
    } else {
        push(o);
        int nM = (nL + nR) >> 1;
        segM(qL, qR, val, o<<1, nL, nM);
        segM(qL, qR, val, o<<1|1, nM, nR);
        pull(o);
    }
}


int segQ (int qL, int qR, int o, int nL, int nR) {
    if (qL >= nR || qR <= nL || qL >= qR) {
        return -INF;
    } else if (qL <= nL && nR <= qR) {
        return get(o);
    } else {
        push(o);
        int nM = (nL + nR) >> 1;
        return max(segQ(qL, qR, o<<1, nL, nM),\
            segQ(qL, qR, o<<1|1, nM, nR));
    }
}


vector<pii> opr[MAXN];
void solve () {
    build();
    for (int i=1; i<=n; i++) {
        opr[i].clear();
    }

    for (int i=1; i<=n; i++) {
        if (i+a[i]<=n) {
            opr[i+a[i]].eb(i, 1);
        }
        if (i+b[i]+1<=n) {
            opr[i+b[i]+1].eb(i, -1);
        }
    }

    for (int i=1; i<=n; i++) {
        debug(i);
        for (auto v : opr[i]) {
            if (v.Y > 0) {
                debug("ADD", v.X, -h[v.X]);
                segC(v.X, -h[v.X], 1, 1, n+1);
            } else {
                debug("RMV", v.X, -h[v.X]);
                segC(v.X, -INF, 1,1,n+1);
            }
        }

        int L = i - b[i];
        int R = i - a[i];

        debug(L, R, h[i]);
        segM(L, R+1, h[i], 1, 1, n+1);
        for (auto p : qry[i]) {
            int res = segQ(p.X, i+1, 1, 1, n+1);
            debug(p.Y, p.X, i, res);
            ans[p.Y] = max(ans[p.Y], segQ(p.X, i+1, 1, 1, n+1));
        }
        debug(mn[1], mx[1], tag[1], seg[1]);

    }

}
/*********************GoodLuck***********************/
signed main () {
    IOS();

    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> h[i] >> a[i] >> b[i];
    }
    cin >> q;
    for (int i=0; i<q; i++) {
        int l, r;
        cin >> l >> r;
        qry[r].eb(l, i);
    }

    fill(ans,ans+q, -INF);
    solve();
    for (int i=1; i<=n; i++) {
        h[i] = -h[i];
    }
    solve();


    for (int i=0; i<q; i++) {
        cout << (ans[i] < 0 ? -1 : ans[i]) << endl;
    }
}
/*
2
1 1 1
2 1 1
1
1 2

*/

Compilation message

antennas.cpp: In function 'void solve()':
antennas.cpp:169:17: warning: unused variable 'res' [-Wunused-variable]
             int res = segQ(p.X, i+1, 1, 1, n+1);
                 ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 17528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 17528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 52 ms 14712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 17528 KB Output isn't correct
2 Halted 0 ms 0 KB -