답안 #66480

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
66480 2018-08-10T18:43:12 Z Benq 새 집 (APIO18_new_home) C++14
100 / 100
4053 ms 472956 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;

void checkmx(int& a, int b) { a = max(a,b); }
void checkmn(int& a, int b) { a = min(a,b); }

int getPos(vi& v, int x) { return ub(all(v),x)-v.begin()-1; }

template<int SZ> struct seg {
    vector<array<int,4>> todo; // query minimum, interval min operations
    vi num;
    int mn[SZ];
    
    void build(int ind, int L, int R) {
        mn[ind] = 2*MOD; 
        if (L == R) return;
        int M = (L+R)/2;
        build(2*ind,L,M); build(2*ind+1,M+1,R);
    }
    
    void build() {
        num.pb(-MOD);
        for (auto a: todo) num.pb(a[2]), num.pb(a[3]+1);
        sort(all(num)); num.erase(unique(all(num)),num.end());
        build(1,0,sz(num)-1);
    }
    
    void modify(int x, int y, int t, int ind, int L, int R) {
        if (R < x || y < L) return;
        if (x <= L && R <= y) { checkmn(mn[ind],t); return; }
        int M = (L+R)/2;
        modify(x,y,t,2*ind,L,M);
        modify(x,y,t,2*ind+1,M+1,R);
    }
    
    void modify(int x, int y, int t) {
        // cout << "OOPS " << x << " " << y << " " << t << " " << getPos(num,x) << " " << getPos(num,y) << "\n";
        modify(getPos(num,x),getPos(num,y),t,1,0,sz(num)-1);
    }
    
    int query(int x, int ind, int L, int R) {
        int ret = mn[ind];
        if (L != R) {
            int M = (L+R)/2;
            if (x <= M) checkmn(ret,query(x,2*ind,L,M));
            else checkmn(ret,query(x,2*ind+1,M+1,R));
        }
        return ret;
    }
    int query(int x) { 
        return query(getPos(num,x),1,0,sz(num)-1); 
    }
};

int n,k,q, ans[MX];

seg<1<<21> S[2];
map<pi,int> type[MX];
vector<array<int,5>> mod;
vector<array<int,3>> query;

ostream& operator <<(ostream& os, const pair<pi,int>& p) {
    os << p.f.f << " " << p.f.s << " " << p.s << " | ";
    return os;
}

void ins(int x, pair<pi,int> a, pair<pi,int> b) {
    // cout << x << " | " << a << b << "\n";
    if (a.f.f == -MOD && b.f.f == MOD) {
        S[0].todo.pb({MOD,-MOD,a.s,x});
        S[1].todo.pb({-MOD,MOD,a.s,x});
    } else {
        S[0].todo.pb({(a.f.f+b.f.f)/2,a.f.f,a.s,x}); // all x <= (a.f.f+b.f.f)/2
        S[1].todo.pb({(a.f.f+b.f.f)/2+1,b.f.f,a.s,x});
    }
}

pair<pi,int> getNex(auto& a, auto b) {
    b = next(b);
    if (b == a.end()) return {{MOD,-1},1};
    return *b;
}

void init() {
    ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> k >> q;
	F0R(i,n) {
		int x,t,a,b; cin >> x >> t >> a >> b;
		mod.pb({a,1,t,x,i});
		mod.pb({b+1,-1,t,x,i});
	}   
	FOR(i,1,k+1) type[i][{-MOD,-1}] = 1;
	sort(all(mod));
	for (auto a: mod) {
	    pi t = {a[3],a[4]};
	    if (a[1] == 1) {
	        type[a[2]][t] = 0;
	        auto it = type[a[2]].find(t);
	        ins(a[0]-1,*prev(it),getNex(type[a[2]],it));
	        prev(it)->s = it->s = a[0];
	    } else {
	        auto it = type[a[2]].find(t);
	        ins(a[0]-1,*prev(it),*it);
	        ins(a[0]-1,*it,getNex(type[a[2]],it));
	        prev(it)->s = a[0];
	        type[a[2]].erase(it);
	    }
	}
	FOR(i,1,k+1) ins(1e8,*type[i].begin(),getNex(type[i],type[i].begin()));
}

