๐Ÿ“ฆ gopikrishna000 / templates-latest

๐Ÿ“„ divide.cpp ยท 204 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204<snippet>
	<content><![CDATA[



/*----------------------changable--------------------------*/

int _p = 0; //update this global variable before querying to switch the structure.
int __nn = 1; //# of segment tree needed
// vector<int> NEUTRAL_DELTA(__nn, 0);

struct Vertex {
	int i;
	int val;
	int lazy;
	Vertex() {
		//constructor neutral node
		if (_p == 0) {
			i = -1;
			val = INT_MAX;
			lazy = 0;
		}
	}
};

//vector<Vertex> NEUTRAL_NODE(__nn, Vertex());


//search "setVertex" to find positions of setting node.
vector<vector<Vertex>> t(__nn, vector<Vertex>(4 * (1e6 + 5)));

Vertex merge(Vertex left, Vertex right) {
	if (_p == 0) {
		Vertex cur;
		cur.val = min(left.val , right.val );
		cur.i = (left.val < right.val) ? left.i : right.i;
		return cur;
	}
	else {
		Vertex cur;
		cur.val = min(left.val , right.val );
		return cur;
	}
}

int joinDeltas(int oldDelta, int newDelta) {
	if (_p == 0) {
		return oldDelta + newDelta;
	}
	else if (_p == 1) { //for range assignment update//also add if(t[_p][v].lazy!=NEUTRAL_DELTA) in push
		return newDelta;
	}
}

int joinValueWithDelta(int value, int delta, int length) {
	if (_p == 0) {
		return value + length * delta;
	}
	else if (_p == 1) { //for range assignment update
		return (length) * delta;
	}
}

/*----------------------changable--------------------------*/

//build(arr,1,0,n-1);
void build(auto &a, int v, int tl, int tr) {

	if (tl == tr) {//setVertex
		if (_p == 0) {
			t[_p][v].val = a[tl];
			t[_p][v].i = tl;
		}
		else {
			t[_p][v].val = a[tl];
		}
	}//endsetVertex
	else {
		int tm = (tl + tr) / 2;
		build(a, v * 2, tl, tm);
		build(a, v * 2 + 1, tm + 1, tr);
		t[_p][v] = merge(t[_p][v * 2] , t[_p][v * 2 + 1]);
	}
}


//sum(1,0,n-1,l,r); //tl,tr are seg range(standard:fixed for vertex v); l,r are actual range needed.
Vertex sum(int v , int tl , int tr , int l, int r) {
	if (l > r)
		return Vertex();  //return NEUTRAL_NODE[_p];
	if (l == tl && r == tr) {
		return t[_p][v];
	}
	int tm = (tl + tr) / 2;
	return merge(sum(v * 2, tl, tm, l, min(r, tm))
	             , sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

//update(1,0,n-1,pos,val); //0-based
void update(int v , int tl , int tr , int pos, int new_val) {
	if (tl == tr) {//setVertex
		t[_p][v].val = new_val; //the actual pos node
	}//endsetVertex
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update(v * 2, tl, tm, pos, new_val);
		else
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		t[_p][v] = merge(t[_p][v * 2] , t[_p][v * 2 + 1]);
	}
}

/*shortcuts*/
vi _nn(__nn);
//build(arr);
void build(auto &arr) {
	_nn[_p] = (int)arr.size();
	build(arr, 1, 0, _nn[_p] - 1);
}
//sum(l,r);
Vertex sum(int l, int r) {
	return sum(1, 0, _nn[_p] - 1, l, r);
}
//update(pos,val);
void update(int pos, int val) {
	update(1, 0, _nn[_p] - 1, pos, val);
}
/*shortcuts*/

/*--------here-comes-range-update--------------*/

void push(int v, int tl, int tr) { //setVertex
	if (_p == 0) {
		int tm = (tl + tr) / 2;
		t[_p][v * 2].val = joinValueWithDelta(t[_p][v * 2].val, t[_p][v].lazy , (tm - tl + 1));
		t[_p][v * 2].lazy = joinDeltas(t[_p][v * 2].lazy, t[_p][v].lazy);
		t[_p][v * 2 + 1].val = joinValueWithDelta(t[_p][v * 2 + 1].val, t[_p][v].lazy, (tr - tm));
		t[_p][v * 2 + 1].lazy = joinDeltas(t[_p][v * 2 + 1].lazy, t[_p][v].lazy);
		t[_p][v].lazy = 0;
	}
	else if (_p == 1) {

	}
}//endsetVertex

void update(int v, int tl, int tr, int l, int r, auto addend) {
	if (l > r)
		return;
	if (l == tl && tr == r) {//setVertex
		t[_p][v].val += (tr - tl + 1) * addend;
		t[_p][v].lazy += addend;
	}//endsetVertex
	else {
		int tm = (tl + tr) / 2;
		push(v, tl, tr);
		update(v * 2, tl, tm, l, min(r, tm), addend);
		update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend);
		t[_p][v] = merge(t[_p][v * 2] , t[_p][v * 2 + 1]);
	}
}

Vertex query(int v, int tl, int tr, int l, int r) {
	if (l > r)
		return Vertex();   //return NEUTRAL_NODE[_p];
	if (l <= tl && tr <= r)
		return t[_p][v];
	int tm = (tl + tr) / 2;
	push(v, tl, tr);
	return merge(query(v * 2, tl, tm, l, min(r, tm)) ,
	             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}


//shortcut
void update(int l, int r, auto val) {
	update(1, 0 , _nn[_p] - 1, l, r, val);
}
Vertex query(int l, int r) {
	return query(1, 0, _nn[_p] - 1, l, r);
}

/*--------here-ends-range-update--------------*/

int ans;
void divide(int l, int r) {
	if (l >=  r)
		return;
	int mid = sum(l, r).i;
	// wd(l,r,mid);
	ans -= sum(l, r).val * (mid - l + 1) * (r - mid + 1) - sum(l, r).val;
// wd(l,r,sum(l,r).val,(mid-l+1),(r-mid+1));
	divide(l, mid - 1);
	divide(mid + 1, r);
}


]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>divide</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>