分类 线段树 下的文章

BZOJ 1012 zkw

嗯嗯向我这种没智商的沙茶一定是会打残代码的。
同一个名称就不要乱用了啊(die。
或许说太久没打zkw忘了怎么打还是得翻以前的代码……
一言以蔽之,曰智商低。

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e5;
namespace FastIO
{
    template<class T>
    void read(T& x)
    {
        char ch = getchar(); T n = 1, a = 0;
        while(ch < '0' || ch > '9'){if(ch == '-') n = -1; ch = getchar();}
        while(ch >= '0' && ch <= '9'){a = a * 10 + ch - '0'; ch = getchar();}
        x = n * a;
    }
    template<class T, class U>
    void read(T& x, U& y)
    {
        read(x), read(y);
    }
}
using namespace FastIO;

struct info
{
    int ma;
    info(int x = 0) : ma(x){}
} A[maxn * 3];

int main()
{
    int m, d;
    read(m, d);
    int t = 0, cur = 0;
    int zkwt = 1;
    while(zkwt <= maxn + 3) zkwt <<= 1;
    while(m--)
    {
        char a[2];int n;
        scanf("%s", a);
        read(n);
        if(a[0] == 'A')
        {
            int i;
            for(A[i = cur + zkwt + 1] = info((n + t) % d), i >>= 1; i > 0; i >>= 1)
                A[i].ma = max(A[i * 2].ma, A[i * 2 + 1].ma); 
            cur++;
        }
        else
        {
            int ans = 0;
            for(int l = cur - n + zkwt, r = cur + zkwt + 1; l ^ r ^ 1; l >>= 1, r >>= 1)
            {
                if(~l & 1)
                    ans = max(ans, A[l ^ 1].ma);
                if(r & 1)
                    ans = max(ans, A[r ^ 1].ma);
            }
            printf("%d\n", t = ans);
        }
    }
    return 0;
}

HDU 1754

tagged-zkw.
稍后做一个教程。

namespace DEL
{
    const int INF = 1e9, maxn = 1 << 25;
    struct info
    {
        int Max;
        info(int x = -INF) : Max(x){}
    } A[maxn];
}
#include <cstdio>
#include <algorithm>
using namespace std;
using namespace DEL;
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        int t;
        for(t = 1; t <= n + 3; t <<= 1);
        for(int i = t + 1; i < t + 1 + n; i++)
        {
            int a;
            scanf("%d", &a);
            A[i] = info(a);
        }
        for(int i = t - 1; i > 0; i--)
            A[i].Max = max(A[i * 2].Max, A[i * 2 + 1].Max);
        while(m--)
        {
            static char c[maxn];int x, y;
            scanf("%s%d%d", &c, &x, &y);
            if(c[0] == 'Q')
            {
                int ans = 0;
                for(x += t - 1, y += t + 1; x ^ y ^ 1; x >>= 1, y >>= 1)
                {
                    if(~x & 1)
                        ans = max(ans, A[x ^ 1].Max);
                    if(y & 1)
                        ans = max(ans, A[y ^ 1].Max);
                }
                printf("%d\n", ans);
            }
            else
            {
                for(A[x += t] = info(y), x >>= 1; x > 0; x >>= 1)
                    A[x].Max = max(A[x * 2].Max, A[x * 2 + 1].Max);
            }
        }
    }
    return 0;
}

POJ3264

Segment tree.
祝卡cin/cout的出题者全·家·爆·炸!

#include <cstdio>
#include <cstring>
#include <limits> 
#include <algorithm>
using namespace std;
namespace DEL
{
    int n;
    template<int maxn, class T>
    class SegmentTree
    {
    private:
        T maxv[maxn*3], minv[maxn*3];
        T left, right, v;
        T _max, _min;
    public:
        void init()
        {
            memset(maxv, 0, sizeof maxv);
            memset(minv, 0, sizeof minv);
            _max = 0;
            _min = numeric_limits<T>::max(); 
        }
        void set(int l, int r, int _v)
        {
            left = l, right = r, v = _v;
        }
        void update(T o = 1, T L = 1, T R = n)
        {
            if(L == R)
            {
                maxv[o] += v;
                minv[o] += v;
            }
            else
            {
                T lc = o << 1, rc = lc + 1, M = (L + R) / 2;
                if(left <= M)
                    update(lc, L, M);
                else
                    update(rc, M + 1, R);
                maxv[o] = max(maxv[lc], maxv[rc]);
                minv[o] = min(minv[lc], minv[rc]);
            }
        }
        void query(T o = 1, T L = 1, T R = n)
        {
            if(left <= L && R <= right)
            {
                _max = max(_max, maxv[o]);
                _min = min(_min, minv[o]);
            }
            else
            {
                T lc = o << 1, rc = lc + 1, M = (L + R) / 2;
                if(left <= M)
                    query(lc, L, M);
                if(M < right)
                    query(rc, M + 1, R);
            }
        }
        T get()
        {
            int ans = _max - _min;
            _max = 0;
            _min = numeric_limits<T>::max(); 
            return ans;
        }
    };
}
using namespace DEL;
SegmentTree<(int)5e4 + 10, int> st;
int q;
int main()
{
    st.init();
    scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; i++)
    {
        int tmp;
        scanf("%d", &tmp);
        st.set(i, 0, tmp);
        st.update();
    }
    while(q--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        st.set(a, b, 0);
        st.query();
        printf("%d\n", st.get());
    }
    return 0;
}

