답안 #49709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49709 2018-06-02T09:24:45 Z gs13105 새 집 (APIO18_new_home) C++17
80 / 100
5000 ms 489536 KB
#pragma GCC optimize ("O3")

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <iterator>

using namespace std;

const int INF = 100000000;

static char buf[33554432]; // size : any number geq than 1024 
static int fio_idx = 0;
static int bytes = 0;
static inline int _read() {
    if(!bytes || fio_idx == bytes) {
        bytes = (int)fread(buf, sizeof(buf[0]), sizeof(buf), stdin);
        fio_idx = 0;
    }
    return buf[fio_idx++];
}
static inline int _readInt() {
    int x = 0;
    int c = _read();
    while(c <= 32) c = _read();
    while(c > 32) x = 10 * x + (c - '0'), c = _read();
    return x;
}

int n, k, q, bp;

int com[524288];
int c_sz;
inline int idx(int x)
{
    return lower_bound(com, com + c_sz, x) - com;
}

struct eve
{
    int t, x, d;
    bool operator <(const eve &e) const
    {
        return t != e.t ? t < e.t : d > e.d;
    }
};

struct pos
{
    int c, l, lt, r, rt;
};

eve arr[1048576];
map<int, pos> loc[524288];

struct seg
{
    int t1, t2, x, y;
    bool operator <(const seg &e) const
    {
        return x < e.x;
    }
};

vector<seg> sor[2];
inline void add_seg(int t1, int t2, int x, int y)
{
    if(t1 > t2 || x == y)
        return;

    int nt1 = bp + idx(t1);
    int nt2 = bp + idx(t2 + 1) - 1;
    if(nt1 > nt2)
        return;

    if(y < x)
        sor[0].push_back({ nt1, nt2, y, x });
    else
        sor[1].push_back({ nt1, nt2, -y, -x });
}

struct ent
{
    // v0 : dec, v1 : inc, q : query
    // v : { x, y }
    // q : { x, i }
    vector<pair<int, int>> v[2];
    vector<pair<int, int>> q;
};

ent mem[1048576];

struct que
{
    int l, y, i;
    bool operator <(const que &x) const
    {
        return l < x.l;
    }
};

que inp[524288];
bool chk[524288];
int ans[524288];

