-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f003377
commit 1bb67db
Showing
6 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <bits/stdc++.h> | ||
#define MAX 1000001 | ||
|
||
using namespace std; | ||
|
||
int N, M; | ||
int arr[MAX], tree[4 * MAX]; | ||
|
||
int init(int start, int end, int n) { | ||
if (start == end) | ||
return tree[n] = arr[start]; | ||
|
||
int mid = (start + end) / 2; | ||
return tree[n] = max(init(start, mid, 2 * n), init(mid + 1, end, 2 * n + 1)); | ||
} | ||
|
||
int query(int start, int end, int n, int left, int right) { | ||
if (left > end || right < start) | ||
return 0; | ||
|
||
if (left <= start && end <= right) | ||
return tree[n]; | ||
|
||
int mid = (start + end) / 2; | ||
return max(query(start, mid, 2 * n, left, right), | ||
query(mid + 1, end, 2 * n + 1, left, right)); | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cin >> N >> M; | ||
|
||
for (int i = 0; i < N; i++) | ||
cin >> arr[i]; | ||
init(0, N - 1, 1); | ||
|
||
for (int i = M - 1; i + M - 1 < N; i++) | ||
cout << query(0, N - 1, 1, i - M + 1, i + M - 1) << " "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <bits/stdc++.h> | ||
#define MAX 500001 | ||
|
||
using namespace std; | ||
|
||
int N, M, a, b, c; | ||
int arr[MAX], arrIndex[MAX], tree[4 * MAX]; | ||
|
||
int init(int start, int end, int n) { | ||
if (start == end) { | ||
arrIndex[start] = n; | ||
return tree[n] = arr[start]; | ||
} | ||
|
||
int mid = (start + end) / 2; | ||
return tree[n] = init(start, mid, 2 * n) + init(mid + 1, end, 2 * n + 1); | ||
} | ||
|
||
void update(int index, int value) { | ||
int cur = arrIndex[index]; | ||
|
||
tree[cur] = value; | ||
while (cur != 1) { | ||
tree[cur / 2] = tree[cur] + tree[(cur % 2 == 0) ? (cur + 1) : (cur - 1)]; | ||
cur /= 2; | ||
} | ||
} | ||
|
||
int query(int start, int end, int n, int sum) { | ||
if (start == end) | ||
return start; | ||
|
||
int mid = (start + end) / 2; | ||
int ret = 0; | ||
|
||
if (sum <= tree[2 * n]) | ||
ret = query(start, mid, 2 * n, sum); | ||
else | ||
ret = query(mid + 1, end, 2 * n + 1, sum - tree[2 * n]); | ||
return ret; | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cin >> N; | ||
for (int i = 0; i < N; i++) | ||
cin >> arr[i]; | ||
|
||
init(0, N - 1, 1); | ||
|
||
cin >> M; | ||
for (int i = 0; i < M; i++) { | ||
cin >> a; | ||
if (a == 1) { | ||
cin >> b >> c; | ||
update(b - 1, arr[b - 1] + c); | ||
arr[b - 1] += c; | ||
} else { | ||
cin >> b; | ||
cout << query(0, N - 1, 1, b) + 1 << "\n"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <bits/stdc++.h> | ||
#define MAX 100001 | ||
|
||
using namespace std; | ||
|
||
int N, t; | ||
int arr[MAX], arrIndex[MAX], tree[4 * MAX]; | ||
|
||
int init(int start, int end, int n) { | ||
if (start == end) { | ||
arrIndex[start] = n; | ||
return tree[n] = 1; | ||
} | ||
|
||
int mid = (start + end) / 2; | ||
return tree[n] = init(start, mid, 2 * n) + init(mid + 1, end, 2 * n + 1); | ||
} | ||
|
||
void update(int index, int value) { | ||
int cur = arrIndex[index]; | ||
|
||
tree[cur] = value; | ||
while (cur != 1) { | ||
tree[cur / 2] = tree[cur] + tree[(cur % 2 == 0) ? (cur + 1) : (cur - 1)]; | ||
cur /= 2; | ||
} | ||
} | ||
|
||
int query(int start, int end, int n, int sum) { | ||
if (start == end) | ||
return start; | ||
|
||
int mid = (start + end) / 2; | ||
int ret = 0; | ||
|
||
if (sum <= tree[2 * n]) | ||
ret = query(start, mid, 2 * n, sum); | ||
else | ||
ret = query(mid + 1, end, 2 * n + 1, sum - tree[2 * n]); | ||
return ret; | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cin >> N; | ||
|
||
fill(tree, tree + 4 * N, 1); | ||
init(0, N - 1, 1); | ||
|
||
for (int i = 0; i < N; i++) { | ||
cin >> t; | ||
int tmp = query(0, N - 1, 1, t + 1); | ||
update(tmp, 0); | ||
arr[tmp] = i; | ||
} | ||
for (int i = 0; i < N; i++) | ||
cout << arr[i] + 1 << "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int N, t; | ||
vector<int> V; | ||
|
||
void compress(vector<int> &A) { | ||
vector<int> B(A); | ||
sort(B.begin(), B.end()); | ||
// Remove Duplicates | ||
B.erase(unique(B.begin(), B.end()), B.end()); | ||
for (int &x : A) | ||
x = lower_bound(B.begin(), B.end(), x) - B.begin(); | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cin >> N; | ||
for (int i = 0; i < N; i++) | ||
cin >> t, V.push_back(t); | ||
|
||
compress(V); | ||
|
||
for (auto i : V) | ||
cout << i << " "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <bits/stdc++.h> | ||
#define MAX 500001 | ||
|
||
using namespace std; | ||
|
||
int N, t; | ||
vector<int> V; | ||
int arrIndex[MAX], tree[4 * MAX]; | ||
|
||
void compress(vector<int> &A) { | ||
vector<int> B(A); | ||
sort(B.begin(), B.end()); | ||
for (int &x : A) | ||
x = lower_bound(B.begin(), B.end(), x) - B.begin(); | ||
} | ||
|
||
void init(int start, int end, int n) { | ||
if (start == end) { | ||
arrIndex[start] = n; | ||
return; | ||
} | ||
|
||
int mid = (start + end) / 2; | ||
init(start, mid, 2 * n); | ||
init(mid + 1, end, 2 * n + 1); | ||
} | ||
|
||
void update(int index, int value) { | ||
int cur = arrIndex[index]; | ||
|
||
tree[cur] = value; | ||
while (cur != 1) { | ||
tree[cur / 2] = tree[cur] + tree[(cur % 2 == 0) ? (cur + 1) : (cur - 1)]; | ||
cur /= 2; | ||
} | ||
} | ||
|
||
int query(int start, int end, int n, int left, int right) { | ||
if (left > end || right < start) | ||
return 0; | ||
|
||
if (left <= start && end <= right) | ||
return tree[n]; | ||
|
||
int mid = (start + end) / 2; | ||
return query(start, mid, 2 * n, left, right) + | ||
query(mid + 1, end, 2 * n + 1, left, right); | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cin >> N; | ||
for (int i = 0; i < N; i++) { | ||
cin >> t; | ||
V.push_back(t); | ||
} | ||
compress(V); | ||
|
||
init(0, N - 1, 1); | ||
|
||
for (int i = 0; i < N; i++) { | ||
update(V[i], 1); | ||
cout << i - query(0, N - 1, 1, 0, V[i] - 1) + 1 << "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters