Submission #159500

# Submission time Handle Problem Language Result Execution time Memory
159500 2019-10-23T01:01:33 Z arnold518 New Home (APIO18_new_home) C++14
47 / 100
5000 ms 433808 KB
#include <bits/stdc++.h>
#pragma gcc optimize "03"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;
const int MAXVAL = 1e8;

struct Query
{
    int t, x, k, p;
    bool operator < (const Query &p)
    {
        if(t!=p.t) return t<p.t;
        return k>p.k;
    }
};

int N, K, Q, ans[MAXN+10], SZ, chk;
vector<Query> query;
multiset<int> S[MAXN+10];
set<int> scomp;
vector<int> comp;

int getcomp(int x)
{
    //for(auto it : comp) printf("%d ", it); printf("-> %d\n", x);
    //assert(binary_search(comp.begin(), comp.end(), x));
    return lower_bound(comp.begin(), comp.end(), x)-comp.begin();
}

priority_queue<int, vector<int>, less<int>> tree1[5*MAXN+10];
priority_queue<int, vector<int>, less<int>> dead1[5*MAXN+10];

priority_queue<int, vector<int>, greater<int>> tree2[5*MAXN+10];
priority_queue<int, vector<int>, greater<int>> dead2[5*MAXN+10];

ll query1(int x)
{
    int y=x;
    x=getcomp(x);
    int ret=0;
    for(; x>0; x-=(x&-x))
    {
        while(!tree1[x].empty() && !dead1[x].empty() && tree1[x].top()==dead1[x].top()) tree1[x].pop(), dead1[x].pop();
        if(!tree1[x].empty())
        {
            ////printf("QUERY1 %d %d\n", x, tree1[x].top()-y);
            ret=max(ret, tree1[x].top()-y);
        }
    }
    return ret;
}

void erase1(int x, int y)
{
    ////printf("ERASE1 %d %d\n", x, y);
    x=getcomp(x);
    for(; x<=SZ; x+=(x&-x))
    {
        dead1[x].push(y);
    }
}

void insert1(int x, int y)
{
    //printf("INSERT1 %d %d\n", x, y);
    x=getcomp(x);
    for(; x<=SZ; x+=(x&-x))
    {
        tree1[x].push(y);
    }
}

ll query2(int x)
{
    int y=x;
    x=getcomp(x);
    x=SZ-x+1;
    int ret=0;
    for(; x>0; x-=(x&-x))
    {
        while(!tree2[x].empty() && !dead2[x].empty() && tree2[x].top()==dead2[x].top()) tree2[x].pop(), dead2[x].pop();
        if(!tree2[x].empty())
        {
            ////printf("QUERY1 %d %d\n", x, y-tree2[x].top());
            ret=max(ret, y-tree2[x].top());
        }
    }
    return ret;
}

void erase2(int x, int y)
{
    swap(x, y);
    ////printf("ERASE2 %d %d\n", x, y);
    x=getcomp(x);
    x=SZ-x+1;
    for(; x<=SZ; x+=(x&-x))
    {
        dead2[x].push(y);
    }
}

void insert2(int x, int y)
{
    //printf("INSERT2 %d %d\n", x, y);
    swap(x, y);
    x=getcomp(x);
    x=SZ-x+1;
    for(; x<=SZ; x+=(x&-x))
    {
        tree2[x].push(y);
    }
}