HDU 1698

Segment Tree.
但是区间修改前缀和的话不一定需要线段树吧……有时间再想想……

#include <cstdio>
#include <cstring>
using namespace std;
namespace DEL
{
    int n;
    template<int maxn, class T>
    class SegmentTree
    {
    private:
        T setv[maxn*3], sumv[maxn*3];
        T left, right, v;
        void maintain(T o, T L, T R)
        {
            int M = (L + R) >> 1, lc = o << 1, rc = lc + 1;
            sumv[o] = 0;
            if(L < R)
                sumv[o] = sumv[lc] + sumv[rc];
            if(setv[o] != 0)
                sumv[o] = setv[o] * (R - L + 1);
        }
        void pushdown(int o)
        {
            T lc = o << 1, rc = lc + 1;
            if(setv[o] != 0)
            {   
                setv[lc] = setv[rc] = setv[o];
                setv[o] = 0;
            }
        }

    public:
        void init()
        {
            memset(setv, 0, sizeof setv);
            memset(sumv, 0, sizeof sumv);
        }

        void set(T _left, T _right, T _v)
        {
            left = _left, right = _right, v = _v;
        }

        void update(T o = 1, T L = 1, T R = n)
        {
            if(left <= L && R <= right)
                setv[o] = v;
            else
            {
                pushdown(o);
                int M = (L + R) >> 1, lc = o << 1, rc = lc + 1;
                if(left <= M)
                    update(lc, L, M);
                else
                    maintain(lc, L, M);
                if(M < right)
                    update(rc, M + 1, R);
                else
                    maintain(rc, M + 1, R);
            }
            maintain(o, L, R);
        }

        T getans()
        {
            return sumv[1];
        }
    };
}
using namespace DEL;
SegmentTree<int(1e5), int> st;
int q;
int main()
{
    int t;
    scanf("%d", &t);
    for(int i = 1; i <= t; i++)
    {
        st.init();
        scanf("%d%d", &n, &q);
        for(int j = 1; j <= n; j++)
        {
            st.set(j, j, 1);
            st.update();
        }
        while(q--)
        {
            int l, r, v;
            scanf("%d%d%d", &l, &r, &v);
            st.set(l, r, v);
            st.update();
        }
        printf("Case %d: The total value of the hook is %d.\n", i, st.getans());
    }
    return 0;
}

POJ 3468

Segment Tree

#include <cstdio>
#include <cstring>
using namespace std;
namespace DEL
{
    int n, q;
    template<int maxn, class T>
    class SegmentTree
    {
    private:
        T sumv[maxn*3], addv[maxn*3];
        
        inline void maintain(T o, T L, T R)
        {
            T lc = o << 1, rc = lc + 1, M = (L + R) / 2;
            sumv[o] = 0;
            if(L < R)
                sumv[o] = sumv[lc] + sumv[rc];
            sumv[o] += addv[o] * (R - L + 1);
        }
    public:
        void init()
        {
            memset(addv, 0, sizeof addv);
            memset(sumv, 0, sizeof sumv);
        }
        
        T yl, yr, v, ans;
        void update(T o = 1, T L = 1, T R = n)
        {
            if(yl <= L && R <= yr)
                addv[o] += v;
            else
            {
                T lc = o << 1, rc = lc + 1, M = (L + R) >> 1;
                if(yl <= M)
                    update(lc, L, M);
                if(M < yr)
                    update(rc, M + 1, R);
            }
            maintain(o, L, R);
        }
        
        void query(T o = 1, T L = 1, T R = n, T add = 0)
        {
            if(yl <= L && R <= yr)
                ans += sumv[o] + add * (R - L + 1);
            else
            {
                T lc = o << 1, rc = lc + 1, M = (L + R) / 2;
                if(yl <= M)
                    query(lc, L, M, add + addv[o]);
                if(M < yr)
                    query(rc, M + 1, R, add + addv[o]);
            }
        }
    };
}
using namespace DEL;
SegmentTree<int(1e5), long long> t;
int main()
{
    scanf("%d%d", &n, &q);
    t.init();
    for(int i = 1; i <= n; i++)
    {
        t.yl = t.yr = i;
        scanf("%I64d", &t.v);
        t.update();
    }
    char c[2];
    while(q--)
    {
        scanf("%s", &c);
        if(c[0] == 'Q')
        {
            scanf("%I64d%I64d%I64d", &t.yl, &t.yr, &t.v);
            t.ans = 0;
            t.query();
            printf("%I64d\n", t.ans);
        }
        else if(c[0] == 'C')
        {
            scanf("%I64d%I64d%I64d", &t.yl, &t.yr, &t.v);
            t.update();
        }
    }
    return 0;
}
bst g22 jinniu lilai opebet orange88 vinbet xbet yuebo zunlong shijiebei bet007 hg0088 ju111 letiantang m88 mayaba qg777 qianyiguoji sbf777 tengbohui tlc ule weilianxier waiweitouzhu xingfayule xinhaotiandi yinheyule youfayule zhongying 2018shijiebei w88 18luck 188bet beplay manbet 12bet 95zz shenbo weide1946 ca88 88bifa aomenxinpujing betway bodog bt365 bwin tongbao vwin weinisiren 88jt fenghuangyule hongyunguoji 918botiantang huanyayule jianada28 jixiangfang libo long8 hongzuyishi zuqiutouzhu