Skip to content

Commit 0a1bf7f

Browse files
committed
Stuff
1 parent 0e443a1 commit 0a1bf7f

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

.vscode/template.code-snippets

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
// Place your CompetitiveProgramming workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
3+
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
4+
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
5+
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
6+
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
7+
// Placeholders with the same ids are connected.
8+
// Example:
9+
// "Print to console": {
10+
// "scope": "javascript,typescript",
11+
// "prefix": "log",
12+
// "body": [
13+
// "console.log('$1');",
14+
// "$2"
15+
// ],
16+
// "description": "Log output to console"
17+
// }
18+
"Template": {
19+
"scope": "cpp",
20+
"prefix": "#include <bits/stdc++.h>",
21+
"body": [
22+
"#include <bits/stdc++.h>",
23+
"typedef long long ll;",
24+
"using namespace std;",
25+
"",
26+
"int main() {",
27+
" cin.tie(0)->sync_with_stdio(0);",
28+
" $0",
29+
" return 0;",
30+
"}"
31+
]
32+
}
33+
}

CSES/(8) Range Queries/2416.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
const ll INF = 1e18;
6+
7+
int n, q;
8+
ll x[200002], pref[200002], ans[200002];
9+
ll contrib[200002], bit[200002];
10+
vector<pair<int, int>> bckt[200001];
11+
12+
void update(int pos, ll val) { for (; pos <= n; pos += pos & -pos) bit[pos] += val; }
13+
14+
ll query(int a, int b) {
15+
ll ans = 0;
16+
for (; b; b -= b & -b) ans += bit[b];
17+
for (a--; a; a -= a & -a) ans -= bit[a];
18+
return ans;
19+
}
20+
21+
int main() {
22+
cin.tie(0)->sync_with_stdio(0);
23+
cin >> n >> q;
24+
for (int i = 1; i <= n; i++) {
25+
cin >> x[i];
26+
pref[i] = pref[i - 1] + x[i];
27+
}
28+
x[n + 1] = INF;
29+
pref[n + 1] = pref[n] + x[n + 1];
30+
for (int i = 1; i <= q; i++) {
31+
int a, b;
32+
cin >> a >> b;
33+
bckt[a].push_back({b, i});
34+
}
35+
deque<int> stck = {n + 1};
36+
for (int i = n; i; i--) {
37+
while (stck.size() && x[i] >= x[stck.front()]) {
38+
update(stck.front(), -contrib[stck.front()]);
39+
stck.pop_front();
40+
}
41+
contrib[i] = (stck.front() - 1 - i) * x[i] - (pref[stck.front() - 1] - pref[i]);
42+
update(i, contrib[i]);
43+
stck.push_front(i);
44+
for (pair<int, int> j : bckt[i]) {
45+
int pos = upper_bound(stck.begin(), stck.end(), j.first) - stck.begin() - 1;
46+
ans[j.second] = (pos ? query(i, stck[pos - 1]) : 0) + (j.first - stck[pos]) * x[stck[pos]] - (pref[j.first] - pref[stck[pos]]);
47+
}
48+
}
49+
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
50+
return 0;
51+
}

IOI/IOI 12-tourist.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
struct Node {
6+
ll l, r, val;
7+
int cnt;
8+
Node *lc, *rc;
9+
10+
Node(ll L, ll R): l(L), r(R), val(0), cnt(0), lc(nullptr), rc(nullptr) {};
11+
12+
void insert(ll v) {
13+
val += v;
14+
cnt++;
15+
if (l == r) return;
16+
ll mid = (l + r) / 2;
17+
if (v > mid) {
18+
if (!rc) rc = new Node(mid + 1, r);
19+
rc->insert(v);
20+
} else {
21+
if (!lc) lc = new Node(l, mid);
22+
lc->insert(v);
23+
}
24+
}
25+
26+
ll query(int k) {
27+
if (cnt <= k) return val;
28+
if (l == r) return (val / cnt) * k;
29+
int mid = (l + r) / 2;
30+
if (rc && rc->cnt >= k) return rc->query(k);
31+
if (!rc) return lc->query(k);
32+
return lc->query(k - rc->cnt) + rc->val;
33+
}
34+
};
35+
36+
int main() {
37+
cin.tie(0)->sync_with_stdio(0);
38+
Node *root = new Node(0, INT_MAX);
39+
int n, k;
40+
cin >> n >> k;
41+
ll ans = 0;
42+
for (int i = 0; i < n; i++) {
43+
ll v;
44+
cin >> v;
45+
root->insert(v);
46+
ans = max(ans, root->query(k - i));
47+
}
48+
cout << ans;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)