Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя javacoder1

Автор javacoder1, история, 8 лет назад, По-английски

Hey , i was learning persistent segment trees , i got what the concept behind it and now i have to solve problems.I guess people out here might be having some built in templates , can you share yours so that i can see how to efficiently code it as i read that too much pointers can cause TLE.

HOW TO UPDATE RANGES USING LAZY PROPAGATION IN THIS METHOD

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

But you shouldn't be having any problems with pointers since you're "javacoder" right? :P

And check this link for a tutorial and with sample code too.

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hi, Here an interesting problem : E. The Classic Problem and my solution

i think if you explored the accepted codes you will find what you need.

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by javacoder1 (previous revision, new revision, compare).

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I use the code posted here , it doesn't use pointers

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Does someone have a max/min template for 2D fenwick?
If it is possible?

I have for one dimensional case, though I don't really understand it but it is fast :D .Link

»
8 лет назад, # |
Rev. 3   Проголосовать: нравится +6 Проголосовать: не нравится

Actually too much pointers wont cause TLE, but dynamic allocation would. You can use pointers with static pool of nodes. Here is code for this problem

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится +14 Проголосовать: не нравится

YOUR SHIFT SEEMS TO BE STUCK

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone suggest me more questions prefably on codeforces solved using this method which has not been mentioned?

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

How to handle range updates in persistent segment trees. Point updates for a particular query means that we have to change only log(n) nodes in path from root to leaf so for q queries space complexity would be n+qlog(n) but for the case of range updates i would keep a lazy value at each node but the problem i am facing is that the node to be updated would be representing some range so to change it would mean recreating the whole structure beneath it which be be too much costly considering the fact that there might be more range updates in the queries.Can someone suggest how to implement this?

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

PLease help me , i am stuck

»
6 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Can someone explain how to update ranges in a persistent segment tree?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have one for you. This is a solution of problem HORRIBLE of SPOJ which uses lazy propagation and persistent segment tree.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct node {
    ll value;
    ll lazy;
    node *l, *r;
    node() {
        value = lazy = 0;
        l = r = NULL;
    }
};

void lazy(node *&t, ll l, ll r) {
    if (t->lazy != 0) {
        t->value += t->lazy * (r - l + 1);
        // if t is not a leaf
        if (l < r) {
            if (t->l == NULL) t->l = new node();
            if (t->r == NULL) t->r = new node();
            t->l->lazy += t->lazy;
            t->r->lazy += t->lazy;
        }
        t->lazy = 0;
    }
}

void update(node *&t, ll l, ll r, int x, int y, ll v) {
    // if t == NULL then t hasn't been visited and hasn't been passed any lazy value
    if (t == NULL) t = new node();
    else lazy(t, l, r);
    if (l > y || r < x) return;
    if (l >= x && r <= y) {
        t->value += v * (r - l + 1);
        if (l < r) {
            if (t->l == NULL) t->l = new node();
            if (t->r == NULL) t->r = new node();
            t->l->lazy += v;
            t->r->lazy += v;
        }
        return;
    }
    int m = (l + r) >> 1;
    update(t->l, l, m, x, y, v);
    update(t->r, m + 1, r, x, y, v);
    t->value = t->l->value + t->r->value;
}

ll get(node *t, int l, int r, int x, int y) {
    if (t == NULL) return 0;
    lazy(t, l, r);
    if (l > y || r < x) return 0;
    if (l >= x && r <= y) return t->value;
    int m = (l + r) >> 1;
    return get(t->l, l, m, x, y) + get(t->r, m + 1, r, x, y);
}

// free memory
void del(node *&t) {
    if (t == NULL) return;
    del(t->l);
    del(t->r);
    delete t;
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        node *t = NULL;
        int n, c;
        scanf("%d %d", &n, &c);
        while (c--) {
            int i;
            scanf("%d", &i);
            if (i == 0) {
                int p, q, v;
                scanf("%d %d %d", &p, &q, &v);
                update(t, 1, n, p, q, v);
            } else {
                int p, q;
                scanf("%d %d", &p, &q);
                printf("%lld\n", get(t, 1, n, p, q));
            }
        }
        del(t);
    }
}