Submission #1105570

# Submission time Handle Problem Language Result Execution time Memory
1105570 2024-10-26T17:52:53 Z SamNguyen Progression (NOI20_progression) C++14
100 / 100
902 ms 144608 KB
#include <bits/stdc++.h>
using namespace std;

template <class T, class F, int N>
class SegmentTree_ {
private:
	int n;
	T st[4 * N];
	F lz[4 * N];

	void push_down(int id) {
		for (int j : {0, 1}) {
			st[id << 1 | j] += lz[id];
			lz[id << 1 | j] += lz[id];
		}
		lz[id] = F::ZERO();
	}
	
	void build(int id, int l, int r, int64_t arr[]) {
		lz[id] = F::ZERO();
		if (l == r) {
			st[id] = T(arr[l], l);
			return;
		}

		int m = (l + r) >> 1;
		build(id << 1, l, m, arr);
		build(id << 1 | 1, m + 1, r, arr);
		st[id] = st[id << 1] + st[id << 1 | 1];
	}
	
	void update(int id, int l, int r, int left, int right, F f) {
		if (r < left or right < l)
			return;

		if (left <= l and r <= right) {
			st[id] += f;
			lz[id] += f;
			return;
		}

		push_down(id);
		int m = (l + r) >> 1;
		update(id << 1, l, m, left, right, f);
		update(id << 1 | 1, m + 1, r, left, right, f);
		st[id] = st[id << 1] + st[id << 1 | 1];
	}

	T get(int id, int l, int r, int left, int right) {
		if (r < left or right < l)
			return T::ZERO();

		if (left <= l and r <= right)
			return st[id];

		push_down(id);
		int m = (l + r) >> 1;
		return get(id << 1, l, m, left, right) + get(id << 1 | 1, m + 1, r, left, right);
	}

public:
	void init(int _n, int64_t arr[]) {
		n = _n;
		build(1, 1, n, arr);
	}

	void update(int left, int right, F f) { update(1, 1, n, left, right, f); }
	T get(int left, int right) { return get(1, 1, n, left, right); }
};

struct F_ {
	bool _is_set;
	int64_t da, db;

	F_(bool __is_set = false, int64_t _da = 0, int64_t _db = 0) {
		_is_set = __is_set;
		da = _da, db = _db;
	}

	inline bool is_set() { return _is_set; }
	inline bool is_add() { return not _is_set; }

	static F_ ADD(int64_t da, int64_t db)    { return F_(false, da, db); }
	static F_ UPDATE(int64_t da, int64_t db) { return F_(true,  da, db); }
	static F_ ZERO() { return F_(); }

	F_ operator + (const F_& oth) const {
		if (oth._is_set) return oth;
		return F_(_is_set, da + oth.da, db + oth.db);
	}
	void operator += (const F_& oth) { (*this) = (*this) + oth; }
};

struct T_ {
	// y = a * i + b
	int64_t sum_i = 0, sum_a = 0, sum_b = 0, sum_ai = 0;
	int left_i, right_i;

	int64_t val_first = 0, val_last = 0;
	int64_t diff_first = 0, diff_last = 0;
	int best_len_prefix = 0, best_len_suffix = 0, best_len = 0;
	
	T_(int64_t val = 0, int64_t i = 0) {
		left_i = right_i = i;
		sum_i = i;
		sum_a = 0;
		sum_b = val;
		sum_ai = 0;
		val_first = val_last = val;
		best_len_prefix = best_len_suffix = best_len = 1;
	}
	static T_ ZERO() { return T_(); }

	inline bool is_zero() const { return left_i == 0 or right_i == 0; }
	inline bool is_single() const { return left_i == right_i; }
	inline int len() const { return right_i - left_i + 1; }

