답안 #62790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
62790 2018-07-30T04:19:39 Z Benq 새 집 (APIO18_new_home) C++11
0 / 100
5000 ms 358524 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 300001;

template<int SZ> struct seg {
    multiset<int> seg[2*SZ];
    void trans(multiset<int>& m, int x, int d) {
        if (d > 0) m.insert(x);
        else m.erase(m.find(x));
    }
    void upd(int l, int r, int x, int d) {
        for (l += SZ, r += SZ+1;l<r; l /= 2, r /= 2) {
            if (l&1) trans(seg[l++],x,d);
            if (r&1) trans(seg[--r],x,d);
        }
    }
    int query(int x) {
        int ans = -MOD;
        for (x += SZ; x; x /= 2) if (sz(seg[x])) ans = max(ans,*seg[x].rbegin());
        return ans;
    }
};

seg<1<<20> S[2];
int n,k,q;
map<int,int> m;
multiset<int> tmp[MX];
vector<array<int,4>> ev;

void tri (int l, int r, int t) {
    if (abs(t) == 1) {
        m[l] = m[r] = 0;
        if (l != -MOD && r != MOD) m[(l+r+1)/2] = 0;
        //cout << "AA\n";
    } else {
        //cout << "BB\n";
        if (l == -MOD) {
            if (r == MOD) return;
            S[1].upd(0,m[r]-1,r,t);
        } else {
            // cout << "AH " << l << " " << m[l] << " " << sz(m)-1 << "\n";
            if (r == MOD) S[0].upd(m[l],sz(m)-1,-l,t);
            else {
                S[0].upd(m[l],m[(l+r+1)/2]-1,-l,t);
                S[1].upd(m[(l+r+1)/2],m[r]-1,r,t);
            }
        }
    }
}

int ok = 0;

void ad(int x, int y, int t) { // team, position
    // cout << "HA " << x << " " << y << " " << t << "\n";
    if (t > 0) {
        if (sz(tmp[x]) == 2) ok ++;
        tmp[x].insert(y);
    }
    
    auto it = tmp[x].find(y);
    tri(*prev(it),*it,t);
    tri(*it,*next(it),t);
    tri(*prev(it),*next(it),-t);
    
    if (t < 0) {
        tmp[x].erase(tmp[x].find(y));
        if (sz(tmp[x]) == 2) ok --;
    }
}

void init() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k >> q;
    FOR(i,1,k+1) tmp[i].insert(-MOD), tmp[i].insert(MOD);
    F0R(i,n) {
        int x,t,a,b; cin >> x >> t >> a >> b;
        ev.pb({a,1,t,x});
        ev.pb({b+1,-1,t,x});
    }
    sort(all(ev));
    for (auto a: ev) ad(a[2],a[3],a[1]);
}

int main() {
    init();
    
    int co = 0;
    for (auto& a: m) a.s = co++;
    
    if (sz(m) > (1<<20)) {
        exit(5);
    }
    
    for (auto& a: ev) a[1] *= 2;
    F0R(i,q) {
        int l,y; cin >> l >> y;
        ev.pb({y,4,0,l});
    }
    sort(all(ev));
    
    for (auto a: ev) {
        if (a[1] == 4) {
            int val = a[3]; a[3] = prev(m.ub(a[3]))->s;
            int x = S[0].query(a[3]);
            int y = S[1].query(a[3]);
            if (ok != k) cout << -1;
            else cout << max(x+val,y-val);
            cout << "\n";
        } else ad(a[2],a[3],a[1]);
    }
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 211352 KB Output is correct
2 Correct 207 ms 211644 KB Output is correct
3 Correct 196 ms 211644 KB Output is correct
4 Incorrect 182 ms 211644 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 211352 KB Output is correct
2 Correct 207 ms 211644 KB Output is correct
3 Correct 196 ms 211644 KB Output is correct
4 Incorrect 182 ms 211644 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5096 ms 358524 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5034 ms 358524 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 211352 KB Output is correct
2 Correct 207 ms 211644 KB Output is correct
3 Correct 196 ms 211644 KB Output is correct
4 Incorrect 182 ms 211644 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 211352 KB Output is correct
2 Correct 207 ms 211644 KB Output is correct
3 Correct 196 ms 211644 KB Output is correct
4 Incorrect 182 ms 211644 KB Output isn't correct
5 Halted 0 ms 0 KB -