int main()
{
    int i, j;

    scanf("%d%d%d", &N, &K, &Q);
    for(i=1; i<=N; i++)
    {
        int x, t, a, b;
        scanf("%d%d%d%d", &x, &t, &a, &b); b++; x*=2;
        query.push_back({a, x, t, 1});
        query.push_back({b, x, t, -1});
        scomp.insert(x);
    }
    int last=0;
    for(i=1; i<=Q; i++)
    {
        int l, x;
        scanf("%d%d", &x, &l); x*=2;
        query.push_back({l, x, -i, 0});
        last=max(last, l);
        scomp.insert(x);
    }
    sort(query.begin(), query.end());

    scomp.insert(0); scomp.insert(2e8);
    for(auto now : query)
    {
        int x=now.x;
        if(now.p==1)
        {
            if(S[now.k].find(x)==S[now.k].end())
            {
                if(S[now.k].size()!=0)
                {
                    auto it=S[now.k].lower_bound(x);
                    int r=*it, l=*prev(it);
                    if(it==S[now.k].end()) scomp.insert(l+x>>1);
                    else if(it==S[now.k].begin()) scomp.insert(r+x>>1);
                    else
                    {
                        scomp.insert(l+x>>1);
                        scomp.insert(r+x>>1);
                    }
                }
            }
            S[now.k].insert(x);
        }
        else if(now.p==-1)
        {
            S[now.k].erase(S[now.k].find(x));
            if(S[now.k].find(x)==S[now.k].end())
            {
                if(S[now.k].size()!=0)
                {
                    auto it=S[now.k].lower_bound(x);
                    int r=*it, l=*prev(it);
                    if(it==S[now.k].end());
                    else if(it==S[now.k].begin());
                    else scomp.insert(l+r>>1);
                }
            }
        }
    }

    comp.push_back(-1);
    for(auto it : scomp) comp.push_back(it);
    SZ=scomp.size();

    for(i=1; i<=K; i++) S[i].clear();

    for(auto now : query)
    {
        //printf("===============\n");
        if(last<now.t) break;
        int x=now.x;
        if(now.p==0)
        {
            if(chk!=K) ans[-now.k]=-1;
            else ans[-now.k]=max(query1(x), query2(x))>>1;
            //printf("!%d %d %d\n", -now.k, query1(x), query2(x));
        }
        else if(now.p==1)
        {
            if(S[now.k].find(now.x)==S[now.k].end())
            {
                if(S[now.k].size()==0)
                {
                    insert1(0, x);
                    insert2(x, 2e8);
                    chk++;
                }
                else
                {
                    auto it=S[now.k].lower_bound(x);
                    int r=*it, l=*prev(it);
                    if(it==S[now.k].end())
                    {
                        erase2(l, 2e8);
                        insert2(l, l+x>>1);
                        insert1(l+x>>1, x);
                        insert2(x, 2e8);
                    }
                    else if(it==S[now.k].begin())
                    {
                        erase1(0, r);
                        insert1(0, x);
                        insert2(x, r+x>>1);
                        insert1(r+x>>1, r);
                    }
                    else
                    {
                        erase2(l, l+r>>1);
                        erase1(l+r>>1, r);
                        insert2(l, l+x>>1);
                        insert1(l+x>>1, x);
                        insert2(x, x+r>>1);
                        insert1(x+r>>1, r);
                    }
                }
            }
            S[now.k].insert(now.x);
        }
        else if(now.p==-1)
        {
            S[now.k].erase(S[now.k].find(now.x));
            if(S[now.k].find(now.x)==S[now.k].end())
            {
                if(S[now.k].size()==0)
                {
                    erase1(0, x);
                    erase2(x, 2e8);
                    chk--;
                }
                else
                {
                    auto it=S[now.k].lower_bound(x);
                    int r=*it, l=*prev(it);
                    if(it==S[now.k].end())
                    {
                        insert2(l, 2e8);
                        erase2(l, l+x>>1);
                        erase1(l+x>>1, x);
                        erase2(x, 2e8);
                    }
                    else if(it==S[now.k].begin())
                    {
                        insert1(0, r);
                        erase1(0, x);
                        erase2(x, r+x>>1);
                        erase1(r+x>>1, r);
                    }
                    else
                    {
                        insert2(l, l+r>>1);
                        insert1(l+r>>1, r);
                        erase2(l, l+x>>1);
                        erase1(l+x>>1, x);
                        erase2(x, x+r>>1);
                        erase1(x+r>>1, r);
                    }
                }
            }
        }
    }
    for(i=1; i<=Q; i++) printf("%d\n", ans[i]);
}

Compilation message

new_home.cpp:2:0: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
 #pragma gcc optimize "03"
 
new_home.cpp: In function 'int main()':
new_home.cpp:156:58: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                     if(it==S[now.k].end()) scomp.insert(l+x>>1);
                                                         ~^~
new_home.cpp:157:65: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                     else if(it==S[now.k].begin()) scomp.insert(r+x>>1);
                                                                ~^~
new_home.cpp:160:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         scomp.insert(l+x>>1);
                                      ~^~
new_home.cpp:161:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         scomp.insert(r+x>>1);
                                      ~^~
new_home.cpp:178:40: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                     else scomp.insert(l+r>>1);
                                       ~^~
new_home.cpp:218:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert2(l, l+x>>1);
                                    ~^~
