Submission #49729

# Submission time Handle Problem Language Result Execution time Memory
49729 2018-06-02T10:48:05 Z SpaimaCarpatilor New Home (APIO18_new_home) C++17
100 / 100
4949 ms 166808 KB
#include<bits/stdc++.h>

using namespace std;

int newN, N, K, Q, timeCount, xCount, x[300009], type[300009], a[300009], b[300009], location[300009], when[300009], ans[300009], realX[300009];
const int INF = 5e8;
int changesLine = 0;
vector < pair < int*, int > > changes[40];
pair < int, int > S[300009];
int prv[300009], nxt[300009];
vector < pair < int, int > > setChanges[40];

void update (int &x, int y)
{
    if (y > x)
        changes[changesLine].push_back ({&x, x}),
        x = y;
}

void rollBack (int id)
{
    reverse (changes[id].begin (), changes[id].end ());
    for (auto it : changes[id])
        *it.first = it.second;
    changes[id].clear ();
}

const int maxSize = 300009;
int aib[2][maxSize];

void update (int lin, int pos, int val)
{
    while (pos <= xCount)
        update (aib[lin][pos], val),
        pos += pos - (pos & (pos - 1));
}

int query (int lin, int pos)
{
    int ans = -INF;
    while (pos)
        ans = max (ans, aib[lin][pos]),
        pos &= pos - 1;
    return ans;
}

int maxDist (int x)
{
    int v1 = query (0, xCount - x + 1) + realX[x];
    int v2 = query (1, x) - realX[x];
    v1 = max (v1, v2);
    if (v1 >= 1e8) return -1;
    return v1;
}

void addSegm (int x0, int x2)
{
    int mid = (x0 + x2) / 2, pos;
    if (mid < realX[1]) pos = 0;
    else pos = lower_bound (realX + 1, realX + xCount + 1, mid + 1) - realX - 1;
    if (pos >= 1) update (0, xCount - pos + 1, -x0);
    if (pos < xCount) update (1, pos + 1, x2);
}

int steps = 0;
void delPoint (int it)
{
    int it0 = prv[it], it2 = nxt[it];
    int x0 = -INF, x2 = +INF;
    if (type[it0] == type[it]) x0 = x[it0];
    if (type[it2] == type[it]) x2 = x[it2];
    if (x2 > x0)
        addSegm (x0, x2);
    changes[changesLine].push_back ({&nxt[it0], nxt[it0]});
    changes[changesLine].push_back ({&prv[it2], prv[it2]});
    nxt[it0] = it2, prv[it2] = it0;
}

pair < pair < int, int >, int > normS[300009];
void init ()
{
    for (int i=1; i<=xCount; i++)
        aib[0][i] = aib[1][i] = -INF;
    for (int i=1; i<=N; i++)
        if (a[i] <= b[i])
            normS[++newN] = {{type[i], x[i]}, i};
    sort (normS + 1, normS + newN + 1);
    for (int i=1; i<=newN; i++)
        S[i] = normS[i].first;
    normS[newN + 1].second = N + 1;
    for (int i=1; i<=newN; i++)
        prv[normS[i].second] = normS[i - 1].second,
        nxt[normS[i].second] = normS[i + 1].second;
    prv[N + 1] = normS[newN].second, nxt[0] = normS[1].second;
    S[0].second = -INF, S[newN + 1].second = +INF;
    S[0].first = 0, S[newN + 1].first = K + 1;
    for (int i=1; i<=newN + 1; i++)
    {
        if (S[i].first == S[i - 1].first) addSegm (S[i - 1].second, S[i].second);
        else
        if (S[i].first == S[i - 1].first + 1)
        {
            if (i > 1) addSegm (S[i - 1].second, +INF);
            if (i <= newN) addSegm (-INF, S[i].second);
        }
        else
        {
            addSegm (-INF, INF);
            break;
        }
    }
    changes[changesLine].clear ();
/*    printf ("%d\n", maxDist (1));
    printf ("%d\n", maxDist (2));
    exit (0);*/
//    printf ("done initializing\n");
}