	void print() const {
		cerr 
			<< "| range: "
			<< setw(3) << left_i 
			<< setw(3) << right_i 
			<< "| sums: "
			<< setw(4) << sum_i
			<< setw(4) << sum_a
			<< setw(4) << sum_b
			<< setw(4) << sum_ai
			<< "| vals: "
			<< setw(4) << val_first
			<< setw(4) << val_last
			<< "| diffs: "
			<< setw(4) << diff_first
			<< setw(4) << diff_last
			<< "| best_lens: "
			<< setw(3) << best_len_prefix
			<< setw(3) << best_len_suffix
			<< setw(3) << best_len
			<< endl;
	}

	T_ operator + (const T_& right) {
		T_ res;
		T_& left = *this;

		if (left.is_zero())
			return right;

		if (right.is_zero())
			return left;

		res.sum_i  = left.sum_i  + right.sum_i;
		res.sum_a  = left.sum_a  + right.sum_a;
		res.sum_b  = left.sum_b  + right.sum_b;
		res.sum_ai = left.sum_ai + right.sum_ai;

		res.left_i  =  left.left_i;
		res.right_i = right.right_i;
		res.val_first =  left.val_first;
		res.val_last  = right.val_last;

		int64_t mid_diff = right.val_first - left.val_last;

		res.diff_first = ( left.is_single()) ? mid_diff : left.diff_first;
		res.diff_last  = (right.is_single()) ? mid_diff : right.diff_last;

		auto merge_parts = [&](int p1, int p2, bool cond_p1, bool cond_mid, bool cond_p2) {
			int res = p1;
			if (not cond_p1 or not cond_mid)
				return res;
			
			res += 1;
			if (not cond_p2)
				return res;

			res += p2 - 1;
			return res;
		};

		res.best_len_prefix = merge_parts(
			left.best_len_prefix,
			right.best_len_prefix,
			left.best_len_prefix == left.len(),
			mid_diff ==  left.diff_first or  left.is_single(),
			mid_diff == right.diff_first or right.is_single()
		);

		res.best_len_suffix = merge_parts(
			right.best_len_suffix,
			left.best_len_suffix,
			right.best_len_suffix == right.len(),
			mid_diff == right.diff_last  or right.is_single(),
			mid_diff ==  left.diff_last  or  left.is_single()
		);

		res.best_len = max(
			max(left.best_len, right.best_len),
			max(res.best_len_prefix, res.best_len_suffix)
		);

		if (left.diff_last == mid_diff)
			res.best_len = max(res.best_len, left.best_len_suffix + 1);

		if (mid_diff == right.diff_first)
			res.best_len = max(res.best_len, right.best_len_prefix + 1);

		if (left.diff_last == mid_diff and mid_diff == right.diff_first)
			res.best_len = max(
				res.best_len,
				left.best_len_suffix + right.best_len_prefix
			);

		/*
		cerr << "==================================" << endl;
		cerr << "left:  ";  left.print();
		cerr << "right: "; right.print();
		cerr << "-------mid_diff = " << mid_diff << "-----------------" << endl;
		cerr << "add:   ";   res.print();
		cerr << "==================================" << endl;
		*/

		return res;
	}

	T_ operator + (const F_& f) const {
		T_ res = *this;
		if (f._is_set) {
			res.sum_a   = f.da;
			res.sum_b   = f.db;
			res.sum_ai  = f.da * res.sum_i;

			res.val_first   = f.da * res.left_i  + f.db;
			res.val_last    = f.da * res.right_i + f.db;
			
			res.diff_first  = f.da;
			res.diff_last   = f.da;

			// Special case due to overwriting!
			res.best_len_prefix = res.best_len_suffix = res.best_len = res.len();
		}
		else {
			res.sum_a  += f.da;
			res.sum_b  += f.db;
			res.sum_ai += f.da * res.sum_i;
			
			res.val_first  += f.da * res.left_i  + f.db;
			res.val_last   += f.da * res.right_i + f.db;
			
			res.diff_first += f.da;
			res.diff_last  += f.da;
		}
		return res;
	}
	void operator += (const F_& oth) { (*this) = (*this) + oth; }
};