new_home.cpp:219:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert1(l+x>>1, x);
                                 ~^~
new_home.cpp:226:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert2(x, r+x>>1);
                                    ~^~
new_home.cpp:227:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert1(r+x>>1, r);
                                 ~^~
new_home.cpp:231:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase2(l, l+r>>1);
                                   ~^~
new_home.cpp:232:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase1(l+r>>1, r);
                                ~^~
new_home.cpp:233:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert2(l, l+x>>1);
                                    ~^~
new_home.cpp:234:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert1(l+x>>1, x);
                                 ~^~
new_home.cpp:235:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert2(x, x+r>>1);
                                    ~^~
new_home.cpp:236:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert1(x+r>>1, r);
                                 ~^~
new_home.cpp:260:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase2(l, l+x>>1);
                                   ~^~
new_home.cpp:261:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase1(l+x>>1, x);
                                ~^~
new_home.cpp:268:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase2(x, r+x>>1);
                                   ~^~
new_home.cpp:269:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase1(r+x>>1, r);
                                ~^~
new_home.cpp:273:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert2(l, l+r>>1);
                                    ~^~
new_home.cpp:274:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         insert1(l+r>>1, r);
                                 ~^~
new_home.cpp:275:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase2(l, l+x>>1);
                                   ~^~
new_home.cpp:276:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase1(l+x>>1, x);
                                ~^~
new_home.cpp:277:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase2(x, x+r>>1);
                                   ~^~
new_home.cpp:278:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
                         erase1(x+r>>1, r);
                                ~^~