int main()
{
    n = _readInt();
    k = _readInt();
    q = _readInt();
    for(int i = 0; i < n; i++)
    {
        int x, t, a, b;
        x = _readInt();
        t = _readInt();
        a = _readInt();
        b = _readInt();
        arr[2 * i] = { a, x, t };
        arr[2 * i + 1] = { b, x, -t };
    }
    for(int i = 0; i < q; i++)
    {
        int l, y;
        l = _readInt();
        y = _readInt();
        inp[i] = { l, y, i };
        com[i] = y;
    }

    sort(com, com + q);
    c_sz = unique(com, com + q) - com;

    for(bp = 1; bp < c_sz; bp *= 2);

    sort(arr, arr + 2 * n);
    int cur, cnt = 0;
    for(int u = 0; u < 2 * n; u++)
    {
        eve &e = arr[u];
        if(e.d > 0)
        {
            if(loc[e.d].size() == 0)
            {
                cnt++;
                if(cnt == k)
                    cur = e.t;
            }

            auto it = loc[e.d].find(e.x);
            if(it != loc[e.d].end())
            {
                it->second.c++;
                continue;
            }

            it = loc[e.d].insert({ e.x,{ 1, 1, e.t, INF, e.t } }).first;
            pos &p = it->second;

            if(it != loc[e.d].begin())
            {
                int x = prev(it)->first;
                pos &q = prev(it)->second;

                add_seg(q.rt, e.t - 1, x, q.r);
                q.rt = e.t;
                q.r = (x + e.x) / 2;

                p.l = (x + e.x + 1) / 2;
            }

            if(next(it) != loc[e.d].end())
            {
                int x = next(it)->first;
                pos &q = next(it)->second;

                add_seg(q.lt, e.t - 1, x, q.l);
                q.lt = e.t;
                q.l = (x + e.x + 1) / 2;

                p.r = (x + e.x) / 2;
            }
        }
        else
        {
            e.d = -e.d;

            auto it = loc[e.d].find(e.x);
            pos &p = it->second;

            if(p.c > 1)
            {
                p.c--;
                continue;
            }

            add_seg(p.lt, e.t, e.x, p.l);
            add_seg(p.rt, e.t, e.x, p.r);

            if(it != loc[e.d].begin())
            {
                int x = prev(it)->first;
                pos &q = prev(it)->second;

                add_seg(q.rt, e.t, x, q.r);
                q.rt = e.t + 1;
                if(next(it) != loc[e.d].end())
                {
                    int y = next(it)->first;
                    q.r = (x + y) / 2;
                }
                else
                    q.r = INF;
            }

            if(next(it) != loc[e.d].end())
            {
                int x = next(it)->first;
                pos &q = next(it)->second;

                add_seg(q.lt, e.t, x, q.l);
                q.lt = e.t + 1;
                if(it != loc[e.d].begin())
                {
                    int y = prev(it)->first;
                    q.l = (y + x + 1) / 2;
                }
                else
                    q.l = 1;
            }

            loc[e.d].erase(it);

            if(loc[e.d].size() == 0)
            {
                if(cnt == k)
                {
                    int t1 = idx(cur);
                    int t2 = idx(e.t + 1) - 1;
                    for(int i = t1; i <= t2; i++)
                        chk[i] = 1;
                }
                cnt--;
            }
        }
    }

    sort(inp, inp + q);
    for(int u = 0; u < q; u++)
    {
        que &e = inp[u];
        e.y = idx(e.y);
        if(chk[e.y])
        {
            e.y += bp;
            while(e.y)
            {
                mem[e.y].q.push_back({ e.l, e.i });
                e.y /= 2;
            }
        }
        else
            ans[e.i] = -1;
    }

    for(int i = 0; i < 2; i++)
    {
        sort(sor[i].begin(), sor[i].end());
        for(seg &e : sor[i])
        {
            while(e.t1 < e.t2)
            {
                if(e.t1 % 2 == 1)
                {
                    mem[e.t1].v[i].push_back({ e.x, e.y });
                    e.t1++;
                }
                if(e.t2 % 2 == 0)
                {
                    mem[e.t2].v[i].push_back({ e.x, e.y });
                    e.t2--;
                }
                e.t1 /= 2;
                e.t2 /= 2;
            }
            if(e.t1 == e.t2)
                mem[e.t1].v[i].push_back({ e.x, e.y });
        }
    }

    for(int x = 1; x <= bp + c_sz - 1; x++)
    {
        int mx, p1, p2;
        {
            mx = p1 = p2 = 0;
            while(p2 < mem[x].q.size())
            {
                if(p1 != mem[x].v[0].size() && mem[x].v[0][p1].first <= mem[x].q[p2].first)
                {
                    mx = max(mx, mem[x].v[0][p1].second);
                    p1++;
                }
                else
                {
                    ans[mem[x].q[p2].second] = max(ans[mem[x].q[p2].second], mx - mem[x].q[p2].first);
                    p2++;
                }
            }
        }
        {
            mx = -INF;
            p1 = 0;
            p2 = mem[x].q.size() - 1;
            while(p2 >= 0)
            {
                if(p1 != mem[x].v[1].size() && mem[x].v[1][p1].first <= -mem[x].q[p2].first)
                {
                    mx = max(mx, mem[x].v[1][p1].second);
                    p1++;
                }
                else
                {
                    ans[mem[x].q[p2].second] = max(ans[mem[x].q[p2].second], mx + mem[x].q[p2].first);
                    p2--;
                }
            }
        }
    }

    for(int i = 0; i < q; i++)
        printf("%d\n", ans[i]);
    return 0;
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:307:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(p2 < mem[x].q.size())
                   ~~~^~~~~~~~~~~~~~~~~
new_home.cpp:309:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(p1 != mem[x].v[0].size() && mem[x].v[0][p1].first <= mem[x].q[p2].first)
                    ~~~^~~~~~~~~~~~~~~~~~~~~
new_home.cpp:327:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(p1 != mem[x].v[1].size() && mem[x].v[1][p1].first <= -mem[x].q[p2].first)
                    ~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 98940 KB Output is correct
2 Correct 101 ms 99044 KB Output is correct
3 Correct 84 ms 99044 KB Output is correct
4 Correct 85 ms 99044 KB Output is correct
5 Correct 92 ms 99104 KB Output is correct
6 Correct 87 ms 99232 KB Output is correct
7 Correct 85 ms 99232 KB Output is correct
8 Correct 91 ms 99236 KB Output is correct
9 Correct 96 ms 99236 KB Output is correct
10 Correct 109 ms 99360 KB Output is correct
11 Correct 92 ms 99360 KB Output is correct
12 Correct 239 ms 99360 KB Output is correct
13 Correct 92 ms 99360 KB Output is correct
14 Correct 88 ms 99360 KB Output is correct
15 Correct 99 ms 99360 KB Output is correct
16 Correct 95 ms 99360 KB Output is correct
17 Correct 93 ms 99360 KB Output is correct
18 Correct 108 ms 99360 KB Output is correct
19 Correct 92 ms 99360 KB Output is correct
20 Correct 92 ms 99360 KB Output is correct
21 Correct 92 ms 99360 KB Output is correct
22 Correct 88 ms 99360 KB Output is correct
23 Correct 85 ms 99360 KB Output is correct
24 Correct 91 ms 99360 KB Output is correct
25 Correct 93 ms 99440 KB Output is correct
26 Correct 89 ms 99492 KB Output is correct
27 Correct 100 ms 99492 KB Output is correct
28 Correct 89 ms 99492 KB Output is correct
29 Correct 96 ms 99492 KB Output is correct
30 Correct 105 ms 99492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 98940 KB Output is correct
2 Correct 101 ms 99044 KB Output is correct
3 Correct 84 ms 99044 KB Output is correct
4 Correct 85 ms 99044 KB Output is correct
5 Correct 92 ms 99104 KB Output is correct
6 Correct 87 ms 99232 KB Output is correct
7 Correct 85 ms 99232 KB Output is correct
8 Correct 91 ms 99236 KB Output is correct
9 Correct 96 ms 99236 KB Output is correct
10 Correct 109 ms 99360 KB Output is correct
11 Correct 92 ms 99360 KB Output is correct
12 Correct 239 ms 99360 KB Output is correct
13 Correct 92 ms 99360 KB Output is correct
14 Correct 88 ms 99360 KB Output is correct
15 Correct 99 ms 99360 KB Output is correct
16 Correct 95 ms 99360 KB Output is correct
17 Correct 93 ms 99360 KB Output is correct
18 Correct 108 ms 99360 KB Output is correct
19 Correct 92 ms 99360 KB Output is correct
20 Correct 92 ms 99360 KB Output is correct
21 Correct 92 ms 99360 KB Output is correct
22 Correct 88 ms 99360 KB Output is correct
23 Correct 85 ms 99360 KB Output is correct
24 Correct 91 ms 99360 KB Output is correct
25 Correct 93 ms 99440 KB Output is correct
26 Correct 89 ms 99492 KB Output is correct
27 Correct 100 ms 99492 KB Output is correct
28 Correct 89 ms 99492 KB Output is correct
29 Correct 96 ms 99492 KB Output is correct
30 Correct 105 ms 99492 KB Output is correct
31 Correct 1045 ms 168932 KB Output is correct
32 Correct 122 ms 168932 KB Output is correct
33 Correct 900 ms 168932 KB Output is correct
34 Correct 909 ms 168932 KB Output is correct
35 Correct 1006 ms 169684 KB Output is correct
36 Correct 976 ms 169792 KB Output is correct
37 Correct 684 ms 169792 KB Output is correct
38 Correct 640 ms 169792 KB Output is correct
39 Correct 490 ms 169792 KB Output is correct
40 Correct 531 ms 169792 KB Output is correct
41 Correct 600 ms 169792 KB Output is correct
42 Correct 628 ms 169792 KB Output is correct
43 Correct 114 ms 169792 KB Output is correct
44 Correct 729 ms 169792 KB Output is correct
45 Correct 631 ms 169792 KB Output is correct
46 Correct 531 ms 169792 KB Output is correct
47 Correct 283 ms 169792 KB Output is correct
48 Correct 287 ms 169792 KB Output is correct
49 Correct 367 ms 169792 KB Output is correct
50 Correct 446 ms 169792 KB Output is correct
51 Correct 377 ms 169792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1808 ms 265120 KB Output is correct
2 Correct 1990 ms 265120 KB Output is correct
3 Correct 1664 ms 265120 KB Output is correct
4 Correct 1784 ms 265328 KB Output is correct
5 Correct 2172 ms 277592 KB Output is correct
6 Correct 2335 ms 277592 KB Output is correct
7 Correct 1634 ms 277592 KB Output is correct
8 Correct 1733 ms 277592 KB Output is correct
9 Correct 1663 ms 284392 KB Output is correct
10 Correct 1743 ms 284392 KB Output is correct
11 Correct 1067 ms 284392 KB Output is correct
12 Correct 1305 ms 284392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4287 ms 346824 KB Output is correct
2 Correct 257 ms 346824 KB Output is correct
3 Correct 4633 ms 368036 KB Output is correct
4 Correct 955 ms 368036 KB Output is correct
5 Correct 2530 ms 368036 KB Output is correct
6 Correct 2414 ms 368036 KB Output is correct
7 Correct 4774 ms 368036 KB Output is correct
8 Correct 4733 ms 368036 KB Output is correct
9 Correct 1553 ms 368036 KB Output is correct
10 Correct 2819 ms 368036 KB Output is correct
11 Correct 4108 ms 368036 KB Output is correct
12 Correct 4750 ms 368176 KB Output is correct
13 Correct 2013 ms 368176 KB Output is correct
14 Correct 1752 ms 368176 KB Output is correct
15 Correct 2208 ms 368176 KB Output is correct
16 Correct 2566 ms 368176 KB Output is correct
17 Correct 2414 ms 368176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 98940 KB Output is correct
2 Correct 101 ms 99044 KB Output is correct
3 Correct 84 ms 99044 KB Output is correct
4 Correct 85 ms 99044 KB Output is correct
5 Correct 92 ms 99104 KB Output is correct
6 Correct 87 ms 99232 KB Output is correct
7 Correct 85 ms 99232 KB Output is correct
8 Correct 91 ms 99236 KB Output is correct
9 Correct 96 ms 99236 KB Output is correct
10 Correct 109 ms 99360 KB Output is correct
11 Correct 92 ms 99360 KB Output is correct
12 Correct 239 ms 99360 KB Output is correct
13 Correct 92 ms 99360 KB Output is correct
14 Correct 88 ms 99360 KB Output is correct
15 Correct 99 ms 99360 KB Output is correct
16 Correct 95 ms 99360 KB Output is correct
17 Correct 93 ms 99360 KB Output is correct
18 Correct 108 ms 99360 KB Output is correct
19 Correct 92 ms 99360 KB Output is correct
20 Correct 92 ms 99360 KB Output is correct
21 Correct 92 ms 99360 KB Output is correct
22 Correct 88 ms 99360 KB Output is correct
23 Correct 85 ms 99360 KB Output is correct
24 Correct 91 ms 99360 KB Output is correct
25 Correct 93 ms 99440 KB Output is correct
26 Correct 89 ms 99492 KB Output is correct
27 Correct 100 ms 99492 KB Output is correct
28 Correct 89 ms 99492 KB Output is correct
29 Correct 96 ms 99492 KB Output is correct
30 Correct 105 ms 99492 KB Output is correct
31 Correct 1045 ms 168932 KB Output is correct
32 Correct 122 ms 168932 KB Output is correct
33 Correct 900 ms 168932 KB Output is correct
34 Correct 909 ms 168932 KB Output is correct
35 Correct 1006 ms 169684 KB Output is correct
36 Correct 976 ms 169792 KB Output is correct
37 Correct 684 ms 169792 KB Output is correct
38 Correct 640 ms 169792 KB Output is correct
39 Correct 490 ms 169792 KB Output is correct
40 Correct 531 ms 169792 KB Output is correct
41 Correct 600 ms 169792 KB Output is correct
42 Correct 628 ms 169792 KB Output is correct
43 Correct 114 ms 169792 KB Output is correct
44 Correct 729 ms 169792 KB Output is correct
45 Correct 631 ms 169792 KB Output is correct
46 Correct 531 ms 169792 KB Output is correct
47 Correct 283 ms 169792 KB Output is correct
48 Correct 287 ms 169792 KB Output is correct
49 Correct 367 ms 169792 KB Output is correct
50 Correct 446 ms 169792 KB Output is correct
51 Correct 377 ms 169792 KB Output is correct
52 Correct 293 ms 368176 KB Output is correct
53 Correct 312 ms 368176 KB Output is correct
54 Correct 547 ms 368176 KB Output is correct
55 Correct 545 ms 368176 KB Output is correct
56 Correct 595 ms 368176 KB Output is correct
57 Correct 572 ms 368176 KB Output is correct
58 Correct 522 ms 368176 KB Output is correct
59 Correct 434 ms 368176 KB Output is correct
60 Correct 514 ms 368176 KB Output is correct
61 Correct 120 ms 368176 KB Output is correct
62 Correct 322 ms 368176 KB Output is correct
63 Correct 531 ms 368176 KB Output is correct
64 Correct 546 ms 368176 KB Output is correct
65 Correct 597 ms 368176 KB Output is correct
66 Correct 610 ms 368176 KB Output is correct
67 Correct 176 ms 368176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 98940 KB Output is correct
2 Correct 101 ms 99044 KB Output is correct
3 Correct 84 ms 99044 KB Output is correct
4 Correct 85 ms 99044 KB Output is correct
5 Correct 92 ms 99104 KB Output is correct
6 Correct 87 ms 99232 KB Output is correct
7 Correct 85 ms 99232 KB Output is correct
8 Correct 91 ms 99236 KB Output is correct
9 Correct 96 ms 99236 KB Output is correct
10 Correct 109 ms 99360 KB Output is correct
11 Correct 92 ms 99360 KB Output is correct
12 Correct 239 ms 99360 KB Output is correct
13 Correct 92 ms 99360 KB Output is correct
14 Correct 88 ms 99360 KB Output is correct
15 Correct 99 ms 99360 KB Output is correct
16 Correct 95 ms 99360 KB Output is correct
17 Correct 93 ms 99360 KB Output is correct
18 Correct 108 ms 99360 KB Output is correct
19 Correct 92 ms 99360 KB Output is correct
20 Correct 92 ms 99360 KB Output is correct
21 Correct 92 ms 99360 KB Output is correct
22 Correct 88 ms 99360 KB Output is correct
23 Correct 85 ms 99360 KB Output is correct
24 Correct 91 ms 99360 KB Output is correct
25 Correct 93 ms 99440 KB Output is correct
26 Correct 89 ms 99492 KB Output is correct
27 Correct 100 ms 99492 KB Output is correct
28 Correct 89 ms 99492 KB Output is correct
29 Correct 96 ms 99492 KB Output is correct
30 Correct 105 ms 99492 KB Output is correct
31 Correct 1045 ms 168932 KB Output is correct
32 Correct 122 ms 168932 KB Output is correct
33 Correct 900 ms 168932 KB Output is correct
34 Correct 909 ms 168932 KB Output is correct
35 Correct 1006 ms 169684 KB Output is correct
36 Correct 976 ms 169792 KB Output is correct
37 Correct 684 ms 169792 KB Output is correct
38 Correct 640 ms 169792 KB Output is correct
39 Correct 490 ms 169792 KB Output is correct
40 Correct 531 ms 169792 KB Output is correct
41 Correct 600 ms 169792 KB Output is correct
42 Correct 628 ms 169792 KB Output is correct
43 Correct 114 ms 169792 KB Output is correct
44 Correct 729 ms 169792 KB Output is correct
45 Correct 631 ms 169792 KB Output is correct
46 Correct 531 ms 169792 KB Output is correct
47 Correct 283 ms 169792 KB Output is correct
48 Correct 287 ms 169792 KB Output is correct
49 Correct 367 ms 169792 KB Output is correct
50 Correct 446 ms 169792 KB Output is correct
51 Correct 377 ms 169792 KB Output is correct
52 Correct 1808 ms 265120 KB Output is correct
53 Correct 1990 ms 265120 KB Output is correct
54 Correct 1664 ms 265120 KB Output is correct
55 Correct 1784 ms 265328 KB Output is correct
56 Correct 2172 ms 277592 KB Output is correct
57 Correct 2335 ms 277592 KB Output is correct
58 Correct 1634 ms 277592 KB Output is correct
59 Correct 1733 ms 277592 KB Output is correct
60 Correct 1663 ms 284392 KB Output is correct
61 Correct 1743 ms 284392 KB Output is correct
62 Correct 1067 ms 284392 KB Output is correct
63 Correct 1305 ms 284392 KB Output is correct
64 Correct 4287 ms 346824 KB Output is correct
65 Correct 257 ms 346824 KB Output is correct
66 Correct 4633 ms 368036 KB Output is correct
67 Correct 955 ms 368036 KB Output is correct
68 Correct 2530 ms 368036 KB Output is correct
69 Correct 2414 ms 368036 KB Output is correct
70 Correct 4774 ms 368036 KB Output is correct
71 Correct 4733 ms 368036 KB Output is correct
72 Correct 1553 ms 368036 KB Output is correct
73 Correct 2819 ms 368036 KB Output is correct
74 Correct 4108 ms 368036 KB Output is correct
75 Correct 4750 ms 368176 KB Output is correct
76 Correct 2013 ms 368176 KB Output is correct
77 Correct 1752 ms 368176 KB Output is correct
78 Correct 2208 ms 368176 KB Output is correct
79 Correct 2566 ms 368176 KB Output is correct
80 Correct 2414 ms 368176 KB Output is correct
81 Correct 293 ms 368176 KB Output is correct
82 Correct 312 ms 368176 KB Output is correct
83 Correct 547 ms 368176 KB Output is correct
84 Correct 545 ms 368176 KB Output is correct
85 Correct 595 ms 368176 KB Output is correct
86 Correct 572 ms 368176 KB Output is correct
87 Correct 522 ms 368176 KB Output is correct
88 Correct 434 ms 368176 KB Output is correct
89 Correct 514 ms 368176 KB Output is correct
90 Correct 120 ms 368176 KB Output is correct
91 Correct 322 ms 368176 KB Output is correct
92 Correct 531 ms 368176 KB Output is correct
93 Correct 546 ms 368176 KB Output is correct
94 Correct 597 ms 368176 KB Output is correct
95 Correct 610 ms 368176 KB Output is correct
96 Correct 176 ms 368176 KB Output is correct
97 Correct 1681 ms 368176 KB Output is correct
98 Correct 270 ms 368176 KB Output is correct
99 Execution timed out 5063 ms 489536 KB Time limit exceeded
100 Halted 0 ms 0 KB -