int mpX[300009];
vector < int > normT;
void normalize ()
{
    sort (normT.begin (), normT.end ());
    normT.erase (unique (normT.begin (), normT.end ()), normT.end ());
    timeCount = normT.size ();
    sort (mpX + 1, mpX + Q + 1);
    for (int i=1; i<=Q; i++)
        if (i == 1 || mpX[i] != mpX[i - 1])
            realX[++xCount] = mpX[i];
    for (int i=1; i<=Q; i++)
        when[i] = lower_bound (normT.begin (), normT.end (), when[i]) - normT.begin () + 1,
        location[i] = lower_bound (realX + 1, realX + xCount, location[i]) - realX;
    for (int i=1; i<=N; i++)
    {
        a[i] = lower_bound (normT.begin (), normT.end (), a[i]) - normT.begin () + 1;
        if (b[i] < normT[0]) b[i] = 0;
        else b[i] = lower_bound (normT.begin (), normT.end (), b[i] + 1) - normT.begin ();
    }
}

vector < int > delList[1200009], qry[300009];
void addToDelList (int nod, int st, int dr, int x, int y, int val)
{
    if (x <= st && dr <= y)
    {
        delList[nod].push_back (val);
        return ;
    }
    int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1;
    if (x <= mij) addToDelList (f1, st, mij, x, y, val);
    if (mij < y) addToDelList (f2, mij + 1, dr, x, y, val);
}

void buildDelList ()
{
    for (int i=1; i<=N; i++)
    if (a[i] <= b[i])
    {
        if (a[i] > 1)
            addToDelList (1, 1, timeCount, 1, a[i] - 1, i);
        if (b[i] < timeCount)
            addToDelList (1, 1, timeCount, b[i] + 1, timeCount, i);
    }
}

void solveDC (int nod, int st, int dr)
{
    changesLine ++;
    for (auto it : delList[nod])
        delPoint (it);
    if (st == dr)
    {
        /*printf ("%d:", st);
        for (int i=nxt[0]; i<=newN; i=nxt[i])
            printf (" %d", i);
        printf ("\n");*/
        for (auto it : qry[st])
            ans[it] = maxDist (location[it]);
    }
    else
    {
        int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1;
        solveDC (f1, st, mij);
        solveDC (f2, mij + 1, dr);
    }
    rollBack (changesLine), changesLine --;
    //for (auto it : delList[nod])
      //  printf ("roll back\n");
}

void Read (int &x);
int main ()
{
//freopen ("100", "r", stdin);
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

Read (N), Read (K), Read (Q), normT.resize (Q);
for (int i=1; i<=N; i++)
    Read (x[i]), Read (type[i]), Read (a[i]), Read (b[i]);
for (int i=1; i<=Q; i++)
    Read (location[i]), Read (when[i]),
    normT[i - 1] = when[i], mpX[i] = location[i];
normalize ();
init ();
buildDelList ();
for (int i=1; i<=Q; i++)
    qry[when[i]].push_back (i);
solveDC (1, 1, timeCount);
for (int i=1; i<=Q; i++)
    printf ("%d\n", ans[i]);
return 0;
}

const int maxl = 500000;
int pos = maxl - 1;
char buff[maxl];

void Next ()
{
    if (++pos == maxl)
        fread (buff, 1, maxl, stdin), pos = 0;
}

void Read (int &x)
{
    while (buff[pos] < '0' || buff[pos] > '9') Next ();
    for (x = 0; buff[pos] >= '0' && buff[pos] <= '9'; Next ()) x = x * 10 + buff[pos] - '0';
}

Compilation message