void solve0() {
	sort(query.rbegin(),query.rend());
	S[0].build();
    sort(all(S[0].todo));
    for (auto a: query) {
        while (sz(S[0].todo)) {
            auto x = S[0].todo.back();
            if (x[0] < a[0]) break;
            S[0].modify(x[2],x[3],x[1]);
            S[0].todo.pop_back();
        }
        checkmx(ans[a[2]],a[0]-S[0].query(a[1]));
    }
}

void solve1() {
    reverse(all(query));
    S[1].build();
    sort(all(S[1].todo)); reverse(all(S[1].todo));
    for (auto a: query) {
        while (sz(S[1].todo)) {
            auto x = S[1].todo.back();
            if (x[0] > a[0]) break;
            S[1].modify(x[2],x[3],-x[1]);
            S[1].todo.pop_back();
        }
        checkmx(ans[a[2]],-S[1].query(a[1])-a[0]);
    }
}

int main() {
    init();
	F0R(i,q) {
		int l,y; cin >> l >> y;
		query.pb({l,y,i});
	}
	solve0();
	solve1();
	F0R(i,q) {
	    if (ans[i] > (1e8)) cout << -1;
	    else cout << ans[i];
	    cout << "\n";
	}
}

/* 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 15 ms 14456 KB Output is correct
2 Correct 14 ms 14624 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Correct 15 ms 14624 KB Output is correct
5 Correct 20 ms 14760 KB Output is correct
6 Correct 16 ms 14760 KB Output is correct
7 Correct 18 ms 14760 KB Output is correct
8 Correct 16 ms 14916 KB Output is correct
9 Correct 18 ms 14916 KB Output is correct
10 Correct 19 ms 14916 KB Output is correct
11 Correct 18 ms 14944 KB Output is correct
12 Correct 18 ms 14948 KB Output is correct
13 Correct 16 ms 14992 KB Output is correct
14 Correct 15 ms 14992 KB Output is correct
15 Correct 18 ms 14992 KB Output is correct
16 Correct 17 ms 14992 KB Output is correct
17 Correct 17 ms 14992 KB Output is correct
18 Correct 16 ms 14992 KB Output is correct
19 Correct 18 ms 14992 KB Output is correct
20 Correct 16 ms 14992 KB Output is correct
21 Correct 16 ms 14992 KB Output is correct
22 Correct 17 ms 14992 KB Output is correct
23 Correct 17 ms 14992 KB Output is correct
24 Correct 17 ms 14992 KB Output is correct
25 Correct 17 ms 14992 KB Output is correct
26 Correct 17 ms 14992 KB Output is correct
27 Correct 17 ms 14992 KB Output is correct
28 Correct 17 ms 14992 KB Output is correct
29 Correct 18 ms 14992 KB Output is correct
30 Correct 14 ms 14992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14624 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Correct 15 ms 14624 KB Output is correct
5 Correct 20 ms 14760 KB Output is correct
6 Correct 16 ms 14760 KB Output is correct
7 Correct 18 ms 14760 KB Output is correct
8 Correct 16 ms 14916 KB Output is correct
9 Correct 18 ms 14916 KB Output is correct
10 Correct 19 ms 14916 KB Output is correct
11 Correct 18 ms 14944 KB Output is correct
12 Correct 18 ms 14948 KB Output is correct
13 Correct 16 ms 14992 KB Output is correct
14 Correct 15 ms 14992 KB Output is correct
15 Correct 18 ms 14992 KB Output is correct
16 Correct 17 ms 14992 KB Output is correct
17 Correct 17 ms 14992 KB Output is correct
18 Correct 16 ms 14992 KB Output is correct
19 Correct 18 ms 14992 KB Output is correct
20 Correct 16 ms 14992 KB Output is correct
21 Correct 16 ms 14992 KB Output is correct
22 Correct 17 ms 14992 KB Output is correct
23 Correct 17 ms 14992 KB Output is correct
24 Correct 17 ms 14992 KB Output is correct
25 Correct 17 ms 14992 KB Output is correct
26 Correct 17 ms 14992 KB Output is correct
27 Correct 17 ms 14992 KB Output is correct
28 Correct 17 ms 14992 KB Output is correct
29 Correct 18 ms 14992 KB Output is correct
30 Correct 14 ms 14992 KB Output is correct
31 Correct 571 ms 31332 KB Output is correct
32 Correct 225 ms 31332 KB Output is correct
33 Correct 559 ms 32684 KB Output is correct
34 Correct 607 ms 35832 KB Output is correct
35 Correct 613 ms 36252 KB Output is correct
36 Correct 723 ms 36364 KB Output is correct
37 Correct 483 ms 36364 KB Output is correct
38 Correct 463 ms 36364 KB Output is correct
39 Correct 410 ms 36364 KB Output is correct
40 Correct 412 ms 36364 KB Output is correct
41 Correct 459 ms 36364 KB Output is correct
42 Correct 460 ms 36364 KB Output is correct
43 Correct 179 ms 36364 KB Output is correct
44 Correct 440 ms 36468 KB Output is correct
45 Correct 419 ms 36468 KB Output is correct
46 Correct 396 ms 36468 KB Output is correct
47 Correct 292 ms 36468 KB Output is correct
48 Correct 282 ms 36468 KB Output is correct
49 Correct 349 ms 36468 KB Output is correct
50 Correct 363 ms 36468 KB Output is correct
51 Correct 320 ms 36468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1171 ms 106416 KB Output is correct
2 Correct 1048 ms 106416 KB Output is correct
3 Correct 1191 ms 146472 KB Output is correct
4 Correct 1087 ms 146472 KB Output is correct
5 Correct 1161 ms 146472 KB Output is correct
6 Correct 1044 ms 146472 KB Output is correct
7 Correct 1167 ms 161804 KB Output is correct
8 Correct 1020 ms 161804 KB Output is correct
9 Correct 925 ms 161804 KB Output is correct
10 Correct 932 ms 161804 KB Output is correct
11 Correct 1002 ms 161804 KB Output is correct
12 Correct 992 ms 161804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2461 ms 161804 KB Output is correct
2 Correct 1162 ms 161804 KB Output is correct
3 Correct 2951 ms 161804 KB Output is correct
4 Correct 2506 ms 168224 KB Output is correct
5 Correct 2424 ms 168224 KB Output is correct
6 Correct 2393 ms 168224 KB Output is correct
7 Correct 2835 ms 168224 KB Output is correct
8 Correct 2788 ms 168224 KB Output is correct
9 Correct 2390 ms 169328 KB Output is correct
10 Correct 2302 ms 169328 KB Output is correct
11 Correct 2392 ms 169328 KB Output is correct
12 Correct 2637 ms 169328 KB Output is correct
13 Correct 1693 ms 169328 KB Output is correct
14 Correct 1696 ms 169328 KB Output is correct
15 Correct 1845 ms 169328 KB Output is correct
16 Correct 1906 ms 169328 KB Output is correct
17 Correct 2068 ms 169328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14624 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Correct 15 ms 14624 KB Output is correct
5 Correct 20 ms 14760 KB Output is correct
6 Correct 16 ms 14760 KB Output is correct
7 Correct 18 ms 14760 KB Output is correct
8 Correct 16 ms 14916 KB Output is correct
9 Correct 18 ms 14916 KB Output is correct
10 Correct 19 ms 14916 KB Output is correct
11 Correct 18 ms 14944 KB Output is correct
12 Correct 18 ms 14948 KB Output is correct
13 Correct 16 ms 14992 KB Output is correct
14 Correct 15 ms 14992 KB Output is correct
15 Correct 18 ms 14992 KB Output is correct
16 Correct 17 ms 14992 KB Output is correct
17 Correct 17 ms 14992 KB Output is correct
18 Correct 16 ms 14992 KB Output is correct
19 Correct 18 ms 14992 KB Output is correct
20 Correct 16 ms 14992 KB Output is correct
21 Correct 16 ms 14992 KB Output is correct
22 Correct 17 ms 14992 KB Output is correct
23 Correct 17 ms 14992 KB Output is correct
24 Correct 17 ms 14992 KB Output is correct
25 Correct 17 ms 14992 KB Output is correct
26 Correct 17 ms 14992 KB Output is correct
27 Correct 17 ms 14992 KB Output is correct
28 Correct 17 ms 14992 KB Output is correct
29 Correct 18 ms 14992 KB Output is correct
30 Correct 14 ms 14992 KB Output is correct
31 Correct 571 ms 31332 KB Output is correct
32 Correct 225 ms 31332 KB Output is correct
33 Correct 559 ms 32684 KB Output is correct
34 Correct 607 ms 35832 KB Output is correct
35 Correct 613 ms 36252 KB Output is correct
36 Correct 723 ms 36364 KB Output is correct
37 Correct 483 ms 36364 KB Output is correct
38 Correct 463 ms 36364 KB Output is correct
39 Correct 410 ms 36364 KB Output is correct
40 Correct 412 ms 36364 KB Output is correct
41 Correct 459 ms 36364 KB Output is correct
42 Correct 460 ms 36364 KB Output is correct
43 Correct 179 ms 36364 KB Output is correct
44 Correct 440 ms 36468 KB Output is correct
45 Correct 419 ms 36468 KB Output is correct
46 Correct 396 ms 36468 KB Output is correct
47 Correct 292 ms 36468 KB Output is correct
48 Correct 282 ms 36468 KB Output is correct
49 Correct 349 ms 36468 KB Output is correct
50 Correct 363 ms 36468 KB Output is correct
51 Correct 320 ms 36468 KB Output is correct
52 Correct 567 ms 169328 KB Output is correct
53 Correct 531 ms 169328 KB Output is correct
54 Correct 549 ms 169328 KB Output is correct
55 Correct 478 ms 169328 KB Output is correct
56 Correct 468 ms 169328 KB Output is correct
57 Correct 452 ms 169328 KB Output is correct
58 Correct 459 ms 169328 KB Output is correct
59 Correct 510 ms 169328 KB Output is correct
60 Correct 475 ms 169328 KB Output is correct
61 Correct 219 ms 169328 KB Output is correct
62 Correct 596 ms 169328 KB Output is correct
63 Correct 549 ms 169328 KB Output is correct
64 Correct 529 ms 169328 KB Output is correct
65 Correct 472 ms 169328 KB Output is correct
66 Correct 448 ms 169328 KB Output is correct
67 Correct 261 ms 169328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14624 KB Output is correct
3 Correct 14 ms 14624 KB Output is correct
4 Correct 15 ms 14624 KB Output is correct
5 Correct 20 ms 14760 KB Output is correct
6 Correct 16 ms 14760 KB Output is correct
7 Correct 18 ms 14760 KB Output is correct
8 Correct 16 ms 14916 KB Output is correct
9 Correct 18 ms 14916 KB Output is correct
10 Correct 19 ms 14916 KB Output is correct
11 Correct 18 ms 14944 KB Output is correct
12 Correct 18 ms 14948 KB Output is correct
13 Correct 16 ms 14992 KB Output is correct
14 Correct 15 ms 14992 KB Output is correct
15 Correct 18 ms 14992 KB Output is correct
16 Correct 17 ms 14992 KB Output is correct
17 Correct 17 ms 14992 KB Output is correct
18 Correct 16 ms 14992 KB Output is correct
19 Correct 18 ms 14992 KB Output is correct
20 Correct 16 ms 14992 KB Output is correct
21 Correct 16 ms 14992 KB Output is correct
22 Correct 17 ms 14992 KB Output is correct
23 Correct 17 ms 14992 KB Output is correct
24 Correct 17 ms 14992 KB Output is correct
25 Correct 17 ms 14992 KB Output is correct
26 Correct 17 ms 14992 KB Output is correct
27 Correct 17 ms 14992 KB Output is correct
28 Correct 17 ms 14992 KB Output is correct
29 Correct 18 ms 14992 KB Output is correct
30 Correct 14 ms 14992 KB Output is correct
31 Correct 571 ms 31332 KB Output is correct
32 Correct 225 ms 31332 KB Output is correct
33 Correct 559 ms 32684 KB Output is correct
34 Correct 607 ms 35832 KB Output is correct
35 Correct 613 ms 36252 KB Output is correct
36 Correct 723 ms 36364 KB Output is correct
37 Correct 483 ms 36364 KB Output is correct
38 Correct 463 ms 36364 KB Output is correct
39 Correct 410 ms 36364 KB Output is correct
40 Correct 412 ms 36364 KB Output is correct
41 Correct 459 ms 36364 KB Output is correct
42 Correct 460 ms 36364 KB Output is correct
43 Correct 179 ms 36364 KB Output is correct
44 Correct 440 ms 36468 KB Output is correct
45 Correct 419 ms 36468 KB Output is correct
46 Correct 396 ms 36468 KB Output is correct
47 Correct 292 ms 36468 KB Output is correct
48 Correct 282 ms 36468 KB Output is correct
49 Correct 349 ms 36468 KB Output is correct
50 Correct 363 ms 36468 KB Output is correct
51 Correct 320 ms 36468 KB Output is correct
52 Correct 1171 ms 106416 KB Output is correct
53 Correct 1048 ms 106416 KB Output is correct
54 Correct 1191 ms 146472 KB Output is correct
55 Correct 1087 ms 146472 KB Output is correct
56 Correct 1161 ms 146472 KB Output is correct
57 Correct 1044 ms 146472 KB Output is correct
58 Correct 1167 ms 161804 KB Output is correct
59 Correct 1020 ms 161804 KB Output is correct
60 Correct 925 ms 161804 KB Output is correct
61 Correct 932 ms 161804 KB Output is correct
62 Correct 1002 ms 161804 KB Output is correct
63 Correct 992 ms 161804 KB Output is correct
64 Correct 2461 ms 161804 KB Output is correct
65 Correct 1162 ms 161804 KB Output is correct
66 Correct 2951 ms 161804 KB Output is correct
67 Correct 2506 ms 168224 KB Output is correct
68 Correct 2424 ms 168224 KB Output is correct
69 Correct 2393 ms 168224 KB Output is correct
70 Correct 2835 ms 168224 KB Output is correct
71 Correct 2788 ms 168224 KB Output is correct
72 Correct 2390 ms 169328 KB Output is correct
73 Correct 2302 ms 169328 KB Output is correct
74 Correct 2392 ms 169328 KB Output is correct
75 Correct 2637 ms 169328 KB Output is correct
76 Correct 1693 ms 169328 KB Output is correct
77 Correct 1696 ms 169328 KB Output is correct
78 Correct 1845 ms 169328 KB Output is correct
79 Correct 1906 ms 169328 KB Output is correct
80 Correct 2068 ms 169328 KB Output is correct
81 Correct 567 ms 169328 KB Output is correct
82 Correct 531 ms 169328 KB Output is correct
83 Correct 549 ms 169328 KB Output is correct
84 Correct 478 ms 169328 KB Output is correct
85 Correct 468 ms 169328 KB Output is correct
86 Correct 452 ms 169328 KB Output is correct
87 Correct 459 ms 169328 KB Output is correct
88 Correct 510 ms 169328 KB Output is correct
89 Correct 475 ms 169328 KB Output is correct
90 Correct 219 ms 169328 KB Output is correct
91 Correct 596 ms 169328 KB Output is correct
92 Correct 549 ms 169328 KB Output is correct
93 Correct 529 ms 169328 KB Output is correct
94 Correct 472 ms 169328 KB Output is correct
95 Correct 448 ms 169328 KB Output is correct
96 Correct 261 ms 169328 KB Output is correct
97 Correct 3437 ms 176208 KB Output is correct
98 Correct 1332 ms 176208 KB Output is correct
99 Correct 3886 ms 176208 KB Output is correct
100 Correct 3434 ms 196984 KB Output is correct
101 Correct 3492 ms 196984 KB Output is correct
102 Correct 4053 ms 196984 KB Output is correct
103 Correct 2994 ms 200944 KB Output is correct
104 Correct 3242 ms 220288 KB Output is correct
105 Correct 2615 ms 228244 KB Output is correct
106 Correct 2577 ms 240500 KB Output is correct
107 Correct 2813 ms 267312 KB Output is correct
108 Correct 2906 ms 289024 KB Output is correct
109 Correct 2700 ms 289024 KB Output is correct
110 Correct 3052 ms 304744 KB Output is correct
111 Correct 3054 ms 340600 KB Output is correct
112 Correct 2833 ms 340600 KB Output is correct
113 Correct 1143 ms 362264 KB Output is correct
114 Correct 3385 ms 386460 KB Output is correct
115 Correct 3535 ms 387084 KB Output is correct
116 Correct 3422 ms 387084 KB Output is correct
117 Correct 3152 ms 387084 KB Output is correct
118 Correct 2940 ms 390248 KB Output is correct
119 Correct 1415 ms 390248 KB Output is correct
120 Correct 1432 ms 399360 KB Output is correct
121 Correct 1604 ms 416296 KB Output is correct
122 Correct 1651 ms 430080 KB Output is correct
123 Correct 1834 ms 437860 KB Output is correct
124 Correct 2040 ms 448592 KB Output is correct
125 Correct 2043 ms 460588 KB Output is correct
126 Correct 2151 ms 472956 KB Output is correct