Skip to content

Commit

Permalink
仅支持单点修改的可持久化线段树(未验证正确性)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS authored Dec 3, 2016
1 parent d776d17 commit d43ff50
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions Persistent-Segment-Tree(Sum).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <cstdio>

#define ELEMENT_COUNT 100000
#define EDITION_COUNT 100000

using namespace std;

struct node
{
int l, r, sum;
node *lch, *rch;
}*root[EDITION_COUNT];

int n, d[ELEMENT_COUNT], hs = 0;

void build(int l, int r, node *u)
{
u->l = l;
u->r = r;
if (l == r)
{
u->sum = d[l];
}
else
{
int mid = (l + r) >> 1;
build(l, mid, u->lch = new node);
build(mid + 1, r, u->rch = new node);
u->sum = u->lch->sum + u->rch->sum;
}
}

int query(int l, int r, node *u)
{
if (u->l == l && u->r == r)
{
return u->sum;
}
else
{
int mid = (u->l + u->r) >> 1;
if (r <= mid)
{
return query(l, r, u->lch);
}
else if (l > mid)
{
return query(l, r, u->rch);
}
else
{
return query(l, mid, u->lch) + query(mid + 1, r, u->rch);
}
}
}

void modify(int k, int v, node *u, node *ori)
{
u->l = ori->l;
u->r = ori->r;
if (u->l == u->r)
{
u->sum = v;
}
else
{
int mid = (u->l + u->r) >> 1;
if (k <= mid)
{
u->rch = ori->rch;
u->lch = new node;
modify(k, v, u->lch, ori->lch);
}
else
{
u->lch = ori->lch;
u->rch = new node;
modify(k, v, u->rch, ori->rch);
}
u->sum = u->lch->sum + u->rch->sum;
}
}

int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &d[i]);
}
build(1, n, root[0] = new node);
int in, edition, ql, qr, k, v;
while (true)
{
scanf("%d", &in);
if (in == 1)
{
scanf("%d%d%d", &edition, &ql, &qr);
printf("%d\n", query(ql, qr, root[edition]));
}
else if (in == 2)
{
scanf("%d%d", &k, &v);
hs++;
modify(k, v, root[hs] = new node, root[hs - 1]);
}
else if (in == 3)
{
printf("Current edition : %d\n", hs);
}
else if (in == 0)
{
return 0;
}
else
{
printf("No such command!\n");
}
}
return 0;
}

0 comments on commit d43ff50

Please sign in to comment.