formatting source-code for 153fb7b8a5

This commit is contained in:
github-actions
2020-05-30 04:02:09 +00:00
parent 92fe9495ec
commit 8a2de9842b
175 changed files with 1671 additions and 3460 deletions

View File

@@ -3,46 +3,38 @@
using namespace std;
class Bit
{
class Bit {
int n;
vector<int> bit;
inline int offset(int x) { return (x & (-x)); }
public:
Bit(vector<int>& arr)
{
Bit(vector<int>& arr) {
n = arr.size();
bit.assign(n + 1, 0);
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
update(i, arr[i]);
}
}
Bit(int x)
{
Bit(int x) {
n = x;
bit.assign(n + 1, 0);
}
void update(int id, int val)
{
void update(int id, int val) {
// Add val at id
id++;
while (id <= n)
{
while (id <= n) {
bit[id] += val;
id += offset(id);
}
}
int sum(int id)
{
int sum(int id) {
// Get prefix sum upto id.
id++;
int res = 0;
while (id > 0)
{
while (id > 0) {
res += bit[id];
id -= offset(id);
}
@@ -52,8 +44,7 @@ class Bit
int sum_range(int l, int r) { return sum(r) - sum(l - 1); }
};
int main()
{
int main() {
int n = 5;
vector<int> arr = {1, 2, 3, 4, 5};
Bit x(arr);

View File

@@ -12,19 +12,15 @@ const int maxn = 1e5 + 7;
int tree[maxn] = {0},
range; // segement of [1...range], notice it must be less than `maxn`
void update(int x, int c)
{
while (x <= range)
{
void update(int x, int c) {
while (x <= range) {
tree[x] += c;
x += lowbit(x);
}
}
int query(int x)
{
int query(int x) {
int ans = 0;
while (x)
{
while (x) {
ans += tree[x];
x -= lowbit(x);
}
@@ -32,29 +28,23 @@ int query(int x)
}
int query_segement(int l, int r) { return query(r) - query(l - 1); }
int main()
{
int main() {
cin >> range;
for (int i = 1; i <= range; i++)
{
for (int i = 1; i <= range; i++) {
int num;
cin >> num;
update(i, num);
}
int q;
cin >> q;
while (q--)
{
while (q--) {
int op;
cin >> op;
if (op == 0)
{
if (op == 0) {
int l, r;
cin >> l >> r;
cout << query_segement(l, r) << endl;
}
else
{
} else {
int x, c;
cin >> x >> c;
update(x, c);

View File

@@ -3,41 +3,35 @@ using namespace std;
const int N = 1e6 + 5;
int a[N], bucket[N], cnt[N];
int bucket_size;
struct query
{
struct query {
int l, r, i;
} q[N];
int ans = 0;
void add(int index)
{
void add(int index) {
cnt[a[index]]++;
if (cnt[a[index]] == 1)
ans++;
}
void remove(int index)
{
void remove(int index) {
cnt[a[index]]--;
if (cnt[a[index]] == 0)
ans--;
}
bool mycmp(query x, query y)
{
bool mycmp(query x, query y) {
if (x.l / bucket_size != y.l / bucket_size)
return x.l / bucket_size < y.l / bucket_size;
return x.r < y.r;
}
int main()
{
int main() {
int n, t, i, j, k = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
bucket_size = ceil(sqrt(n));
scanf("%d", &t);
for (i = 0; i < t; i++)
{
for (i = 0; i < t; i++) {
scanf("%d %d", &q[i].l, &q[i].r);
q[i].l--;
q[i].r--;
@@ -45,26 +39,21 @@ int main()
}
sort(q, q + t, mycmp);
int left = 0, right = 0;
for (i = 0; i < t; i++)
{
for (i = 0; i < t; i++) {
int L = q[i].l, R = q[i].r;
while (left < L)
{
while (left < L) {
remove(left);
left++;
}
while (left > L)
{
while (left > L) {
add(left - 1);
left--;
}
while (right <= R)
{
while (right <= R) {
add(right);
right++;
}
while (right > R + 1)
{
while (right > R + 1) {
remove(right - 1);
right--;
}

View File

@@ -3,10 +3,8 @@
#define MAX 4000000
using namespace std;
typedef long long ll;
void ConsTree(ll arr[], ll segtree[], ll low, ll high, ll pos)
{
if (low == high)
{
void ConsTree(ll arr[], ll segtree[], ll low, ll high, ll pos) {
if (low == high) {
segtree[pos] = arr[low];
return;
}
@@ -15,17 +13,14 @@ void ConsTree(ll arr[], ll segtree[], ll low, ll high, ll pos)
ConsTree(arr, segtree, mid + 1, high, 2 * pos + 2);
segtree[pos] = segtree[2 * pos + 1] + segtree[2 * pos + 2];
}
ll query(ll segtree[], ll lazy[], ll qlow, ll qhigh, ll low, ll high, ll pos)
{
ll query(ll segtree[], ll lazy[], ll qlow, ll qhigh, ll low, ll high, ll pos) {
if (low > high)
return 0;
if (qlow > high || qhigh < low)
return 0;
if (lazy[pos] != 0)
{
if (lazy[pos] != 0) {
segtree[pos] += lazy[pos] * (high - low + 1);
if (low != high)
{
if (low != high) {
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
@@ -38,15 +33,12 @@ ll query(ll segtree[], ll lazy[], ll qlow, ll qhigh, ll low, ll high, ll pos)
query(segtree, lazy, qlow, qhigh, mid + 1, high, 2 * pos + 2);
}
void update(ll segtree[], ll lazy[], ll start, ll end, ll delta, ll low,
ll high, ll pos)
{
ll high, ll pos) {
if (low > high)
return;
if (lazy[pos] != 0)
{
if (lazy[pos] != 0) {
segtree[pos] += lazy[pos] * (high - low + 1);
if (low != high)
{
if (low != high) {
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
@@ -54,11 +46,9 @@ void update(ll segtree[], ll lazy[], ll start, ll end, ll delta, ll low,
}
if (start > high || end < low)
return;
if (start <= low && end >= high)
{
if (start <= low && end >= high) {
segtree[pos] += delta * (high - low + 1);
if (low != high)
{
if (low != high) {
lazy[2 * pos + 1] += delta;
lazy[2 * pos + 2] += delta;
}
@@ -69,23 +59,18 @@ void update(ll segtree[], ll lazy[], ll start, ll end, ll delta, ll low,
update(segtree, lazy, start, end, delta, mid + 1, high, 2 * pos + 2);
segtree[pos] = segtree[2 * pos + 1] + segtree[2 * pos + 2];
}
int main()
{
int main() {
ll n, c;
scanf("%lld %lld", &n, &c);
ll arr[n] = {0}, p, q, v, choice;
ll segtree[MAX], lazy[MAX] = {0};
ConsTree(arr, segtree, 0, n - 1, 0);
while (c--)
{
while (c--) {
scanf("%lld", &choice);
if (choice == 0)
{
if (choice == 0) {
scanf("%lld %lld %lld", &p, &q, &v);
update(segtree, lazy, p - 1, q - 1, v, 0, n - 1, 0);
}
else
{
} else {
scanf("%lld %lld", &p, &q);
printf("%lld\n", query(segtree, lazy, p - 1, q - 1, 0, n - 1, 0));
}