new_home.cpp:122:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
new_home.cpp:124:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &N, &K, &Q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:128:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d", &x, &t, &a, &b); b++; x*=2;
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &l); x*=2;
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 185 ms 202360 KB Output is correct
2 Correct 188 ms 202264 KB Output is correct
3 Correct 187 ms 202300 KB Output is correct
4 Correct 187 ms 202332 KB Output is correct
5 Correct 188 ms 202384 KB Output is correct
6 Correct 192 ms 202812 KB Output is correct
7 Correct 187 ms 202464 KB Output is correct
8 Correct 189 ms 202608 KB Output is correct
9 Correct 187 ms 202568 KB Output is correct
10 Correct 191 ms 202652 KB Output is correct
11 Correct 189 ms 202580 KB Output is correct
12 Correct 194 ms 202580 KB Output is correct
13 Correct 190 ms 202456 KB Output is correct
14 Correct 204 ms 202692 KB Output is correct
15 Correct 190 ms 202488 KB Output is correct
16 Correct 189 ms 202484 KB Output is correct
17 Correct 190 ms 202572 KB Output is correct
18 Correct 188 ms 202616 KB Output is correct
19 Correct 189 ms 202512 KB Output is correct
20 Correct 188 ms 202616 KB Output is correct
21 Correct 189 ms 202368 KB Output is correct
22 Correct 187 ms 202548 KB Output is correct
23 Correct 190 ms 202520 KB Output is correct
24 Correct 220 ms 202488 KB Output is correct
25 Correct 192 ms 202624 KB Output is correct
26 Correct 188 ms 202488 KB Output is correct
27 Correct 191 ms 202444 KB Output is correct
28 Correct 188 ms 202492 KB Output is correct
29 Correct 187 ms 202612 KB Output is correct
30 Correct 186 ms 202488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 202360 KB Output is correct
2 Correct 188 ms 202264 KB Output is correct
3 Correct 187 ms 202300 KB Output is correct
4 Correct 187 ms 202332 KB Output is correct
5 Correct 188 ms 202384 KB Output is correct
6 Correct 192 ms 202812 KB Output is correct
7 Correct 187 ms 202464 KB Output is correct
8 Correct 189 ms 202608 KB Output is correct
9 Correct 187 ms 202568 KB Output is correct
10 Correct 191 ms 202652 KB Output is correct
11 Correct 189 ms 202580 KB Output is correct
12 Correct 194 ms 202580 KB Output is correct
13 Correct 190 ms 202456 KB Output is correct
14 Correct 204 ms 202692 KB Output is correct
15 Correct 190 ms 202488 KB Output is correct
16 Correct 189 ms 202484 KB Output is correct
17 Correct 190 ms 202572 KB Output is correct
18 Correct 188 ms 202616 KB Output is correct
19 Correct 189 ms 202512 KB Output is correct
20 Correct 188 ms 202616 KB Output is correct
21 Correct 189 ms 202368 KB Output is correct
22 Correct 187 ms 202548 KB Output is correct
23 Correct 190 ms 202520 KB Output is correct
24 Correct 220 ms 202488 KB Output is correct
25 Correct 192 ms 202624 KB Output is correct
26 Correct 188 ms 202488 KB Output is correct
27 Correct 191 ms 202444 KB Output is correct
28 Correct 188 ms 202492 KB Output is correct
29 Correct 187 ms 202612 KB Output is correct
30 Correct 186 ms 202488 KB Output is correct
31 Correct 1771 ms 272772 KB Output is correct
32 Correct 294 ms 206688 KB Output is correct
33 Correct 1839 ms 273900 KB Output is correct
34 Correct 1771 ms 270784 KB Output is correct
35 Correct 1973 ms 274216 KB Output is correct
36 Correct 2172 ms 276136 KB Output is correct
37 Correct 1416 ms 272084 KB Output is correct
38 Correct 1423 ms 273244 KB Output is correct
39 Correct 1169 ms 271432 KB Output is correct
40 Correct 1226 ms 272608 KB Output is correct
41 Correct 1165 ms 250872 KB Output is correct
42 Correct 1101 ms 252124 KB Output is correct
43 Correct 263 ms 208684 KB Output is correct
44 Correct 1130 ms 250464 KB Output is correct
45 Correct 1186 ms 247324 KB Output is correct
46 Correct 1276 ms 239996 KB Output is correct
47 Correct 791 ms 249180 KB Output is correct
48 Correct 821 ms 250540 KB Output is correct
49 Correct 930 ms 251320 KB Output is correct
50 Correct 941 ms 251828 KB Output is correct
51 Correct 955 ms 249680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4387 ms 409868 KB Output is correct
2 Execution timed out 5057 ms 433808 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5131 ms 421612 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 185 ms 202360 KB Output is correct
2 Correct 188 ms 202264 KB Output is correct
3 Correct 187 ms 202300 KB Output is correct
4 Correct 187 ms 202332 KB Output is correct
5 Correct 188 ms 202384 KB Output is correct
6 Correct 192 ms 202812 KB Output is correct
7 Correct 187 ms 202464 KB Output is correct
8 Correct 189 ms 202608 KB Output is correct
9 Correct 187 ms 202568 KB Output is correct
10 Correct 191 ms 202652 KB Output is correct
11 Correct 189 ms 202580 KB Output is correct
12 Correct 194 ms 202580 KB Output is correct
13 Correct 190 ms 202456 KB Output is correct
14 Correct 204 ms 202692 KB Output is correct
15 Correct 190 ms 202488 KB Output is correct
16 Correct 189 ms 202484 KB Output is correct
17 Correct 190 ms 202572 KB Output is correct
18 Correct 188 ms 202616 KB Output is correct
19 Correct 189 ms 202512 KB Output is correct
20 Correct 188 ms 202616 KB Output is correct
21 Correct 189 ms 202368 KB Output is correct
22 Correct 187 ms 202548 KB Output is correct
23 Correct 190 ms 202520 KB Output is correct
24 Correct 220 ms 202488 KB Output is correct
25 Correct 192 ms 202624 KB Output is correct
26 Correct 188 ms 202488 KB Output is correct
27 Correct 191 ms 202444 KB Output is correct
28 Correct 188 ms 202492 KB Output is correct
29 Correct 187 ms 202612 KB Output is correct
30 Correct 186 ms 202488 KB Output is correct
31 Correct 1771 ms 272772 KB Output is correct
32 Correct 294 ms 206688 KB Output is correct
33 Correct 1839 ms 273900 KB Output is correct
34 Correct 1771 ms 270784 KB Output is correct
35 Correct 1973 ms 274216 KB Output is correct
36 Correct 2172 ms 276136 KB Output is correct
37 Correct 1416 ms 272084 KB Output is correct
38 Correct 1423 ms 273244 KB Output is correct
39 Correct 1169 ms 271432 KB Output is correct
40 Correct 1226 ms 272608 KB Output is correct
41 Correct 1165 ms 250872 KB Output is correct
42 Correct 1101 ms 252124 KB Output is correct
43 Correct 263 ms 208684 KB Output is correct
44 Correct 1130 ms 250464 KB Output is correct
45 Correct 1186 ms 247324 KB Output is correct
46 Correct 1276 ms 239996 KB Output is correct
47 Correct 791 ms 249180 KB Output is correct
48 Correct 821 ms 250540 KB Output is correct
49 Correct 930 ms 251320 KB Output is correct
50 Correct 941 ms 251828 KB Output is correct
51 Correct 955 ms 249680 KB Output is correct
52 Correct 559 ms 231812 KB Output is correct
53 Correct 505 ms 230660 KB Output is correct
54 Correct 969 ms 251972 KB Output is correct
55 Correct 1068 ms 246820 KB Output is correct
56 Correct 818 ms 243916 KB Output is correct
57 Correct 1113 ms 248656 KB Output is correct
58 Correct 921 ms 246184 KB Output is correct
59 Correct 805 ms 243760 KB Output is correct
60 Correct 1046 ms 249268 KB Output is correct
61 Correct 259 ms 209784 KB Output is correct
62 Correct 480 ms 231900 KB Output is correct
63 Correct 771 ms 245924 KB Output is correct
64 Correct 880 ms 249340 KB Output is correct
65 Correct 1030 ms 252880 KB Output is correct
66 Correct 1121 ms 251832 KB Output is correct
67 Correct 388 ms 218136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 202360 KB Output is correct
2 Correct 188 ms 202264 KB Output is correct
3 Correct 187 ms 202300 KB Output is correct
4 Correct 187 ms 202332 KB Output is correct
5 Correct 188 ms 202384 KB Output is correct
6 Correct 192 ms 202812 KB Output is correct
7 Correct 187 ms 202464 KB Output is correct
8 Correct 189 ms 202608 KB Output is correct
9 Correct 187 ms 202568 KB Output is correct
10 Correct 191 ms 202652 KB Output is correct
11 Correct 189 ms 202580 KB Output is correct
12 Correct 194 ms 202580 KB Output is correct
13 Correct 190 ms 202456 KB Output is correct
14 Correct 204 ms 202692 KB Output is correct
15 Correct 190 ms 202488 KB Output is correct
16 Correct 189 ms 202484 KB Output is correct
17 Correct 190 ms 202572 KB Output is correct
18 Correct 188 ms 202616 KB Output is correct
19 Correct 189 ms 202512 KB Output is correct
20 Correct 188 ms 202616 KB Output is correct
21 Correct 189 ms 202368 KB Output is correct
22 Correct 187 ms 202548 KB Output is correct
23 Correct 190 ms 202520 KB Output is correct
24 Correct 220 ms 202488 KB Output is correct
25 Correct 192 ms 202624 KB Output is correct
26 Correct 188 ms 202488 KB Output is correct
27 Correct 191 ms 202444 KB Output is correct
28 Correct 188 ms 202492 KB Output is correct
29 Correct 187 ms 202612 KB Output is correct
30 Correct 186 ms 202488 KB Output is correct
31 Correct 1771 ms 272772 KB Output is correct
32 Correct 294 ms 206688 KB Output is correct
33 Correct 1839 ms 273900 KB Output is correct
34 Correct 1771 ms 270784 KB Output is correct
35 Correct 1973 ms 274216 KB Output is correct
36 Correct 2172 ms 276136 KB Output is correct
37 Correct 1416 ms 272084 KB Output is correct
38 Correct 1423 ms 273244 KB Output is correct
39 Correct 1169 ms 271432 KB Output is correct
40 Correct 1226 ms 272608 KB Output is correct
41 Correct 1165 ms 250872 KB Output is correct
42 Correct 1101 ms 252124 KB Output is correct
43 Correct 263 ms 208684 KB Output is correct
44 Correct 1130 ms 250464 KB Output is correct
45 Correct 1186 ms 247324 KB Output is correct
46 Correct 1276 ms 239996 KB Output is correct
47 Correct 791 ms 249180 KB Output is correct
48 Correct 821 ms 250540 KB Output is correct
49 Correct 930 ms 251320 KB Output is correct
50 Correct 941 ms 251828 KB Output is correct
51 Correct 955 ms 249680 KB Output is correct
52 Correct 4387 ms 409868 KB Output is correct
53 Execution timed out 5057 ms 433808 KB Time limit exceeded
54 Halted 0 ms 0 KB -