rename Range queries -> range_queries (#646)

This commit is contained in:
Christian Clauss
2019-11-28 13:28:16 +01:00
committed by GitHub
parent 886960a3c2
commit fb2bef8cd0
4 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
#include<iostream>
using namespace std;
/**
* ` lowbit(x) ` aims to find the last 1 in binary of a positive number
* twos complement works good on this
* also using ` x - (x & (x - 1)) `
*/
#define lowbit(x) (x & (-x) )
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) {
tree[x] += c;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while(x) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
int query_segement(int l, int r) {
return query(r) - query(l - 1);
}
int main() {
cin >> range;
for(int i = 1; i <= range; i++) {
int num;
cin >> num;
update(i, num);
}
int q;
cin >> q;
while(q--) {
int op;
cin >> op;
if(op == 0) {
int l, r;
cin >> l >> r;
cout << query_segement(l, r) << endl;
} else {
int x, c;
cin >> x >> c;
update(x, c);
}
}
return 0;
}

77
range_queries/MO.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 5;
int a[N], bucket[N], cnt[N];
int bucket_size;
struct query
{
int l, r, i;
} q[N];
int ans = 0;
void add(int index)
{
cnt[a[index]]++;
if (cnt[a[index]] == 1)
ans++;
}
void remove(int index)
{
cnt[a[index]]--;
if (cnt[a[index]] == 0)
ans--;
}
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 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++)
{
scanf("%d %d", &q[i].l, &q[i].r);
q[i].l--;
q[i].r--;
q[i].i = i;
}
sort(q, q + t, mycmp);
int left = 0, right = 0;
for (i = 0; i < t; i++)
{
int L = q[i].l, R = q[i].r;
while (left < L)
{
remove(left);
left++;
}
while (left > L)
{
add(left - 1);
left--;
}
while (right <= R)
{
add(right);
right++;
}
while (right > R + 1)
{
remove(right - 1);
right--;
}
bucket[q[i].i] = ans;
}
for (i = 0; i < t; i++)
printf("%d\n", bucket[i]);
return 0;
}

76
range_queries/bit.cpp Normal file
View File

@@ -0,0 +1,76 @@
// Binary Indexed Tree.
#include<bits/stdc++.h>
using namespace std;
class Bit
{
int n;
vector<int> bit;
inline int offset(int x)
{
return (x & (-x));
}
public:
Bit(vector<int>& arr)
{
n = arr.size();
bit.assign(n + 1, 0);
for (int i = 0; i < n; ++i)
{
update(i, arr[i]);
}
}
Bit(int x)
{
n = x;
bit.assign(n + 1, 0);
}
void update(int id, int val)
{
// Add val at id
id++;
while (id <= n)
{
bit[id] += val;
id += offset(id);
}
}
int sum(int id)
{
// Get prefix sum upto id.
id++;
int res = 0;
while (id > 0)
{
res += bit[id];
id -= offset(id);
}
return res;
}
int sum_range(int l, int r)
{
return sum(r) - sum(l - 1);
}
};
int main()
{
int n = 5;
vector<int> arr = { 1, 2, 3, 4, 5 };
Bit x(arr);
assert(x.sum_range(0, 0) == 1);
assert(x.sum_range(0, 1) == 3);
assert(x.sum_range(0, 2) == 6);
x.update(0, 6);
assert(x.sum_range(0, 0) == 6);
assert(x.sum_range(0, 1) == 8);
assert(x.sum_range(0, 2) == 11);
return 0;
}

91
range_queries/segTree.cpp Normal file
View File

@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
#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)
{
segtree[pos] = arr[low];
return;
}
ll mid = (low + high) / 2;
ConsTree(arr, segtree, low, mid, 2 * pos + 1);
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)
{
if (low > high)
return 0;
if (qlow > high || qhigh < low)
return 0;
if (lazy[pos] != 0)
{
segtree[pos] += lazy[pos] * (high - low + 1);
if (low != high)
{
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
lazy[pos] = 0;
}
if (qlow <= low && qhigh >= high)
return segtree[pos];
ll mid = (low + high) / 2;
return query(segtree, lazy, qlow, qhigh, low, mid, 2 * pos + 1) + 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)
{
if (low > high)
return;
if (lazy[pos] != 0)
{
segtree[pos] += lazy[pos] * (high - low + 1);
if (low != high)
{
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
lazy[pos] = 0;
}
if (start > high || end < low)
return;
if (start <= low && end >= high)
{
segtree[pos] += delta * (high - low + 1);
if (low != high)
{
lazy[2 * pos + 1] += delta;
lazy[2 * pos + 2] += delta;
}
return;
}
ll mid = (low + high) / 2;
update(segtree, lazy, start, end, delta, low, mid, 2 * pos + 1);
update(segtree, lazy, start, end, delta, mid + 1, high, 2 * pos + 2);
segtree[pos] = segtree[2 * pos + 1] + segtree[2 * pos + 2];
}
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--)
{
scanf("%lld", &choice);
if (choice == 0)
{
scanf("%lld %lld %lld", &p, &q, &v);
update(segtree, lazy, p - 1, q - 1, v, 0, n - 1, 0);
}
else
{
scanf("%lld %lld", &p, &q);
printf("%lld\n", query(segtree, lazy, p - 1, q - 1, 0, n - 1, 0));
}
}
return 0;
}