Submission #60840

# Submission time Handle Problem Language Result Execution time Memory
60840 2018-07-24T20:04:08 Z duality New Home (APIO18_new_home) C++11
57 / 100
5000 ms 351552 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

struct event { int time,x,t; };
bool comp(event a,event b) {
    if (a.time == b.time) return a.t < b.t;
    else return a.time < b.time;
}
vi sorted;
vector<event> events[300000];
multiset<int> S;
multimap<pii,int> M;
vector<pair<pii,pii> > oper;
vpii tree[1 << 20];
int ans[300000];
int update(int s,int e,int as,int ae,int i,pii p) {
    if (as > ae) return 0;
    else if ((s > ae) || (e < as)) return 0;
    else if ((s >= as) && (e <= ae)) {
        tree[i].pb(p);
        return 0;
    }

    int mid = (s+e) / 2;
    update(s,mid,as,ae,2*i+1,p);
    update(mid+1,e,as,ae,2*i+2,p);
    return 0;
}
int query(int s,int e,int q,int i,pii p) {
    if ((s > q) || (e < q)) return 0;
    else if (s == e) {
        tree[i].pb(p);
        return 0;
    }

    int mid = (s+e) / 2;
    query(s,mid,q,2*i+1,p);
    query(mid+1,e,q,2*i+2,p);
    tree[i].pb(p);
    return 0;
}
int solve(int s,int e,int i) {
    int j,k;
    pii m = mp(-3e8,0);
    //sort(tree[i].begin(),tree[i].end());
    for (j = 0; j < tree[i].size(); j++) {
        if (tree[i][j].second >= 0) {
            if (tree[i][j].second > m.second-(tree[i][j].first-m.first)) m = tree[i][j];
        }
        else ans[-tree[i][j].second-1] = max(ans[-tree[i][j].second-1],m.second-(tree[i][j].first-m.first));
    }
    m = mp(3e8,0);
    for (j = tree[i].size()-1; j >= 0; j--) {
        if (tree[i][j].second >= 0) {
            if (tree[i][j].second > m.second-(m.first-tree[i][j].first)) m = tree[i][j];
        }
        else ans[-tree[i][j].second-1] = max(ans[-tree[i][j].second-1],m.second-(m.first-tree[i][j].first));
    }
    if (s != e) {
        int mid = (s+e) / 2;
        solve(s,mid,2*i+1),solve(mid+1,e,2*i+2);
    }
    return 0;
}
int main() {
    int i,j;
    int n,k,q;
    int x,t,a,b,l,y;
    scanf("%d %d %d",&n,&k,&q);
    sorted.pb(0),sorted.pb(3e8);
    for (i = 0; i < n; i++) {
        scanf("%d %d %d %d",&x,&t,&a,&b),t--;
        events[t].pb((event){a,x,1});
        events[t].pb((event){b+1,x,-1});
        sorted.pb(a),sorted.pb(b+1);
    }
    sort(sorted.begin(),sorted.end());
    sorted.resize(unique(sorted.begin(),sorted.end())-sorted.begin());
    for (i = 0; i < k; i++) {
        sort(events[i].begin(),events[i].end(),comp);
        S.insert(-3e8),S.insert(3e8),M.insert(mp(mp(0,6e8),0));
        for (j = 0; j < events[i].size(); j++) {
            events[i][j].time = lower_bound(sorted.begin(),sorted.end(),events[i][j].time)-sorted.begin();
            if (events[i][j].t == 1) {
                auto it = S.lower_bound(events[i][j].x);
                auto it2 = it;
                it2--;
                auto it3 = M.find(mp(*it+*it2,*it-*it2));
                oper.pb(mp(mp(*it+*it2,*it-*it2),mp(it3->second,events[i][j].time-1)));
                //update(0,sorted.size()-2,it3->second,events[i][j].time-1,0,mp(*it+*it2,*it-*it2));
                M.erase(it3);
                M.insert(mp(mp(*it+events[i][j].x,*it-events[i][j].x),events[i][j].time));
                M.insert(mp(mp(events[i][j].x+*it2,events[i][j].x-*it2),events[i][j].time));
                S.insert(events[i][j].x);
            }
            else {
                auto it = S.lower_bound(events[i][j].x);
                auto it2 = it;
                it2--;
                auto it3 = M.find(mp(*it+*it2,*it-*it2));
                oper.pb(mp(mp(*it+*it2,*it-*it2),mp(it3->second,events[i][j].time-1)));
                //update(0,sorted.size()-2,it3->second,events[i][j].time-1,0,mp(*it+*it2,*it-*it2));
                M.erase(it3);
                it++,it2++;
                it3 = M.find(mp(*it+*it2,*it-*it2));
                oper.pb(mp(mp(*it+*it2,*it-*it2),mp(it3->second,events[i][j].time-1)));
                //update(0,sorted.size()-2,it3->second,events[i][j].time-1,0,mp(*it+*it2,*it-*it2));
                M.erase(it3);
                it2--;
                M.insert(mp(mp(*it+*it2,*it-*it2),events[i][j].time));
                S.erase(--it);
            }
        }
        for (auto it = M.begin(); it != M.end(); it++) {
            oper.pb(mp(it->first,mp(it->second,sorted.size()-2)));
            //update(0,sorted.size()-2,it->second,sorted.size()-2,0,it->first);
        }
        S.clear(),M.clear();
    }
    for (i = 0; i < q; i++) {
        scanf("%d %d",&l,&y);
        int p = upper_bound(sorted.begin(),sorted.end(),y)-sorted.begin()-1;
        oper.pb(mp(mp(2*l,-(i+1)),mp(p,-1)));
        //query(0,sorted.size()-2,p,0,mp(2*l,-(i+1)));
    }
    sort(oper.begin(),oper.end());
    for (i = 0; i < oper.size(); i++) {
        if (oper[i].second.second == -1) query(0,sorted.size()-2,oper[i].second.first,0,oper[i].first);
        else update(0,sorted.size()-2,oper[i].second.first,oper[i].second.second,0,oper[i].first);
    }
    solve(0,sorted.size()-2,0);
    for (i = 0; i < q; i++) printf("%d\n",(ans[i] > 3e8) ? -1:ans[i]/2);

    return 0;
}

