模板:区间GCD

一、RMQ实现

        RMQ不能实现更新。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef long long LL;
const int MAXN = 100100;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int INF = 2e9;
const double EPS = 1e-6;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int input[MAXN];
int dp[MAXN][20];

int GCD(int a, int b) {
	int r;
	while (b) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}

//RMQ初始化
void init(int n) {
	for (int i = 0; i < n; ++i)
		dp[i][0] = input[i];
	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) - 1 < n; ++i)
			dp[i][j] = GCD(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}

int getGcd(int l, int r) {
	int k = 0;
	while ((1 << (k + 1)) <= r - l + 1)
		k++;
	return GCD(dp[l][k], dp[r - (1 << k) + 1][k]);
}

int main() {
	//system("pause");
	return 0;
}

二、线段树实现

        线段树能够实现更新

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef long long LL;
const int MAXN = 500500;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int INF = 2e9;
const double EPS = 1e-6;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

#define lson	(rt*2+1)
#define rson	(rt*2+2)

struct Node {
	int l, r;
	int val;
	int mid() { return (l + r) >> 1; }
};

Node tree[MAXN << 2];
int input[MAXN];

int GCD(int a, int b) {
	int r;
	while (b) {
		r = a%b;
		a = b;
		b = r;
	}
	return a;
}

void buildTree(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;

	if (l == r)
		tree[rt].val = input[l];
	else {
		int mid = tree[rt].mid();
		buildTree(lson, l, mid);
		buildTree(rson, mid + 1, r);
		tree[rt].val = GCD(tree[lson].val, tree[rson].val);
	}
}

//单点更新
void update(int rt, int pos, int val) {
	if (tree[rt].l == tree[rt].r) {
		tree[rt].val = val;
		return;
	}

	int mid = tree[rt].mid();
	if (pos <= mid)
		update(lson, pos, val);
	else
		update(rson, pos, val);

	tree[rt].val = GCD(tree[lson].val, tree[rson].val);
}

int query(int rt, int l, int r) {
	if (tree[rt].l == l && tree[rt].r == r)
		return tree[rt].val;

	int mid = tree[rt].mid();
	if (mid >= r)
		return query(lson, l, r);
	else if (mid < l)
		return query(rson, l, r);
	else {
		int q1 = query(lson, l, mid);
		int q2 = query(rson, mid + 1, r);
		return GCD(q1, q2);
	}
}

int main() {
	//system("pause");
	return 0;
}

你可能感兴趣的:(ACM,模板,数论)