Submission #147156

# Submission time Handle Problem Language Result Execution time Memory
147156 2019-08-28T06:16:40 Z jh05013 Bulldozer (JOI17_bulldozer) C++17
100 / 100
846 ms 91432 KB
#include <bits/stdc++.h>
#define entire(x) begin(x),end(x)
#define dbgv(V) {for(auto x:V)cout<<x<<' ';cout<<endl;}
using namespace std;
typedef long long ll;

const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Segnode{
    ll sum, ans, L, R;

    Segnode(): sum{0}, ans{-INF}, L{-INF}, R{-INF} {}
    void operator=(ll x){
        sum = ans = L = R = x;
    }
    void combine(Segnode A, Segnode B){
        sum = A.sum + B.sum;
        L = max(A.L, A.sum + B.L);
        R = max(B.R, B.sum + A.R);
        ans = max({A.ans, B.ans, A.R+B.L});
    }
};

struct Segtree{    
    int n; vector<Segnode> arr;
    Segtree(int sz): n{1} {
        while(n < sz) n<<=1;
        arr.resize(n*2);
    }
    Segnode &operator[](int i){return arr[i+n];}
    void initialize(){for(int i=n-1; i>=0; i--) arr[i].combine(arr[2*i], arr[2*i+1]);}
    void update(int i){for(i+= n; i>>=1;) arr[i].combine(arr[i<<1], arr[i<<1|1]);}
    Segnode query(int l, int r){
        Segnode resl, resr, ans;
        for(l+= n, r+= n+1; l < r; l/= 2, r/= 2){
            if(l&1) resl.combine(resl, arr[l++]);
            if(r&1) resr.combine(arr[--r], resr);
        }
        ans.combine(resl, resr);
        return ans;
    }
};

struct Point{ll x, y;};
struct Frac{
    ll n, d;
    Frac(){Frac(0,0);}
    Frac(ll a, ll b){
        if(b==0) n=abs(a), d=0;
        else if(b<0) n=-a, d=-b;
        else n=a, d=b;
    }
};
bool operator==(const Frac &A, const Frac &B){
    return A.n*B.d == A.d*B.n;
}
bool operator<(const Frac &A, const Frac &B){
    return A.n*B.d < A.d*B.n;
}