Compilation message

new_home.cpp: In function 'int solve(int, int, int)':
new_home.cpp:100:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j = 0; j < tree[i].size(); j++) {
                 ~~^~~~~~~~~~~~~~~~
new_home.cpp:97:11: warning: unused variable 'k' [-Wunused-variable]
     int j,k;
           ^
new_home.cpp: In function 'int main()':
new_home.cpp:136:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < events[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~~
new_home.cpp:181:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < oper.size(); i++) {
                 ~~^~~~~~~~~~~~~
new_home.cpp:123: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:126:41: 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),t--;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
new_home.cpp:175:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&l,&y);
         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31956 KB Output is correct
2 Correct 39 ms 32104 KB Output is correct
3 Correct 38 ms 32104 KB Output is correct
4 Correct 43 ms 32216 KB Output is correct
5 Correct 31 ms 32372 KB Output is correct
6 Correct 33 ms 32372 KB Output is correct
7 Correct 33 ms 32444 KB Output is correct
8 Correct 32 ms 32496 KB Output is correct
9 Correct 35 ms 32496 KB Output is correct
10 Correct 40 ms 32496 KB Output is correct
11 Correct 40 ms 32496 KB Output is correct
12 Correct 32 ms 32496 KB Output is correct
13 Correct 32 ms 32496 KB Output is correct
14 Correct 32 ms 32496 KB Output is correct
15 Correct 43 ms 32496 KB Output is correct
16 Correct 35 ms 32496 KB Output is correct
17 Correct 38 ms 32496 KB Output is correct
18 Correct 34 ms 32496 KB Output is correct
19 Correct 33 ms 32496 KB Output is correct
20 Correct 40 ms 32496 KB Output is correct
21 Correct 31 ms 32496 KB Output is correct
22 Correct 33 ms 32496 KB Output is correct
23 Correct 34 ms 32496 KB Output is correct
24 Correct 32 ms 32496 KB Output is correct
25 Correct 33 ms 32496 KB Output is correct
26 Correct 32 ms 32496 KB Output is correct
27 Correct 31 ms 32496 KB Output is correct
28 Correct 31 ms 32496 KB Output is correct
29 Correct 32 ms 32496 KB Output is correct
30 Correct 34 ms 32496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31956 KB Output is correct
2 Correct 39 ms 32104 KB Output is correct
3 Correct 38 ms 32104 KB Output is correct
4 Correct 43 ms 32216 KB Output is correct
5 Correct 31 ms 32372 KB Output is correct
6 Correct 33 ms 32372 KB Output is correct
7 Correct 33 ms 32444 KB Output is correct
8 Correct 32 ms 32496 KB Output is correct
9 Correct 35 ms 32496 KB Output is correct
10 Correct 40 ms 32496 KB Output is correct
11 Correct 40 ms 32496 KB Output is correct
12 Correct 32 ms 32496 KB Output is correct
13 Correct 32 ms 32496 KB Output is correct
14 Correct 32 ms 32496 KB Output is correct
15 Correct 43 ms 32496 KB Output is correct
16 Correct 35 ms 32496 KB Output is correct
17 Correct 38 ms 32496 KB Output is correct
18 Correct 34 ms 32496 KB Output is correct
19 Correct 33 ms 32496 KB Output is correct
20 Correct 40 ms 32496 KB Output is correct
21 Correct 31 ms 32496 KB Output is correct
22 Correct 33 ms 32496 KB Output is correct
23 Correct 34 ms 32496 KB Output is correct
24 Correct 32 ms 32496 KB Output is correct
25 Correct 33 ms 32496 KB Output is correct
26 Correct 32 ms 32496 KB Output is correct
27 Correct 31 ms 32496 KB Output is correct
28 Correct 31 ms 32496 KB Output is correct
29 Correct 32 ms 32496 KB Output is correct
30 Correct 34 ms 32496 KB Output is correct
31 Correct 1165 ms 81324 KB Output is correct
32 Correct 222 ms 81324 KB Output is correct
33 Correct 1134 ms 81324 KB Output is correct
34 Correct 955 ms 81324 KB Output is correct
35 Correct 986 ms 81324 KB Output is correct
36 Correct 887 ms 81324 KB Output is correct
37 Correct 703 ms 81324 KB Output is correct
38 Correct 739 ms 81324 KB Output is correct
39 Correct 525 ms 81324 KB Output is correct
40 Correct 517 ms 81324 KB Output is correct
41 Correct 928 ms 81324 KB Output is correct
42 Correct 899 ms 81324 KB Output is correct
43 Correct 160 ms 81324 KB Output is correct
44 Correct 911 ms 81324 KB Output is correct
45 Correct 773 ms 81324 KB Output is correct
46 Correct 680 ms 81324 KB Output is correct
47 Correct 421 ms 81324 KB Output is correct
48 Correct 360 ms 81324 KB Output is correct
49 Correct 405 ms 81324 KB Output is correct
50 Correct 512 ms 81324 KB Output is correct
51 Correct 473 ms 81324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 972 ms 89920 KB Output is correct
2 Correct 1728 ms 89920 KB Output is correct
3 Correct 943 ms 95868 KB Output is correct
4 Correct 1018 ms 95868 KB Output is correct
5 Correct 2988 ms 95992 KB Output is correct
6 Correct 2141 ms 95992 KB Output is correct
7 Correct 804 ms 96080 KB Output is correct
8 Correct 812 ms 96080 KB Output is correct
9 Correct 944 ms 96080 KB Output is correct
10 Correct 1012 ms 96080 KB Output is correct
11 Correct 1073 ms 96080 KB Output is correct
12 Correct 1002 ms 96080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4554 ms 252756 KB Output is correct
2 Correct 1311 ms 252756 KB Output is correct
3 Correct 4692 ms 261988 KB Output is correct
4 Correct 4436 ms 351552 KB Output is correct
5 Correct 4053 ms 351552 KB Output is correct
6 Correct 4018 ms 351552 KB Output is correct
7 Execution timed out 5038 ms 351552 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31956 KB Output is correct
2 Correct 39 ms 32104 KB Output is correct
3 Correct 38 ms 32104 KB Output is correct
4 Correct 43 ms 32216 KB Output is correct
5 Correct 31 ms 32372 KB Output is correct
6 Correct 33 ms 32372 KB Output is correct
7 Correct 33 ms 32444 KB Output is correct
8 Correct 32 ms 32496 KB Output is correct
9 Correct 35 ms 32496 KB Output is correct
10 Correct 40 ms 32496 KB Output is correct
11 Correct 40 ms 32496 KB Output is correct
12 Correct 32 ms 32496 KB Output is correct
13 Correct 32 ms 32496 KB Output is correct
14 Correct 32 ms 32496 KB Output is correct
15 Correct 43 ms 32496 KB Output is correct
16 Correct 35 ms 32496 KB Output is correct
17 Correct 38 ms 32496 KB Output is correct
18 Correct 34 ms 32496 KB Output is correct
19 Correct 33 ms 32496 KB Output is correct
20 Correct 40 ms 32496 KB Output is correct
21 Correct 31 ms 32496 KB Output is correct
22 Correct 33 ms 32496 KB Output is correct
23 Correct 34 ms 32496 KB Output is correct
24 Correct 32 ms 32496 KB Output is correct
25 Correct 33 ms 32496 KB Output is correct
26 Correct 32 ms 32496 KB Output is correct
27 Correct 31 ms 32496 KB Output is correct
28 Correct 31 ms 32496 KB Output is correct
29 Correct 32 ms 32496 KB Output is correct
30 Correct 34 ms 32496 KB Output is correct
31 Correct 1165 ms 81324 KB Output is correct
32 Correct 222 ms 81324 KB Output is correct
33 Correct 1134 ms 81324 KB Output is correct
34 Correct 955 ms 81324 KB Output is correct
35 Correct 986 ms 81324 KB Output is correct
36 Correct 887 ms 81324 KB Output is correct
37 Correct 703 ms 81324 KB Output is correct
38 Correct 739 ms 81324 KB Output is correct
39 Correct 525 ms 81324 KB Output is correct
40 Correct 517 ms 81324 KB Output is correct
41 Correct 928 ms 81324 KB Output is correct
42 Correct 899 ms 81324 KB Output is correct
43 Correct 160 ms 81324 KB Output is correct
44 Correct 911 ms 81324 KB Output is correct
45 Correct 773 ms 81324 KB Output is correct
46 Correct 680 ms 81324 KB Output is correct
47 Correct 421 ms 81324 KB Output is correct
48 Correct 360 ms 81324 KB Output is correct
49 Correct 405 ms 81324 KB Output is correct
50 Correct 512 ms 81324 KB Output is correct
51 Correct 473 ms 81324 KB Output is correct
52 Correct 939 ms 351552 KB Output is correct
53 Correct 905 ms 351552 KB Output is correct
54 Correct 1161 ms 351552 KB Output is correct
55 Correct 862 ms 351552 KB Output is correct
56 Correct 862 ms 351552 KB Output is correct
57 Correct 954 ms 351552 KB Output is correct
58 Correct 920 ms 351552 KB Output is correct
59 Correct 847 ms 351552 KB Output is correct
60 Correct 895 ms 351552 KB Output is correct
61 Correct 200 ms 351552 KB Output is correct
62 Correct 922 ms 351552 KB Output is correct
63 Correct 961 ms 351552 KB Output is correct
64 Correct 926 ms 351552 KB Output is correct
65 Correct 1006 ms 351552 KB Output is correct
66 Correct 991 ms 351552 KB Output is correct
67 Correct 198 ms 351552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31956 KB Output is correct
2 Correct 39 ms 32104 KB Output is correct
3 Correct 38 ms 32104 KB Output is correct
4 Correct 43 ms 32216 KB Output is correct
5 Correct 31 ms 32372 KB Output is correct
6 Correct 33 ms 32372 KB Output is correct
7 Correct 33 ms 32444 KB Output is correct
8 Correct 32 ms 32496 KB Output is correct
9 Correct 35 ms 32496 KB Output is correct
10 Correct 40 ms 32496 KB Output is correct
11 Correct 40 ms 32496 KB Output is correct
12 Correct 32 ms 32496 KB Output is correct
13 Correct 32 ms 32496 KB Output is correct
14 Correct 32 ms 32496 KB Output is correct
15 Correct 43 ms 32496 KB Output is correct
16 Correct 35 ms 32496 KB Output is correct
17 Correct 38 ms 32496 KB Output is correct
18 Correct 34 ms 32496 KB Output is correct
19 Correct 33 ms 32496 KB Output is correct
20 Correct 40 ms 32496 KB Output is correct
21 Correct 31 ms 32496 KB Output is correct
22 Correct 33 ms 32496 KB Output is correct
23 Correct 34 ms 32496 KB Output is correct
24 Correct 32 ms 32496 KB Output is correct
25 Correct 33 ms 32496 KB Output is correct
26 Correct 32 ms 32496 KB Output is correct
27 Correct 31 ms 32496 KB Output is correct
28 Correct 31 ms 32496 KB Output is correct
29 Correct 32 ms 32496 KB Output is correct
30 Correct 34 ms 32496 KB Output is correct
31 Correct 1165 ms 81324 KB Output is correct
32 Correct 222 ms 81324 KB Output is correct
33 Correct 1134 ms 81324 KB Output is correct
34 Correct 955 ms 81324 KB Output is correct
35 Correct 986 ms 81324 KB Output is correct
36 Correct 887 ms 81324 KB Output is correct
37 Correct 703 ms 81324 KB Output is correct
38 Correct 739 ms 81324 KB Output is correct
39 Correct 525 ms 81324 KB Output is correct
40 Correct 517 ms 81324 KB Output is correct
41 Correct 928 ms 81324 KB Output is correct
42 Correct 899 ms 81324 KB Output is correct
43 Correct 160 ms 81324 KB Output is correct
44 Correct 911 ms 81324 KB Output is correct
45 Correct 773 ms 81324 KB Output is correct
46 Correct 680 ms 81324 KB Output is correct
47 Correct 421 ms 81324 KB Output is correct
48 Correct 360 ms 81324 KB Output is correct
49 Correct 405 ms 81324 KB Output is correct
50 Correct 512 ms 81324 KB Output is correct
51 Correct 473 ms 81324 KB Output is correct
52 Correct 972 ms 89920 KB Output is correct
53 Correct 1728 ms 89920 KB Output is correct
54 Correct 943 ms 95868 KB Output is correct
55 Correct 1018 ms 95868 KB Output is correct
56 Correct 2988 ms 95992 KB Output is correct
57 Correct 2141 ms 95992 KB Output is correct
58 Correct 804 ms 96080 KB Output is correct
59 Correct 812 ms 96080 KB Output is correct
60 Correct 944 ms 96080 KB Output is correct
61 Correct 1012 ms 96080 KB Output is correct
62 Correct 1073 ms 96080 KB Output is correct
63 Correct 1002 ms 96080 KB Output is correct
64 Correct 4554 ms 252756 KB Output is correct
65 Correct 1311 ms 252756 KB Output is correct
66 Correct 4692 ms 261988 KB Output is correct
67 Correct 4436 ms 351552 KB Output is correct
68 Correct 4053 ms 351552 KB Output is correct
69 Correct 4018 ms 351552 KB Output is correct
70 Execution timed out 5038 ms 351552 KB Time limit exceeded
71 Halted 0 ms 0 KB -