Submission #222506

# Submission time Handle Problem Language Result Execution time Memory
222506 2020-04-13T08:30:47 Z Fshoaib Just Long Neckties (JOI20_ho_t1) C++11
100 / 100
263 ms 41720 KB
#include "bits/stdc++.h"
using namespace std;
/*ios_base::sync_with_stdio(false);
    cin.tie(NULL);*/
    
typedef long long ll;
struct node {
    int s, e;
    ll mn, mx, sum;
    bool lset;
    ll add_val, set_val;
    node *l, *r;
    node (int _s, int _e, int A[] = NULL): s(_s), e(_e), mn(0), mx(0), sum(0), lset(0), add_val(0), set_val(0), l(NULL), r(NULL) {
        if (A == NULL) return;
        if (s == e) mn = mx = sum = A[s];
        else {
            l = new node(s, (s+e)>>1, A), r = new node((s+e+2)>>1, e, A);
            combine();
        }
    }
    void create_children() {
        if (s == e) return;
        if (l != NULL) return;
        int m = (s+e)>>1;
        l = new node(s, m);
        r = new node(m+1, e);
    }
    void self_set(ll v) {
        lset = 1;
        mn = mx = set_val = v;
        sum = v * (e-s+1);
        add_val = 0;
    }
    void self_add(ll v) {
        if (lset) { self_set(v + set_val); return; }
        mn += v, mx += v, add_val += v;
        sum += v*(e-s+1);
    }
    void lazy_propagate() {
        if (s == e) return;
        if (lset) {
            l->self_set(set_val), r->self_set(set_val);
            lset = set_val = 0;
        }   
        if (add_val != 0) {
            l->self_add(add_val), r->self_add(add_val);
            add_val = 0;
        }
    }
    void combine() {
        if (l == NULL) return;
        sum = l->sum + r->sum;
        mn = min(l->mn, r->mn);
        mx = max(l->mx, r->mx);
    }
    void add(int x, int y, ll v) {
        if (s == x && e == y) { self_add(v); return; }
        int m = (s+e)>>1;
        create_children(); lazy_propagate();
        if (x <= m) l->add(x, min(y, m), v);
        if (y > m) r->add(max(x, m+1), y, v);
        combine();
    }
    void set(int x, int y, ll v) {
        if (s == x && e == y) { self_set(v); return; }
        int m = (s+e)>>1;
        create_children(); lazy_propagate();
        if (x <= m) l->set(x, min(y, m), v);
        if (y > m) r->set(max(x, m+1), y, v);
        combine();
    }
    ll range_sum(int x, int y) {
        if (s == x && e == y) return sum;
        if (l == NULL || lset) return (sum / (e-s+1)) * (y-x+1);
        int m = (s+e)>>1;
        lazy_propagate();
        if (y <= m) return l->range_sum(x, y);
        if (x > m) return r->range_sum(x, y);
        return l->range_sum(x, m) + r->range_sum(m+1, y);
    }
    ll range_min(int x, int y) {
        if (s == x && e == y) return mn;
        if (l == NULL || lset) return mn;
        int m = (s+e)>>1;
        lazy_propagate();
        if (y <= m) return l->range_min(x, y);
        if (x > m) return r->range_min(x, y);
        return min(l->range_min(x, m), r->range_min(m+1, y));
    }
    ll range_max(int x, int y) {
        if (s == x && e == y) return mx;
        if (l == NULL || lset) return mx;
        int m = (s+e)>>1;
        lazy_propagate();
        if (y <= m) return l->range_max(x, y);
        if (x > m) return r->range_max(x, y);
        return max(l->range_max(x, m), r->range_max(m+1, y));
    }
    ~node() {
        if (l != NULL) delete l;
        if (r != NULL) delete r;
    }
} *root;
 
 
int main( )
{	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll N; 
	cin >> N; 
	root = new node(0, N + 5);
	root->set(0, N+3, LLONG_MAX - 3); 
	pair<ll,ll> ties[N + 1]; // tie and original order 
	ll emp[N]; 
	for(int i=0;i<=N;i++){
		ll a; 
		cin >> a;
		ties[i] = make_pair(a,i);  
	}
	sort(ties,ties + N + 1); // smallest tie and index, answer goes at tie[i].second
	for(int i=0;i<N;i++){
		cin >> emp[i]; 
	}
	sort(emp,emp+N); 
	
	ll shuff[N+1]; 
	for(int i=0;i<=N;i++){
		shuff[i] = ties[i].second; 
	}
	
	ll ans[N + 1];
	
	
	for(int i=0;i<N;i++){
		ll calc = (ties[i+1].first - emp[i]); 
		if (calc < 0){
			calc = 0; 
		}
		root->set(i,i,calc); 
	}
	ll m = root->range_max(0, N-1); 
	ans[shuff[0]] = m; 
	
	for(int i=0;i<N;i++){
		ll calc = (ties[i].first - emp[i]); 
		if (calc < 0){
			calc = 0; 
		} 
		root->set(i,i,calc);  
		ll an = root->range_max(0, N-1); 
		ans[shuff[i+1]] = an; 
	}
	for(int i=0;i<=N;i++){
		cout << ans[i] << " "; 
	}
	cout << endl; 
   
}

Compilation message

ho_t1.cpp: In member function 'void node::lazy_propagate()':
ho_t1.cpp:43:28: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             lset = set_val = 0;
                    ~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 768 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 11 ms 640 KB Output is correct
20 Correct 11 ms 640 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 6 ms 640 KB Output is correct
25 Correct 7 ms 768 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 768 KB Output is correct
28 Correct 11 ms 768 KB Output is correct
29 Correct 6 ms 768 KB Output is correct
30 Correct 6 ms 768 KB Output is correct
31 Correct 7 ms 768 KB Output is correct
32 Correct 7 ms 740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 768 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 11 ms 640 KB Output is correct
20 Correct 11 ms 640 KB Output is correct
21 Correct 6 ms 640 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 6 ms 640 KB Output is correct
25 Correct 7 ms 768 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 768 KB Output is correct
28 Correct 11 ms 768 KB Output is correct
29 Correct 6 ms 768 KB Output is correct
30 Correct 6 ms 768 KB Output is correct
31 Correct 7 ms 768 KB Output is correct
32 Correct 7 ms 740 KB Output is correct
33 Correct 233 ms 39544 KB Output is correct
34 Correct 228 ms 41208 KB Output is correct
35 Correct 236 ms 39800 KB Output is correct
36 Correct 263 ms 40464 KB Output is correct
37 Correct 228 ms 41720 KB Output is correct
38 Correct 240 ms 41336 KB Output is correct
39 Correct 228 ms 39928 KB Output is correct
40 Correct 233 ms 39800 KB Output is correct
41 Correct 223 ms 40184 KB Output is correct
42 Correct 231 ms 40072 KB Output is correct
43 Correct 236 ms 39932 KB Output is correct
44 Correct 221 ms 38904 KB Output is correct
45 Correct 227 ms 39288 KB Output is correct
46 Correct 226 ms 38904 KB Output is correct
47 Correct 234 ms 40380 KB Output is correct
48 Correct 215 ms 40440 KB Output is correct
49 Correct 232 ms 41080 KB Output is correct
50 Correct 233 ms 41080 KB Output is correct
51 Correct 243 ms 41080 KB Output is correct
52 Correct 236 ms 41080 KB Output is correct
53 Correct 229 ms 41080 KB Output is correct