vector<Point> P;
const ll fs = -2000000005;
int main(){
    cin.tie(NULL); ios_base::sync_with_stdio(false);
    
    // IO and special case
    int n; cin>>n;
    P.resize(n);
    vector<ll> score(n);
    for(int i=0; i<n; i++) cin>>P[i].x>>P[i].y>>score[i];
    if (n == 1){cout<<max(0LL,score[0]); return 0;}

    // (Slope, intercept, p1, p2)
    vector<tuple<Frac, Frac, int, int>> slope;
    for(int i=0; i<n; i++) for(int j=i+1; j<n; j++){
        Frac a(P[i].y-P[j].y, P[i].x-P[j].x), b;
        if(a.d == 0) b = Frac(P[i].x, 1);
        else b = Frac(P[i].y*a.d - P[i].x*a.n, a.d);
        slope.push_back(make_tuple(a, b, i, j));
    }
    sort(entire(slope));
    slope.push_back(slope[0]); // sentinel

    // Points sorted by y-intercept
    vector<int> C(n);
    iota(entire(C), 0);
    sort(entire(C), [](const int &a, const int &b)
        {return P[a].y-P[a].x*fs < P[b].y-P[b].x*fs;});
    Segtree ST(n);
    for(int i=0; i<n; i++) ST[i] = score[C[i]];
    ST.initialize();
    ll ans = max(0LL, ST.query(0,n-1).ans);

    // Reverse map
    vector<int> rev(n), colinear;
    for(int i=0; i<n; i++) rev[C[i]] = i;

    // Scan
    size_t ss = slope.size()-1; // Excluding sentinel
    for(size_t si=0; si<ss; si++){
        Frac a1,b1; int i1,j1; tie(a1,b1,i1,j1) = slope[si];
        Frac a2,b2; int i2,j2; tie(a2,b2,i2,j2) = slope[si+1];
        colinear.push_back(rev[i1]);
        colinear.push_back(rev[j1]);
        if(si != ss-1 && a1 == a2 && b1 == b2) continue;
        // Invert
        int l = *min_element(entire(colinear));
        int r = *max_element(entire(colinear));
        while(l < r){
            int pl = C[l], pr = C[r];
            ST[l] = score[pr], ST.update(l);
            ST[r] = score[pl], ST.update(r);
            rev[pl] = r, rev[pr] = l;
            swap(C[l], C[r]);
            l++, r--;
        }
        colinear.clear();
        if(si != ss-1 && a1 == a2) continue;
        ll V = ST.query(0,n-1).ans;
        ans = max(ans, V);
    }
    cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 856 KB Output is correct
2 Correct 3 ms 728 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 3 ms 856 KB Output is correct
6 Correct 3 ms 856 KB Output is correct
7 Correct 3 ms 856 KB Output is correct
8 Correct 3 ms 856 KB Output is correct
9 Correct 3 ms 728 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 856 KB Output is correct
2 Correct 4 ms 728 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 4 ms 856 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 4 ms 856 KB Output is correct
9 Correct 4 ms 856 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 296 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 4 ms 856 KB Output is correct
22 Correct 4 ms 856 KB Output is correct
23 Correct 4 ms 856 KB Output is correct
24 Correct 4 ms 856 KB Output is correct
25 Correct 4 ms 728 KB Output is correct
26 Correct 4 ms 856 KB Output is correct
27 Correct 4 ms 856 KB Output is correct
28 Correct 4 ms 856 KB Output is correct
29 Correct 4 ms 728 KB Output is correct
30 Correct 4 ms 856 KB Output is correct
31 Correct 4 ms 728 KB Output is correct
32 Correct 4 ms 728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 856 KB Output is correct
2 Correct 4 ms 728 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 4 ms 856 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 4 ms 856 KB Output is correct
9 Correct 4 ms 856 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 296 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 4 ms 856 KB Output is correct
22 Correct 4 ms 856 KB Output is correct
23 Correct 4 ms 856 KB Output is correct
24 Correct 4 ms 856 KB Output is correct
25 Correct 4 ms 728 KB Output is correct
26 Correct 4 ms 856 KB Output is correct
27 Correct 4 ms 856 KB Output is correct
28 Correct 4 ms 856 KB Output is correct
29 Correct 4 ms 728 KB Output is correct
30 Correct 4 ms 856 KB Output is correct
31 Correct 4 ms 728 KB Output is correct
32 Correct 4 ms 728 KB Output is correct
33 Correct 818 ms 82696 KB Output is correct
34 Correct 822 ms 82692 KB Output is correct
35 Correct 820 ms 82696 KB Output is correct
36 Correct 820 ms 82696 KB Output is correct
37 Correct 834 ms 82700 KB Output is correct
38 Correct 826 ms 82700 KB Output is correct
39 Correct 825 ms 82828 KB Output is correct
40 Correct 846 ms 82816 KB Output is correct
41 Correct 831 ms 82696 KB Output is correct
42 Correct 824 ms 82716 KB Output is correct
43 Correct 816 ms 82824 KB Output is correct
44 Correct 815 ms 82696 KB Output is correct
45 Correct 815 ms 82824 KB Output is correct
46 Correct 812 ms 82692 KB Output is correct
47 Correct 813 ms 82700 KB Output is correct
48 Correct 814 ms 82696 KB Output is correct
49 Correct 819 ms 82768 KB Output is correct
50 Correct 818 ms 82696 KB Output is correct
51 Correct 815 ms 82792 KB Output is correct
52 Correct 827 ms 82748 KB Output is correct
53 Correct 820 ms 82700 KB Output is correct
54 Correct 818 ms 82696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 856 KB Output is correct
2 Correct 4 ms 728 KB Output is correct
3 Correct 4 ms 728 KB Output is correct
4 Correct 4 ms 856 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 4 ms 856 KB Output is correct
9 Correct 4 ms 856 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 296 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 4 ms 856 KB Output is correct
22 Correct 4 ms 856 KB Output is correct
23 Correct 4 ms 856 KB Output is correct
24 Correct 4 ms 856 KB Output is correct
25 Correct 4 ms 728 KB Output is correct
26 Correct 4 ms 856 KB Output is correct
27 Correct 4 ms 856 KB Output is correct
28 Correct 4 ms 856 KB Output is correct
29 Correct 4 ms 728 KB Output is correct
30 Correct 4 ms 856 KB Output is correct
31 Correct 4 ms 728 KB Output is correct
32 Correct 4 ms 728 KB Output is correct
33 Correct 818 ms 82696 KB Output is correct
34 Correct 822 ms 82692 KB Output is correct
35 Correct 820 ms 82696 KB Output is correct
36 Correct 820 ms 82696 KB Output is correct
37 Correct 834 ms 82700 KB Output is correct
38 Correct 826 ms 82700 KB Output is correct
39 Correct 825 ms 82828 KB Output is correct
40 Correct 846 ms 82816 KB Output is correct
41 Correct 831 ms 82696 KB Output is correct
42 Correct 824 ms 82716 KB Output is correct
43 Correct 816 ms 82824 KB Output is correct
44 Correct 815 ms 82696 KB Output is correct
45 Correct 815 ms 82824 KB Output is correct
46 Correct 812 ms 82692 KB Output is correct
47 Correct 813 ms 82700 KB Output is correct
48 Correct 814 ms 82696 KB Output is correct
49 Correct 819 ms 82768 KB Output is correct
50 Correct 818 ms 82696 KB Output is correct
51 Correct 815 ms 82792 KB Output is correct
52 Correct 827 ms 82748 KB Output is correct
53 Correct 820 ms 82700 KB Output is correct
54 Correct 818 ms 82696 KB Output is correct
55 Correct 823 ms 82660 KB Output is correct
56 Correct 817 ms 82696 KB Output is correct
57 Correct 829 ms 82696 KB Output is correct
58 Correct 819 ms 82696 KB Output is correct
59 Correct 823 ms 82708 KB Output is correct
60 Correct 841 ms 82876 KB Output is correct
61 Correct 826 ms 82708 KB Output is correct
62 Correct 823 ms 82696 KB Output is correct
63 Correct 815 ms 82824 KB Output is correct
64 Correct 824 ms 82696 KB Output is correct
65 Correct 815 ms 82696 KB Output is correct
66 Correct 821 ms 82720 KB Output is correct
67 Correct 828 ms 82696 KB Output is correct
68 Correct 823 ms 82696 KB Output is correct
69 Correct 837 ms 82696 KB Output is correct
70 Correct 836 ms 82696 KB Output is correct
71 Correct 835 ms 82852 KB Output is correct
72 Correct 824 ms 82688 KB Output is correct
73 Correct 813 ms 82696 KB Output is correct
74 Correct 820 ms 82752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 856 KB Output is correct
2 Correct 3 ms 728 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 3 ms 856 KB Output is correct
6 Correct 3 ms 856 KB Output is correct
7 Correct 3 ms 856 KB Output is correct
8 Correct 3 ms 856 KB Output is correct
9 Correct 3 ms 728 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 4 ms 856 KB Output is correct
17 Correct 4 ms 728 KB Output is correct
18 Correct 4 ms 728 KB Output is correct
19 Correct 4 ms 856 KB Output is correct
20 Correct 4 ms 860 KB Output is correct
21 Correct 4 ms 856 KB Output is correct
22 Correct 4 ms 768 KB Output is correct
23 Correct 4 ms 856 KB Output is correct
24 Correct 4 ms 856 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 296 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 4 ms 856 KB Output is correct
37 Correct 4 ms 856 KB Output is correct
38 Correct 4 ms 856 KB Output is correct
39 Correct 4 ms 856 KB Output is correct
40 Correct 4 ms 728 KB Output is correct
41 Correct 4 ms 856 KB Output is correct
42 Correct 4 ms 856 KB Output is correct
43 Correct 4 ms 856 KB Output is correct
44 Correct 4 ms 728 KB Output is correct
45 Correct 4 ms 856 KB Output is correct
46 Correct 4 ms 728 KB Output is correct
47 Correct 4 ms 728 KB Output is correct
48 Correct 818 ms 82696 KB Output is correct
49 Correct 822 ms 82692 KB Output is correct
50 Correct 820 ms 82696 KB Output is correct
51 Correct 820 ms 82696 KB Output is correct
52 Correct 834 ms 82700 KB Output is correct
53 Correct 826 ms 82700 KB Output is correct
54 Correct 825 ms 82828 KB Output is correct
55 Correct 846 ms 82816 KB Output is correct
56 Correct 831 ms 82696 KB Output is correct
57 Correct 824 ms 82716 KB Output is correct
58 Correct 816 ms 82824 KB Output is correct
59 Correct 815 ms 82696 KB Output is correct
60 Correct 815 ms 82824 KB Output is correct
61 Correct 812 ms 82692 KB Output is correct
62 Correct 813 ms 82700 KB Output is correct
63 Correct 814 ms 82696 KB Output is correct
64 Correct 819 ms 82768 KB Output is correct
65 Correct 818 ms 82696 KB Output is correct
66 Correct 815 ms 82792 KB Output is correct
67 Correct 827 ms 82748 KB Output is correct
68 Correct 820 ms 82700 KB Output is correct
69 Correct 818 ms 82696 KB Output is correct
70 Correct 823 ms 82660 KB Output is correct
71 Correct 817 ms 82696 KB Output is correct
72 Correct 829 ms 82696 KB Output is correct
73 Correct 819 ms 82696 KB Output is correct
74 Correct 823 ms 82708 KB Output is correct
75 Correct 841 ms 82876 KB Output is correct
76 Correct 826 ms 82708 KB Output is correct
77 Correct 823 ms 82696 KB Output is correct
78 Correct 815 ms 82824 KB Output is correct
79 Correct 824 ms 82696 KB Output is correct
80 Correct 815 ms 82696 KB Output is correct
81 Correct 821 ms 82720 KB Output is correct
82 Correct 828 ms 82696 KB Output is correct
83 Correct 823 ms 82696 KB Output is correct
84 Correct 837 ms 82696 KB Output is correct
85 Correct 836 ms 82696 KB Output is correct
86 Correct 835 ms 82852 KB Output is correct
87 Correct 824 ms 82688 KB Output is correct
88 Correct 813 ms 82696 KB Output is correct
89 Correct 820 ms 82752 KB Output is correct
90 Correct 823 ms 82712 KB Output is correct
91 Correct 817 ms 82692 KB Output is correct
92 Correct 821 ms 82700 KB Output is correct
93 Correct 818 ms 82696 KB Output is correct
94 Correct 826 ms 82692 KB Output is correct
95 Correct 829 ms 82692 KB Output is correct
96 Correct 820 ms 82696 KB Output is correct
97 Correct 823 ms 82696 KB Output is correct
98 Correct 824 ms 82796 KB Output is correct
99 Correct 820 ms 82696 KB Output is correct
100 Correct 674 ms 87272 KB Output is correct
101 Correct 706 ms 86852 KB Output is correct
102 Correct 681 ms 91432 KB Output is correct
103 Correct 678 ms 87076 KB Output is correct
104 Correct 675 ms 86928 KB Output is correct
105 Correct 723 ms 82740 KB Output is correct
106 Correct 724 ms 82996 KB Output is correct
107 Correct 737 ms 82868 KB Output is correct
108 Correct 731 ms 82716 KB Output is correct
109 Correct 747 ms 82996 KB Output is correct
110 Correct 718 ms 82696 KB Output is correct
111 Correct 722 ms 82696 KB Output is correct
112 Correct 722 ms 82776 KB Output is correct
113 Correct 734 ms 82696 KB Output is correct
114 Correct 726 ms 82596 KB Output is correct
115 Correct 724 ms 82624 KB Output is correct
116 Correct 736 ms 82752 KB Output is correct
117 Correct 721 ms 82736 KB Output is correct
118 Correct 716 ms 82568 KB Output is correct
119 Correct 718 ms 82696 KB Output is correct
120 Correct 2 ms 376 KB Output is correct
121 Correct 2 ms 376 KB Output is correct
122 Correct 827 ms 82696 KB Output is correct
123 Correct 817 ms 82636 KB Output is correct
124 Correct 812 ms 82696 KB Output is correct
125 Correct 821 ms 82824 KB Output is correct
126 Correct 831 ms 82664 KB Output is correct
127 Correct 818 ms 82648 KB Output is correct
128 Correct 823 ms 82628 KB Output is correct
129 Correct 815 ms 82640 KB Output is correct
130 Correct 832 ms 82652 KB Output is correct
131 Correct 818 ms 82720 KB Output is correct
132 Correct 831 ms 82620 KB Output is correct
133 Correct 814 ms 82716 KB Output is correct