Submission #314987

# Submission time Handle Problem Language Result Execution time Memory
314987 2020-10-21T20:59:47 Z aZvezda Fire (JOI20_ho_t5) C++14
100 / 100
905 ms 78780 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const ll MAX_N = 2e5 + 10, LOG = 20;

struct SegTree {
	ll tree[4 * MAX_N];
	void upd(ll curr, ll l, ll r, ll ind, ll val) {
		if(l == r && l == ind) {
			tree[curr] = val;
			return;
		} else if(r < ind || ind < l) {return;}
		ll m = (l + r) / 2ll;
		upd(curr * 2, l, m, ind, val);
		upd(curr * 2 + 1, m + 1, r, ind, val);
		tree[curr] = tree[curr * 2] + tree[curr * 2 + 1];
	}
	ll ans(ll curr, ll l, ll r, ll ql, ll qr) {
		if(ql <= l && r <= qr) {
			return tree[curr];
		} else if(l > qr || r < ql) {
			return 0;
		}
		ll m = (l + r) / 2ll;
		return ans(curr * 2, l, m, ql, qr) + ans(curr * 2 + 1, m + 1, r, ql, qr);
	}
};
	  
struct SparseTable {
    ll ans[LOG][MAX_N], arr[MAX_N];
    void construct(ll n, ll (&_arr)[MAX_N]) {
        for(ll i = 0; i < n; i ++) {arr[i] = _arr[i]; ans[0][i] = i;}
        for(ll j = 1; j < LOG; j ++) {
            for(ll i = 0; i + (1 << j) <= n; i ++) {
            	if(arr[ans[j - 1][i]] > arr[ans[j - 1][i + (1 << (j - 1))]]) {
					ans[j][i] = ans[j - 1][i];            		
            	} else {
            		ans[j][i] = ans[j - 1][i + (1 << (j - 1))];
            	}
            }
        }
    }
    ll query(ll l, ll r) {
        ll q = 0;
        while((1 << q) <= r - l + 1) {q ++;} q --;
        if(arr[ans[q][l]] > arr[ans[q][r - (1 << q) + 1]]) {
        	return ans[q][l];
        } else {
        	return ans[q][r - (1 << q) + 1];	
        }
    }
};

SegTree delta, m;
SparseTable sTable;
ll arr[MAX_N];
ll lft[MAX_N], rght[MAX_N];
ll n, q;
vector<pair<ll, ll> > events[MAX_N];
vector<pair<pair<ll, ll>, ll> > queries[MAX_N];
ll ans[MAX_N];

ll getAns(ll t, ll r) {
	if(r == -1) {return 0;}
	ll indCurr = sTable.query(max(0ll, r - t + 1), r);
	ll begCurr = max(indCurr, lft[indCurr] + t);
	ll cumm = (r - begCurr + 1) * arr[indCurr];
	if(indCurr == -1) {return cumm;}
	//cout << "Asking " << t << " " << r << " " << indCurr << " " << cumm << endl;
	//cout << delta.ans(1, 0, n - 1, 0, indCurr - 1) * t << " " << m.ans(1, 0, n - 1, 0, indCurr - 1) << endl;
	return delta.ans(1, 0, n - 1, 0, indCurr - 1) * t + m.ans(1, 0, n - 1, 0, indCurr - 1) + cumm;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> q;
	for(ll i = 0; i < n; i ++) {
		cin >> arr[i];
	}	
	sTable.construct(n, arr);
	stack<pair<ll, ll> > st;
	st.push({mod, -mod});
	for(ll i = 0; i < n; i ++) {
		while(!st.empty() && st.top().first <= arr[i]) {
			st.pop();
		}
		lft[i] = st.top().second;
		st.push({arr[i], i});
	}
	while(!st.empty()) {st.pop();}	
	st.push({mod, n});
	for(ll i = n - 1; i >= 0; i --) {
		while(!st.empty() && st.top().first < arr[i]) {
			st.pop();
		}
		rght[i] = st.top().second;
		st.push({arr[i], i});
	}
	for(ll i = 0; i < n; i ++) {
		ll tmeRght = rght[i] - i;
		if(tmeRght < MAX_N) {
			events[tmeRght].push_back({i, 0}); 
		}
		
		ll tmeLft = i - lft[i];
		if(tmeLft < MAX_N) {
			events[tmeLft].push_back({i, 1});
		}
		
		ll tmeFull = rght[i] - lft[i];
		if(tmeFull < MAX_N) {
			events[tmeFull].push_back({i, 2});
		}
	}
	
	for(ll i = 0; i < q; i ++) {
		ll l, r, t;
		cin >> t >> l >> r;
		queries[t].push_back({{l - 1, r - 1}, i});
	}
	for(ll i = 0; i < n; i ++) {
		delta.upd(1, 0, n - 1, i, arr[i]);
	}
	for(ll tme = 1; tme <= n; tme ++) {
		for(auto it : events[tme]) {
			if(it.second == 0 || it.second == 1) {
				ll nowDelta = delta.ans(1, 0, n - 1, it.first, it.first);
				ll nowm = m.ans(1, 0, n - 1, it.first, it.first);
				if(nowDelta == 0) {
					m.upd(1, 0, n - 1, it.first, nowDelta * (tme + 1) + nowm + arr[it.first] * tme);
				} else {
					m.upd(1, 0, n - 1, it.first, nowDelta * tme + nowm);					
				}
				delta.upd(1, 0, n - 1, it.first, nowDelta - arr[it.first]);
			} else {
				m.upd(1, 0, n - 1, it.first, 0);
				delta.upd(1, 0, n - 1, it.first, 0);
			}
		}
		for(auto it : queries[tme]) {
			ans[it.second] = getAns(tme + 1, it.first.second) - getAns(tme + 1, it.first.first - 1);
		}
		continue;
		
		cout << out(tme) << " " << endl;
		for(ll i = 0; i < n; i ++) {cout << delta.ans(1, 0, n - 1, i, i) << " ";} cout << endl;
		for(ll i = 0; i < n; i ++) {cout << m.ans(1, 0, n - 1, i, i) << " ";} cout << endl;			
	}
	
	for(ll i = 0; i < q; i ++) {
		cout << ans[i] << endl;
	}
	return 0;
	
}

# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 7 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 8 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9856 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 8 ms 9984 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 7 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 8 ms 9960 KB Output is correct
22 Correct 9 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 8 ms 9856 KB Output is correct
26 Correct 7 ms 9856 KB Output is correct
27 Correct 7 ms 9856 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 8 ms 9856 KB Output is correct
31 Correct 7 ms 9856 KB Output is correct
32 Correct 8 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 710 ms 72720 KB Output is correct
3 Correct 721 ms 77120 KB Output is correct
4 Correct 718 ms 77432 KB Output is correct
5 Correct 731 ms 78396 KB Output is correct
6 Correct 726 ms 77460 KB Output is correct
7 Correct 721 ms 77944 KB Output is correct
8 Correct 750 ms 78780 KB Output is correct
9 Correct 729 ms 78448 KB Output is correct
10 Correct 678 ms 76848 KB Output is correct
11 Correct 728 ms 78764 KB Output is correct
12 Correct 718 ms 76864 KB Output is correct
13 Correct 727 ms 78332 KB Output is correct
14 Correct 772 ms 78136 KB Output is correct
15 Correct 757 ms 78664 KB Output is correct
16 Correct 724 ms 77376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 791 ms 72524 KB Output is correct
3 Correct 777 ms 74192 KB Output is correct
4 Correct 780 ms 76448 KB Output is correct
5 Correct 770 ms 74428 KB Output is correct
6 Correct 778 ms 75296 KB Output is correct
7 Correct 779 ms 75460 KB Output is correct
8 Correct 780 ms 74960 KB Output is correct
9 Correct 763 ms 74308 KB Output is correct
10 Correct 773 ms 73804 KB Output is correct
11 Correct 796 ms 76040 KB Output is correct
12 Correct 769 ms 75328 KB Output is correct
13 Correct 831 ms 75776 KB Output is correct
14 Correct 769 ms 74560 KB Output is correct
15 Correct 800 ms 75928 KB Output is correct
16 Correct 782 ms 75460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 849 ms 66372 KB Output is correct
2 Correct 851 ms 70272 KB Output is correct
3 Correct 868 ms 71472 KB Output is correct
4 Correct 864 ms 70064 KB Output is correct
5 Correct 853 ms 70468 KB Output is correct
6 Correct 839 ms 70752 KB Output is correct
7 Correct 856 ms 71248 KB Output is correct
8 Correct 852 ms 70880 KB Output is correct
9 Correct 854 ms 70232 KB Output is correct
10 Correct 848 ms 70948 KB Output is correct
11 Correct 865 ms 70576 KB Output is correct
12 Correct 905 ms 70788 KB Output is correct
13 Correct 899 ms 70344 KB Output is correct
14 Correct 874 ms 70668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 7 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 8 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9856 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 8 ms 9984 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 7 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 8 ms 9960 KB Output is correct
22 Correct 9 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 8 ms 9856 KB Output is correct
26 Correct 7 ms 9856 KB Output is correct
27 Correct 7 ms 9856 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 8 ms 9856 KB Output is correct
31 Correct 7 ms 9856 KB Output is correct
32 Correct 8 ms 9856 KB Output is correct
33 Correct 799 ms 74052 KB Output is correct
34 Correct 809 ms 77004 KB Output is correct
35 Correct 817 ms 76712 KB Output is correct
36 Correct 790 ms 75724 KB Output is correct
37 Correct 781 ms 75588 KB Output is correct
38 Correct 800 ms 76112 KB Output is correct
39 Correct 783 ms 75940 KB Output is correct
40 Correct 796 ms 75660 KB Output is correct
41 Correct 799 ms 76608 KB Output is correct
42 Correct 797 ms 75716 KB Output is correct
43 Correct 766 ms 78096 KB Output is correct
44 Correct 755 ms 77888 KB Output is correct
45 Correct 737 ms 76232 KB Output is correct
46 Correct 767 ms 77828 KB Output is correct
47 Correct 744 ms 76788 KB Output is correct
48 Correct 726 ms 75980 KB Output is correct
49 Correct 745 ms 76872 KB Output is correct
50 Correct 777 ms 78396 KB Output is correct
51 Correct 767 ms 78148 KB Output is correct
52 Correct 754 ms 77104 KB Output is correct
53 Correct 771 ms 76976 KB Output is correct
54 Correct 778 ms 76108 KB Output is correct
55 Correct 783 ms 76824 KB Output is correct
56 Correct 793 ms 77260 KB Output is correct
57 Correct 788 ms 76488 KB Output is correct
58 Correct 798 ms 77632 KB Output is correct
59 Correct 710 ms 72720 KB Output is correct
60 Correct 721 ms 77120 KB Output is correct
61 Correct 718 ms 77432 KB Output is correct
62 Correct 731 ms 78396 KB Output is correct
63 Correct 726 ms 77460 KB Output is correct
64 Correct 721 ms 77944 KB Output is correct
65 Correct 750 ms 78780 KB Output is correct
66 Correct 729 ms 78448 KB Output is correct
67 Correct 678 ms 76848 KB Output is correct
68 Correct 728 ms 78764 KB Output is correct
69 Correct 718 ms 76864 KB Output is correct
70 Correct 727 ms 78332 KB Output is correct
71 Correct 772 ms 78136 KB Output is correct
72 Correct 757 ms 78664 KB Output is correct
73 Correct 724 ms 77376 KB Output is correct
74 Correct 791 ms 72524 KB Output is correct
75 Correct 777 ms 74192 KB Output is correct
76 Correct 780 ms 76448 KB Output is correct
77 Correct 770 ms 74428 KB Output is correct
78 Correct 778 ms 75296 KB Output is correct
79 Correct 779 ms 75460 KB Output is correct
80 Correct 780 ms 74960 KB Output is correct
81 Correct 763 ms 74308 KB Output is correct
82 Correct 773 ms 73804 KB Output is correct
83 Correct 796 ms 76040 KB Output is correct
84 Correct 769 ms 75328 KB Output is correct
85 Correct 831 ms 75776 KB Output is correct
86 Correct 769 ms 74560 KB Output is correct
87 Correct 800 ms 75928 KB Output is correct
88 Correct 782 ms 75460 KB Output is correct
89 Correct 849 ms 66372 KB Output is correct
90 Correct 851 ms 70272 KB Output is correct
91 Correct 868 ms 71472 KB Output is correct
92 Correct 864 ms 70064 KB Output is correct
93 Correct 853 ms 70468 KB Output is correct
94 Correct 839 ms 70752 KB Output is correct
95 Correct 856 ms 71248 KB Output is correct
96 Correct 852 ms 70880 KB Output is correct
97 Correct 854 ms 70232 KB Output is correct
98 Correct 848 ms 70948 KB Output is correct
99 Correct 865 ms 70576 KB Output is correct
100 Correct 905 ms 70788 KB Output is correct
101 Correct 899 ms 70344 KB Output is correct
102 Correct 874 ms 70668 KB Output is correct