zoukankan      html  css  js  c++  java
  • 洛谷P3113 [USACO14DEC]马拉松赛跑Marathon_Gold 线段树维护区间最大值 模板

    如此之裸…
    Code:

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxn = 200000 + 3;
    int x[maxn], y[maxn], n, m, len;
    long long  nex[maxn], nexx[maxn], maxv[maxn << 2], C[maxn];
    inline long long dist(int i,int j){ return abs(x[i] - x[j]) + abs(y[i] - y[j]); }
    void build(int l,int r , int o)
    {
        if(l > r) return ;
        if(l == r)
        {
            maxv[o] = nexx[l];
            return;
        }
        int mid = (l + r) >> 1, ls = (o << 1), rs = (o << 1)|1;
        build(l, mid , ls);
        build(mid + 1, r, rs);
        maxv[o] = max(maxv[ls], maxv[rs]);
    }
    inline long long query(int l,int r,int L,int R,int o)
    {
        if(l > r || r < L || l > R) return 0;
        if(l >= L && r <= R) 
        {
            return maxv[o];
        }
        int mid = (l + r) >> 1, ls = (o << 1), rs = (o << 1) | 1;
        long long ans = 0;
        ans = max(ans, query(l, mid,L,R,ls));
        ans = max(ans, query(mid + 1, r, L,R,rs));
        return ans;
    }
    void change(int l,int r,int pos,int o)
    {
        if(l > r || r < pos || l > pos) return ;
        if(l == r)
        {
            maxv[o] = nexx[pos];
            return ;
        }
        int mid = (l + r) >> 1, ls = (o<<1), rs = (o << 1) |1;
        change(l,mid,pos,ls);
        change(mid + 1,r,pos,rs);
        maxv[o] = max(maxv[ls], maxv[rs]);
    }
    int lowbit(int t){ return t & (-t); }
    inline void update(int x,long long delta)
    {
        while(x <= n)
        {
            C[x] += delta;
            x += lowbit(x);
        }
    }
    inline long long ask(int x)
    {
        long long sums = 0;
        while(x > 0)
        {
            sums += C[x];
            x -= lowbit(x);
        }
        return sums;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n; ++i)    scanf("%d%d",&x[i],&y[i]);
        for(int i = 1;i <= n;  ++i)  { if(i < n)nex[i] = dist(i, i + 1); update(i, dist(i,i - 1));}
        for(int i = 1;i <= n - 2; ++i) nexx[i] = nex[i] + nex[i + 1] - dist(i, i + 2);
        len = n - 2;
        build(1, len, 1);
        for(int i = 1;i <= m; ++i)
        {
            char op[10];
            scanf("%s",op);
            if(op[0] == 'Q')
            {
                int st, ed;
                scanf("%d%d",&st,&ed);
                long long s_ed = ask(ed);
                long long s_st = ask(st);
                printf("%lld
    ",s_ed - s_st - query(1,len,st,ed - 2,1));
            }
            else
            {
                int id, a,b;
                scanf("%d%d%d",&id,&a,&b);
                update(id, -dist(id - 1, id));
                update(id + 1, -dist(id, id + 1));
                x[id] = a, y[id] = b;
                nex[id - 1] = dist(id - 1, id);
                nex[id] = dist(id, id + 1); 
                if(id + 2 <= n)
                {
                    nexx[id] = nex[id] + nex[id + 1] - dist(id, id + 2);
                    change(1,len, id, 1);
                }
                if(id - 1 > 0 )
                {
                    nexx[id - 1] = nex[id - 1] + nex[id] - dist(id - 1, id + 1);
                    change(1,len,id - 1,1);
                }
                if(id - 2 >= 1)
                {
                    nexx[id -2] = nex[id - 2] + nex[id - 1] - dist(id - 2, id);
                    change(1,len,id - 2,1);
                }
                update(id, dist(id - 1, id));
                update(id + 1, dist(id, id + 1));
            }
        }
        return 0;
    }
    
  • 相关阅读:
    day11 函数的进阶
    day10 文件的补充以及函数
    day9 文件处理
    day8 字典的补充以及集合
    Vue学习下
    前端常用js方法集
    js实现千位符格式化
    Vue项目总结上
    VUE项目github
    http://www.jianshu.com/p/42e11515c10f#
  • 原文地址:https://www.cnblogs.com/guangheli/p/9845166.html
Copyright © 2011-2022 走看看