new_home.cpp: In function 'void Next()':
new_home.cpp:222:37: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         fread (buff, 1, maxl, stdin), pos = 0;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 35576 KB Output is correct
2 Correct 30 ms 35812 KB Output is correct
3 Correct 31 ms 35812 KB Output is correct
4 Correct 34 ms 35812 KB Output is correct
5 Correct 31 ms 35828 KB Output is correct
6 Correct 34 ms 36088 KB Output is correct
7 Correct 30 ms 36088 KB Output is correct
8 Correct 30 ms 36088 KB Output is correct
9 Correct 33 ms 36088 KB Output is correct
10 Correct 34 ms 36088 KB Output is correct
11 Correct 31 ms 36088 KB Output is correct
12 Correct 30 ms 36088 KB Output is correct
13 Correct 31 ms 36088 KB Output is correct
14 Correct 32 ms 36088 KB Output is correct
15 Correct 31 ms 36088 KB Output is correct
16 Correct 32 ms 36088 KB Output is correct
17 Correct 30 ms 36088 KB Output is correct
18 Correct 34 ms 36088 KB Output is correct
19 Correct 33 ms 36088 KB Output is correct
20 Correct 34 ms 36088 KB Output is correct
21 Correct 36 ms 36088 KB Output is correct
22 Correct 32 ms 36088 KB Output is correct
23 Correct 34 ms 36088 KB Output is correct
24 Correct 34 ms 36088 KB Output is correct
25 Correct 33 ms 36088 KB Output is correct
26 Correct 32 ms 36092 KB Output is correct
27 Correct 34 ms 36092 KB Output is correct
28 Correct 34 ms 36092 KB Output is correct
29 Correct 35 ms 36212 KB Output is correct
30 Correct 35 ms 36212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 35576 KB Output is correct
2 Correct 30 ms 35812 KB Output is correct
3 Correct 31 ms 35812 KB Output is correct
4 Correct 34 ms 35812 KB Output is correct
5 Correct 31 ms 35828 KB Output is correct
6 Correct 34 ms 36088 KB Output is correct
7 Correct 30 ms 36088 KB Output is correct
8 Correct 30 ms 36088 KB Output is correct
9 Correct 33 ms 36088 KB Output is correct
10 Correct 34 ms 36088 KB Output is correct
11 Correct 31 ms 36088 KB Output is correct
12 Correct 30 ms 36088 KB Output is correct
13 Correct 31 ms 36088 KB Output is correct
14 Correct 32 ms 36088 KB Output is correct
15 Correct 31 ms 36088 KB Output is correct
16 Correct 32 ms 36088 KB Output is correct
17 Correct 30 ms 36088 KB Output is correct
18 Correct 34 ms 36088 KB Output is correct
19 Correct 33 ms 36088 KB Output is correct
20 Correct 34 ms 36088 KB Output is correct
21 Correct 36 ms 36088 KB Output is correct
22 Correct 32 ms 36088 KB Output is correct
23 Correct 34 ms 36088 KB Output is correct
24 Correct 34 ms 36088 KB Output is correct
25 Correct 33 ms 36088 KB Output is correct
26 Correct 32 ms 36092 KB Output is correct
27 Correct 34 ms 36092 KB Output is correct
28 Correct 34 ms 36092 KB Output is correct
29 Correct 35 ms 36212 KB Output is correct
30 Correct 35 ms 36212 KB Output is correct
31 Correct 613 ms 56432 KB Output is correct
32 Correct 79 ms 56432 KB Output is correct
33 Correct 535 ms 59592 KB Output is correct
34 Correct 510 ms 59592 KB Output is correct
35 Correct 522 ms 59592 KB Output is correct
36 Correct 534 ms 59592 KB Output is correct
37 Correct 509 ms 59592 KB Output is correct
38 Correct 479 ms 60816 KB Output is correct
39 Correct 373 ms 60816 KB Output is correct
40 Correct 370 ms 60816 KB Output is correct
41 Correct 490 ms 60816 KB Output is correct
42 Correct 416 ms 60816 KB Output is correct
43 Correct 55 ms 60816 KB Output is correct
44 Correct 438 ms 60816 KB Output is correct
45 Correct 519 ms 60816 KB Output is correct
46 Correct 554 ms 60816 KB Output is correct
47 Correct 351 ms 60816 KB Output is correct
48 Correct 366 ms 60816 KB Output is correct
49 Correct 342 ms 60816 KB Output is correct
50 Correct 319 ms 60816 KB Output is correct
51 Correct 377 ms 60816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 746 ms 81112 KB Output is correct
2 Correct 679 ms 84580 KB Output is correct
3 Correct 537 ms 84580 KB Output is correct
4 Correct 666 ms 84580 KB Output is correct
5 Correct 663 ms 123372 KB Output is correct
6 Correct 694 ms 123372 KB Output is correct
7 Correct 660 ms 123372 KB Output is correct
8 Correct 602 ms 123372 KB Output is correct
9 Correct 668 ms 123372 KB Output is correct
10 Correct 601 ms 123372 KB Output is correct
11 Correct 397 ms 123372 KB Output is correct
12 Correct 453 ms 123372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2741 ms 140040 KB Output is correct
2 Correct 263 ms 140040 KB Output is correct
3 Correct 2949 ms 143816 KB Output is correct
4 Correct 1415 ms 143816 KB Output is correct
5 Correct 1925 ms 143816 KB Output is correct
6 Correct 2000 ms 143816 KB Output is correct
7 Correct 2455 ms 166808 KB Output is correct
8 Correct 2493 ms 166808 KB Output is correct
9 Correct 1210 ms 166808 KB Output is correct
10 Correct 1869 ms 166808 KB Output is correct
11 Correct 2484 ms 166808 KB Output is correct
12 Correct 2228 ms 166808 KB Output is correct
13 Correct 1018 ms 166808 KB Output is correct
14 Correct 1008 ms 166808 KB Output is correct
15 Correct 1140 ms 166808 KB Output is correct
16 Correct 1389 ms 166808 KB Output is correct
17 Correct 1271 ms 166808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 35576 KB Output is correct
2 Correct 30 ms 35812 KB Output is correct
3 Correct 31 ms 35812 KB Output is correct
4 Correct 34 ms 35812 KB Output is correct
5 Correct 31 ms 35828 KB Output is correct
6 Correct 34 ms 36088 KB Output is correct
7 Correct 30 ms 36088 KB Output is correct
8 Correct 30 ms 36088 KB Output is correct
9 Correct 33 ms 36088 KB Output is correct
10 Correct 34 ms 36088 KB Output is correct
11 Correct 31 ms 36088 KB Output is correct
12 Correct 30 ms 36088 KB Output is correct
13 Correct 31 ms 36088 KB Output is correct
14 Correct 32 ms 36088 KB Output is correct
15 Correct 31 ms 36088 KB Output is correct
16 Correct 32 ms 36088 KB Output is correct
17 Correct 30 ms 36088 KB Output is correct
18 Correct 34 ms 36088 KB Output is correct
19 Correct 33 ms 36088 KB Output is correct
20 Correct 34 ms 36088 KB Output is correct
21 Correct 36 ms 36088 KB Output is correct
22 Correct 32 ms 36088 KB Output is correct
23 Correct 34 ms 36088 KB Output is correct
24 Correct 34 ms 36088 KB Output is correct
25 Correct 33 ms 36088 KB Output is correct
26 Correct 32 ms 36092 KB Output is correct
27 Correct 34 ms 36092 KB Output is correct
28 Correct 34 ms 36092 KB Output is correct
29 Correct 35 ms 36212 KB Output is correct
30 Correct 35 ms 36212 KB Output is correct
31 Correct 613 ms 56432 KB Output is correct
32 Correct 79 ms 56432 KB Output is correct
33 Correct 535 ms 59592 KB Output is correct
34 Correct 510 ms 59592 KB Output is correct
35 Correct 522 ms 59592 KB Output is correct
36 Correct 534 ms 59592 KB Output is correct
37 Correct 509 ms 59592 KB Output is correct
38 Correct 479 ms 60816 KB Output is correct
39 Correct 373 ms 60816 KB Output is correct
40 Correct 370 ms 60816 KB Output is correct
41 Correct 490 ms 60816 KB Output is correct
42 Correct 416 ms 60816 KB Output is correct
43 Correct 55 ms 60816 KB Output is correct
44 Correct 438 ms 60816 KB Output is correct
45 Correct 519 ms 60816 KB Output is correct
46 Correct 554 ms 60816 KB Output is correct
47 Correct 351 ms 60816 KB Output is correct
48 Correct 366 ms 60816 KB Output is correct
49 Correct 342 ms 60816 KB Output is correct
50 Correct 319 ms 60816 KB Output is correct
51 Correct 377 ms 60816 KB Output is correct
52 Correct 333 ms 166808 KB Output is correct
53 Correct 319 ms 166808 KB Output is correct
54 Correct 384 ms 166808 KB Output is correct
55 Correct 417 ms 166808 KB Output is correct
56 Correct 326 ms 166808 KB Output is correct
57 Correct 457 ms 166808 KB Output is correct
58 Correct 477 ms 166808 KB Output is correct
59 Correct 406 ms 166808 KB Output is correct
60 Correct 468 ms 166808 KB Output is correct
61 Correct 60 ms 166808 KB Output is correct
62 Correct 308 ms 166808 KB Output is correct
63 Correct 320 ms 166808 KB Output is correct
64 Correct 313 ms 166808 KB Output is correct
65 Correct 356 ms 166808 KB Output is correct
66 Correct 490 ms 166808 KB Output is correct
67 Correct 106 ms 166808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 35576 KB Output is correct
2 Correct 30 ms 35812 KB Output is correct
3 Correct 31 ms 35812 KB Output is correct
4 Correct 34 ms 35812 KB Output is correct
5 Correct 31 ms 35828 KB Output is correct
6 Correct 34 ms 36088 KB Output is correct
7 Correct 30 ms 36088 KB Output is correct
8 Correct 30 ms 36088 KB Output is correct
9 Correct 33 ms 36088 KB Output is correct
10 Correct 34 ms 36088 KB Output is correct
11 Correct 31 ms 36088 KB Output is correct
12 Correct 30 ms 36088 KB Output is correct
13 Correct 31 ms 36088 KB Output is correct
14 Correct 32 ms 36088 KB Output is correct
15 Correct 31 ms 36088 KB Output is correct
16 Correct 32 ms 36088 KB Output is correct
17 Correct 30 ms 36088 KB Output is correct
18 Correct 34 ms 36088 KB Output is correct
19 Correct 33 ms 36088 KB Output is correct
20 Correct 34 ms 36088 KB Output is correct
21 Correct 36 ms 36088 KB Output is correct
22 Correct 32 ms 36088 KB Output is correct
23 Correct 34 ms 36088 KB Output is correct
24 Correct 34 ms 36088 KB Output is correct
25 Correct 33 ms 36088 KB Output is correct
26 Correct 32 ms 36092 KB Output is correct
27 Correct 34 ms 36092 KB Output is correct
28 Correct 34 ms 36092 KB Output is correct
29 Correct 35 ms 36212 KB Output is correct
30 Correct 35 ms 36212 KB Output is correct
31 Correct 613 ms 56432 KB Output is correct
32 Correct 79 ms 56432 KB Output is correct
33 Correct 535 ms 59592 KB Output is correct
34 Correct 510 ms 59592 KB Output is correct
35 Correct 522 ms 59592 KB Output is correct
36 Correct 534 ms 59592 KB Output is correct
37 Correct 509 ms 59592 KB Output is correct
38 Correct 479 ms 60816 KB Output is correct
39 Correct 373 ms 60816 KB Output is correct
40 Correct 370 ms 60816 KB Output is correct
41 Correct 490 ms 60816 KB Output is correct
42 Correct 416 ms 60816 KB Output is correct
43 Correct 55 ms 60816 KB Output is correct
44 Correct 438 ms 60816 KB Output is correct
45 Correct 519 ms 60816 KB Output is correct
46 Correct 554 ms 60816 KB Output is correct
47 Correct 351 ms 60816 KB Output is correct
48 Correct 366 ms 60816 KB Output is correct
49 Correct 342 ms 60816 KB Output is correct
50 Correct 319 ms 60816 KB Output is correct
51 Correct 377 ms 60816 KB Output is correct
52 Correct 746 ms 81112 KB Output is correct
53 Correct 679 ms 84580 KB Output is correct
54 Correct 537 ms 84580 KB Output is correct
55 Correct 666 ms 84580 KB Output is correct
56 Correct 663 ms 123372 KB Output is correct
57 Correct 694 ms 123372 KB Output is correct
58 Correct 660 ms 123372 KB Output is correct
59 Correct 602 ms 123372 KB Output is correct
60 Correct 668 ms 123372 KB Output is correct
61 Correct 601 ms 123372 KB Output is correct
62 Correct 397 ms 123372 KB Output is correct
63 Correct 453 ms 123372 KB Output is correct
64 Correct 2741 ms 140040 KB Output is correct
65 Correct 263 ms 140040 KB Output is correct
66 Correct 2949 ms 143816 KB Output is correct
67 Correct 1415 ms 143816 KB Output is correct
68 Correct 1925 ms 143816 KB Output is correct
69 Correct 2000 ms 143816 KB Output is correct
70 Correct 2455 ms 166808 KB Output is correct
71 Correct 2493 ms 166808 KB Output is correct
72 Correct 1210 ms 166808 KB Output is correct
73 Correct 1869 ms 166808 KB Output is correct
74 Correct 2484 ms 166808 KB Output is correct
75 Correct 2228 ms 166808 KB Output is correct
76 Correct 1018 ms 166808 KB Output is correct
77 Correct 1008 ms 166808 KB Output is correct
78 Correct 1140 ms 166808 KB Output is correct
79 Correct 1389 ms 166808 KB Output is correct
80 Correct 1271 ms 166808 KB Output is correct
81 Correct 333 ms 166808 KB Output is correct
82 Correct 319 ms 166808 KB Output is correct
83 Correct 384 ms 166808 KB Output is correct
84 Correct 417 ms 166808 KB Output is correct
85 Correct 326 ms 166808 KB Output is correct
86 Correct 457 ms 166808 KB Output is correct
87 Correct 477 ms 166808 KB Output is correct
88 Correct 406 ms 166808 KB Output is correct
89 Correct 468 ms 166808 KB Output is correct
90 Correct 60 ms 166808 KB Output is correct
91 Correct 308 ms 166808 KB Output is correct
92 Correct 320 ms 166808 KB Output is correct
93 Correct 313 ms 166808 KB Output is correct
94 Correct 356 ms 166808 KB Output is correct
95 Correct 490 ms 166808 KB Output is correct
96 Correct 106 ms 166808 KB Output is correct
97 Correct 2622 ms 166808 KB Output is correct
98 Correct 328 ms 166808 KB Output is correct
99 Correct 4949 ms 166808 KB Output is correct
100 Correct 2492 ms 166808 KB Output is correct
101 Correct 3082 ms 166808 KB Output is correct
102 Correct 4931 ms 166808 KB Output is correct
103 Correct 3798 ms 166808 KB Output is correct
104 Correct 3233 ms 166808 KB Output is correct
105 Correct 2237 ms 166808 KB Output is correct
106 Correct 2581 ms 166808 KB Output is correct
107 Correct 3054 ms 166808 KB Output is correct
108 Correct 2526 ms 166808 KB Output is correct
109 Correct 2960 ms 166808 KB Output is correct
110 Correct 3111 ms 166808 KB Output is correct
111 Correct 2450 ms 166808 KB Output is correct
112 Correct 3100 ms 166808 KB Output is correct
113 Correct 139 ms 166808 KB Output is correct
114 Correct 1909 ms 166808 KB Output is correct
115 Correct 2571 ms 166808 KB Output is correct
116 Correct 2327 ms 166808 KB Output is correct
117 Correct 2879 ms 166808 KB Output is correct
118 Correct 3731 ms 166808 KB Output is correct
119 Correct 486 ms 166808 KB Output is correct
120 Correct 1514 ms 166808 KB Output is correct
121 Correct 1666 ms 166808 KB Output is correct
122 Correct 1711 ms 166808 KB Output is correct
123 Correct 1821 ms 166808 KB Output is correct
124 Correct 1975 ms 166808 KB Output is correct
125 Correct 2154 ms 166808 KB Output is correct
126 Correct 1973 ms 166808 KB Output is correct