template <int N>
using SegmentTree = SegmentTree_<T_, F_, N>;

const int MAX = 3e5 + 7;
int N, Q;
int64_t arr[MAX];
SegmentTree<MAX> st;

void update_1(int l, int r, int64_t s, int64_t c) {
	st.update(l, r, F_::ADD(c, s - l * c));
}

void update_2(int l, int r, int64_t s, int64_t c) {
	st.update(l, r, F_::UPDATE(c, s - l * c));
}
	
int64_t evaluate(int l, int r) {
	return st.get(l, r).best_len;
}

void print(int l, int r) {
	for (int i = 1; i <= N; i++) {
		T_ node = st.get(i, i);
		int64_t x = node.sum_ai + node.sum_b;
		cerr << setw(5) << x;
	}
	cerr << endl;
}
	
int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin >> N >> Q;
	for (int i = 1; i <= N; i++)
		cin >> arr[i];
	st.init(N, arr);

	while (Q--) {
		int t; cin >> t;
		if (t == 1) {
			int l, r; int64_t s, c;
			cin >> l >> r >> s >> c;
			update_1(l, r, s, c);
		}
		if (t == 2) {
			int l, r; int64_t s, c;
			cin >> l >> r >> s >> c;
			update_2(l, r, s, c);
		}
		if (t == 3) {
			int l, r;
			cin >> l >> r;
			cout << evaluate(l, r) << '\n';
			// print(l, r);
		}
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 147 ms 134732 KB Output is correct
2 Correct 68 ms 134216 KB Output is correct
3 Correct 70 ms 134216 KB Output is correct
4 Correct 81 ms 134216 KB Output is correct
5 Correct 73 ms 134216 KB Output is correct
6 Correct 75 ms 137288 KB Output is correct
7 Correct 72 ms 137032 KB Output is correct
8 Correct 17 ms 133968 KB Output is correct
9 Correct 16 ms 133968 KB Output is correct
10 Correct 17 ms 133968 KB Output is correct
11 Correct 123 ms 142920 KB Output is correct
12 Correct 121 ms 142828 KB Output is correct
13 Correct 112 ms 142944 KB Output is correct
14 Correct 118 ms 143176 KB Output is correct
15 Correct 118 ms 142956 KB Output is correct
16 Correct 116 ms 142816 KB Output is correct
17 Correct 116 ms 142664 KB Output is correct
18 Correct 119 ms 142708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 133968 KB Output is correct
2 Correct 17 ms 133968 KB Output is correct
3 Correct 18 ms 133984 KB Output is correct
4 Correct 21 ms 133968 KB Output is correct
5 Correct 17 ms 134028 KB Output is correct
6 Correct 17 ms 133968 KB Output is correct
7 Correct 16 ms 133968 KB Output is correct
8 Correct 19 ms 133968 KB Output is correct
9 Correct 17 ms 133968 KB Output is correct
10 Correct 18 ms 133968 KB Output is correct
11 Correct 17 ms 133968 KB Output is correct
12 Correct 19 ms 133904 KB Output is correct
13 Correct 26 ms 131912 KB Output is correct
14 Correct 21 ms 131912 KB Output is correct
15 Correct 17 ms 133980 KB Output is correct
16 Correct 18 ms 133968 KB Output is correct
17 Correct 19 ms 131996 KB Output is correct
18 Correct 24 ms 131920 KB Output is correct
19 Correct 19 ms 131920 KB Output is correct
20 Correct 18 ms 133968 KB Output is correct
21 Correct 20 ms 131920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 135248 KB Output is correct
2 Correct 131 ms 136688 KB Output is correct
3 Correct 108 ms 136520 KB Output is correct
4 Correct 102 ms 136520 KB Output is correct
5 Correct 104 ms 136740 KB Output is correct
6 Correct 106 ms 136776 KB Output is correct
7 Correct 104 ms 136732 KB Output is correct
8 Correct 16 ms 133968 KB Output is correct
9 Correct 16 ms 133968 KB Output is correct
10 Correct 16 ms 133968 KB Output is correct
11 Correct 467 ms 140016 KB Output is correct
12 Correct 370 ms 141384 KB Output is correct
13 Correct 456 ms 139848 KB Output is correct
14 Correct 472 ms 139824 KB Output is correct
15 Correct 397 ms 141284 KB Output is correct
16 Correct 508 ms 141804 KB Output is correct
17 Correct 514 ms 141896 KB Output is correct
18 Correct 532 ms 141896 KB Output is correct
19 Correct 433 ms 141156 KB Output is correct
20 Correct 424 ms 141160 KB Output is correct
21 Correct 426 ms 141136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 645 ms 134472 KB Output is correct
2 Correct 135 ms 137288 KB Output is correct
3 Correct 139 ms 137288 KB Output is correct
4 Correct 134 ms 137288 KB Output is correct
5 Correct 138 ms 137136 KB Output is correct
6 Correct 139 ms 137296 KB Output is correct
7 Correct 138 ms 137288 KB Output is correct
8 Correct 17 ms 133968 KB Output is correct
9 Correct 17 ms 133968 KB Output is correct
10 Correct 16 ms 133968 KB Output is correct
11 Correct 640 ms 140876 KB Output is correct
12 Correct 705 ms 144048 KB Output is correct
13 Correct 617 ms 140804 KB Output is correct
14 Correct 611 ms 140880 KB Output is correct
15 Correct 549 ms 144104 KB Output is correct
16 Correct 613 ms 144108 KB Output is correct
17 Correct 631 ms 144200 KB Output is correct
18 Correct 638 ms 144156 KB Output is correct
19 Correct 645 ms 144144 KB Output is correct
20 Correct 617 ms 144156 KB Output is correct
21 Correct 670 ms 144056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 135248 KB Output is correct
2 Correct 131 ms 136688 KB Output is correct
3 Correct 108 ms 136520 KB Output is correct
4 Correct 102 ms 136520 KB Output is correct
5 Correct 104 ms 136740 KB Output is correct
6 Correct 106 ms 136776 KB Output is correct
7 Correct 104 ms 136732 KB Output is correct
8 Correct 16 ms 133968 KB Output is correct
9 Correct 16 ms 133968 KB Output is correct
10 Correct 16 ms 133968 KB Output is correct
11 Correct 467 ms 140016 KB Output is correct
12 Correct 370 ms 141384 KB Output is correct
13 Correct 456 ms 139848 KB Output is correct
14 Correct 472 ms 139824 KB Output is correct
15 Correct 397 ms 141284 KB Output is correct
16 Correct 508 ms 141804 KB Output is correct
17 Correct 514 ms 141896 KB Output is correct
18 Correct 532 ms 141896 KB Output is correct
19 Correct 433 ms 141156 KB Output is correct
20 Correct 424 ms 141160 KB Output is correct
21 Correct 426 ms 141136 KB Output is correct
22 Correct 854 ms 143480 KB Output is correct
23 Correct 140 ms 137036 KB Output is correct
24 Correct 138 ms 137032 KB Output is correct
25 Correct 134 ms 137032 KB Output is correct
26 Correct 138 ms 137288 KB Output is correct
27 Correct 139 ms 137084 KB Output is correct
28 Correct 137 ms 137032 KB Output is correct
29 Correct 16 ms 133968 KB Output is correct
30 Correct 16 ms 133968 KB Output is correct
31 Correct 16 ms 134032 KB Output is correct
32 Correct 800 ms 140848 KB Output is correct
33 Correct 829 ms 143428 KB Output is correct
34 Correct 809 ms 140808 KB Output is correct
35 Correct 746 ms 140812 KB Output is correct
36 Correct 700 ms 140588 KB Output is correct
37 Correct 653 ms 140480 KB Output is correct
38 Correct 696 ms 140536 KB Output is correct
39 Correct 797 ms 143388 KB Output is correct
40 Correct 840 ms 143688 KB Output is correct
41 Correct 870 ms 143688 KB Output is correct
42 Correct 867 ms 143432 KB Output is correct
43 Correct 808 ms 143588 KB Output is correct
44 Correct 805 ms 143444 KB Output is correct
45 Correct 791 ms 143420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 134732 KB Output is correct
2 Correct 68 ms 134216 KB Output is correct
3 Correct 70 ms 134216 KB Output is correct
4 Correct 81 ms 134216 KB Output is correct
5 Correct 73 ms 134216 KB Output is correct
6 Correct 75 ms 137288 KB Output is correct
7 Correct 72 ms 137032 KB Output is correct
8 Correct 17 ms 133968 KB Output is correct
9 Correct 16 ms 133968 KB Output is correct
10 Correct 17 ms 133968 KB Output is correct
11 Correct 123 ms 142920 KB Output is correct
12 Correct 121 ms 142828 KB Output is correct
13 Correct 112 ms 142944 KB Output is correct
14 Correct 118 ms 143176 KB Output is correct
15 Correct 118 ms 142956 KB Output is correct
16 Correct 116 ms 142816 KB Output is correct
17 Correct 116 ms 142664 KB Output is correct
18 Correct 119 ms 142708 KB Output is correct
19 Correct 18 ms 133968 KB Output is correct
20 Correct 17 ms 133968 KB Output is correct
21 Correct 18 ms 133984 KB Output is correct
22 Correct 21 ms 133968 KB Output is correct
23 Correct 17 ms 134028 KB Output is correct
24 Correct 17 ms 133968 KB Output is correct
25 Correct 16 ms 133968 KB Output is correct
26 Correct 19 ms 133968 KB Output is correct
27 Correct 17 ms 133968 KB Output is correct
28 Correct 18 ms 133968 KB Output is correct
29 Correct 17 ms 133968 KB Output is correct
30 Correct 19 ms 133904 KB Output is correct
31 Correct 26 ms 131912 KB Output is correct
32 Correct 21 ms 131912 KB Output is correct
33 Correct 17 ms 133980 KB Output is correct
34 Correct 18 ms 133968 KB Output is correct
35 Correct 19 ms 131996 KB Output is correct
36 Correct 24 ms 131920 KB Output is correct
37 Correct 19 ms 131920 KB Output is correct
38 Correct 18 ms 133968 KB Output is correct
39 Correct 20 ms 131920 KB Output is correct
40 Correct 393 ms 135248 KB Output is correct
41 Correct 131 ms 136688 KB Output is correct
42 Correct 108 ms 136520 KB Output is correct
43 Correct 102 ms 136520 KB Output is correct
44 Correct 104 ms 136740 KB Output is correct
45 Correct 106 ms 136776 KB Output is correct
46 Correct 104 ms 136732 KB Output is correct
47 Correct 16 ms 133968 KB Output is correct
48 Correct 16 ms 133968 KB Output is correct
49 Correct 16 ms 133968 KB Output is correct
50 Correct 467 ms 140016 KB Output is correct
51 Correct 370 ms 141384 KB Output is correct
52 Correct 456 ms 139848 KB Output is correct
53 Correct 472 ms 139824 KB Output is correct
54 Correct 397 ms 141284 KB Output is correct
55 Correct 508 ms 141804 KB Output is correct
56 Correct 514 ms 141896 KB Output is correct
57 Correct 532 ms 141896 KB Output is correct
58 Correct 433 ms 141156 KB Output is correct
59 Correct 424 ms 141160 KB Output is correct
60 Correct 426 ms 141136 KB Output is correct
61 Correct 645 ms 134472 KB Output is correct
62 Correct 135 ms 137288 KB Output is correct
63 Correct 139 ms 137288 KB Output is correct
64 Correct 134 ms 137288 KB Output is correct
65 Correct 138 ms 137136 KB Output is correct
66 Correct 139 ms 137296 KB Output is correct
67 Correct 138 ms 137288 KB Output is correct
68 Correct 17 ms 133968 KB Output is correct
69 Correct 17 ms 133968 KB Output is correct
70 Correct 16 ms 133968 KB Output is correct
71 Correct 640 ms 140876 KB Output is correct
72 Correct 705 ms 144048 KB Output is correct
73 Correct 617 ms 140804 KB Output is correct
74 Correct 611 ms 140880 KB Output is correct
75 Correct 549 ms 144104 KB Output is correct
76 Correct 613 ms 144108 KB Output is correct
77 Correct 631 ms 144200 KB Output is correct
78 Correct 638 ms 144156 KB Output is correct
79 Correct 645 ms 144144 KB Output is correct
80 Correct 617 ms 144156 KB Output is correct
81 Correct 670 ms 144056 KB Output is correct
82 Correct 854 ms 143480 KB Output is correct
83 Correct 140 ms 137036 KB Output is correct
84 Correct 138 ms 137032 KB Output is correct
85 Correct 134 ms 137032 KB Output is correct
86 Correct 138 ms 137288 KB Output is correct
87 Correct 139 ms 137084 KB Output is correct
88 Correct 137 ms 137032 KB Output is correct
89 Correct 16 ms 133968 KB Output is correct
90 Correct 16 ms 133968 KB Output is correct
91 Correct 16 ms 134032 KB Output is correct
92 Correct 800 ms 140848 KB Output is correct
93 Correct 829 ms 143428 KB Output is correct
94 Correct 809 ms 140808 KB Output is correct
95 Correct 746 ms 140812 KB Output is correct
96 Correct 700 ms 140588 KB Output is correct
97 Correct 653 ms 140480 KB Output is correct
98 Correct 696 ms 140536 KB Output is correct
99 Correct 797 ms 143388 KB Output is correct
100 Correct 840 ms 143688 KB Output is correct
101 Correct 870 ms 143688 KB Output is correct
102 Correct 867 ms 143432 KB Output is correct
103 Correct 808 ms 143588 KB Output is correct
104 Correct 805 ms 143444 KB Output is correct
105 Correct 791 ms 143420 KB Output is correct
106 Correct 902 ms 144404 KB Output is correct
107 Correct 154 ms 137288 KB Output is correct
108 Correct 166 ms 137288 KB Output is correct
109 Correct 150 ms 137288 KB Output is correct
110 Correct 17 ms 133968 KB Output is correct
111 Correct 16 ms 134136 KB Output is correct
112 Correct 16 ms 133968 KB Output is correct
113 Correct 759 ms 143640 KB Output is correct
114 Correct 773 ms 143524 KB Output is correct
115 Correct 743 ms 143516 KB Output is correct
116 Correct 783 ms 143460 KB Output is correct
117 Correct 836 ms 144416 KB Output is correct
118 Correct 774 ms 143464 KB Output is correct
119 Correct 766 ms 143472 KB Output is correct
120 Correct 497 ms 142192 KB Output is correct
121 Correct 484 ms 141924 KB Output is correct
122 Correct 485 ms 142160 KB Output is correct
123 Correct 451 ms 141160 KB Output is correct
124 Correct 421 ms 141132 KB Output is correct
125 Correct 434 ms 141260 KB Output is correct
126 Correct 792 ms 141128 KB Output is correct
127 Correct 729 ms 141128 KB Output is correct
128 Correct 766 ms 144456 KB Output is correct
129 Correct 712 ms 141252 KB Output is correct
130 Correct 635 ms 141128 KB Output is correct
131 Correct 699 ms 141296 KB Output is correct
132 Correct 675 ms 141284 KB Output is correct
133 Correct 792 ms 144608 KB Output is correct
134 Correct 811 ms 144452 KB Output is correct
135 Correct 772 ms 144456 KB Output is correct
136 Correct 155 ms 137220 KB Output is correct
137 Correct 156 ms 137288 KB Output is correct
138 Correct 152 ms 137288 KB Output is correct