Can you answer these queries VI 【SPOJ - GSS6】【Splay 最大子段和】

2023-10-28

题目链接


  这道题很容易看到,它是把操作给放到了Splay上面来做的,其实主要改变的就是一些pushup()里面的细节!左右子树没有节点的时候,可千万不要再去比较了!会WA死…… 呜呜呜,当时一直以为自己哪里没考虑到,后来一想,好像就是左右儿子节点不存在,而原来的左右最大是负数的时候,我们假如再去比较,就会得到一个实际上不存在的非负数。

  然后,剩下的,就是基本的操作了,注意细节,Splay真的不大好写…… 呜呜呜…… 写了好久好久。

  然后,为了方便,我在两头分别加入了a[1] = 0, a[N + 2] = 0,并且把原来的1~N变成了2~N+1,这样避免了在查询x-1~x的时候我们要去取x却取不到的情况了(基本操作)

  然后在这里用了加速,直接build了SplayTree,不去一个一个的插入,听说卡常……

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-6
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define max5(a, b, c, d, f) max4(a, b, c, max(d, f))
#define max6(a, b, c, d, f, g) max(max3(a, b, c), max3(d, f, g))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, a[maxN], root = 0, tot;
struct node
{
    int ff, siz, ch[2], lm, rm, mx, all, val;
    node() { ff = siz = ch[0] = ch[1] = lm = rm = mx = all = 0; }
    void get(int x) { lm = rm = mx = all = val = x; }
}t[maxN<<1];
inline void pushup(int rt)
{
    t[rt].lm = t[rt].rm = t[rt].mx = -INF;
    int lc = t[rt].ch[0], rc = t[rt].ch[1];
    t[rt].siz = t[lc].siz + t[rc].siz + 1;
    if(lc) t[rt].lm = t[lc].lm;
    if(rc) t[rt].rm = t[rc].rm;
    t[rt].lm = max3(t[rt].lm, t[lc].all + t[rt].val, t[lc].all + t[rt].val + t[rc].lm);
    t[rt].rm = max3(t[rt].rm, t[rc].all + t[rt].val, t[rc].all + t[rt].val + t[lc].rm);
    if(lc) t[rt].mx = t[lc].mx;
    if(rc) t[rt].mx = max(t[rt].mx, t[rc].mx);
    t[rt].mx = max5(t[rt].mx, t[lc].rm + t[rt].val + t[rc].lm, t[rt].val, t[lc].rm + t[rt].val, t[rc].lm + t[rt].val);
    t[rt].all = t[lc].all + t[rc].all + t[rt].val;
}
void build_Splay(int l, int r, int f)
{
    if(l > r) return;
    int mid = HalF;
    t[f].ch[mid > f] = mid;
    t[mid].ff = f;
    t[mid].siz = 1;
    t[mid].get(a[mid]);
    if(l == r) return;
    build_Splay(l, mid - 1, mid); build_Splay(mid + 1, r, mid);
    pushup(mid);
}
inline void Rotate(int x)
{
    int y = t[x].ff, z = t[y].ff, k = t[y].ch[1] == x;
    t[z].ch[t[z].ch[1] == y] = x;
    t[x].ff = z;
    t[y].ch[k] = t[x].ch[k^1];
    t[t[x].ch[k^1]].ff = y;
    t[x].ch[k^1] = y;
    t[y].ff = x;
    pushup(y); pushup(x);
}
inline void Splay(int x, int goal)
{
    while(t[x].ff != goal)
    {
        int y = t[x].ff, z = t[y].ff;
        if(z != goal) (t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? Rotate(x) : Rotate(y);
        Rotate(x);
    }
    if(!goal) root = x;
}
inline int Kth(int x)  //返回的是位置
{
    int u = root;
    if(t[u].siz < x) return 0;
    while(true)
    {
        int y = t[u].ch[0];
        if(x > t[y].siz + 1)
        {
            x -= t[y].siz + 1;
            u = t[u].ch[1];
        }
        else
        {
            if(t[y].siz >= x) u = y;
            else return u;
        }
    }
}
inline void Insert(int x, int val)
{
    int las = Kth(x), nex = Kth(x + 1);
    Splay(las, 0); Splay(nex, las);
    int u = nex;
    t[u].ch[0] = ++tot;
    u = tot;
    t[u].ff = nex;
    t[u].siz = 1;
    t[u].get(val);
    pushup(nex);
    pushup(las);
}
inline void Delet(int x)
{
    int las = Kth(x - 1), nex = Kth(x + 1);
    Splay(las, 0); Splay(nex, las);
    t[nex].ch[0] = 0;
    pushup(nex);
    pushup(las);
}
inline void Reset(int x, int val)
{
    int now = Kth(x); Splay(now, 0);
    t[now].get(val);
    pushup(now);
}
inline int query(int x, int y)
{
    int las = Kth(x - 1), nex = Kth(y + 1);
    Splay(las, 0); Splay(nex, las);
    int u = t[nex].ch[0];
    return t[u].mx;
}
int main()
{
    scanf("%d", &N);
    for(int i=2; i<=N + 1; i++) scanf("%d", &a[i]);
    tot = N + 2;
    a[1] = a[tot] = 0;
    build_Splay(1, N + 2, root);
    t[0].siz = 0; t[0].get(0); t[0].ch[0] = t[0].ch[1] = 0; t[0].ff = 0;
    root = (N + 3) >> 1;
    scanf("%d", &M);
    char op[3];
    int x, y;
    while(M--)
    {
        scanf("%s", op);
        if(op[0] == 'I')    //在x和x-1之间插入y——这里变成x~x+1之间插入一个数
        {
            scanf("%d%d", &x, &y);
            Insert(x, y);
        }
        else if(op[0] == 'D')   //删除x位置
        {
            scanf("%d", &x); x++;
            Delet(x);
        }
        else if(op[0] == 'R')   //用y替换x位置上的元素
        {
            scanf("%d%d", &x, &y); x++;
            Reset(x, y);
        }
        else    //查询x~y的最大子段和
        {
            scanf("%d%d", &x, &y); x++; y++;
            printf("%d\n", query(x, y));
        }
    }
    return 0;
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Can you answer these queries VI 【SPOJ - GSS6】【Splay 最大子段和】 的相关文章

  • juc辅助类

    目录 减少计数 CountDownLatch 循环栅栏 CyclicBarrier 减少计数 CountDownLatch 循环栅栏 CyclicBarrier 两者区别 信号灯 Semaphore juc辅助类有 减少计数 CountDo
  • Doxygen文档生成工具

    Doxygen代码文档生成工具 文章目录 Doxygen代码文档生成工具 Doxygen Doxygen的注释 vscode插件 Doxygen实际使用 Doxygen 根据百度百科说法 Doxygen是一种开源的 跨平台的文档系统 支持C

随机推荐

  • 记一次由于外部K8S集群证书到期导致Jenkins无法生成动态agent节点错误解决(入坑出坑)...

    欢迎关注 WeiyiGeek 公众号 点击 下方卡片 即可关注我哟 设为 星标 每天带你 基础入门 到 进阶实践 再到 放弃学习 涉及 网络安全运维 应用开发 物联网IOT 学习路径 个人感悟 等知识 花开堪折直须折 莫待无花空折枝 作者主
  • Flask虚拟环境

    一 虚拟环境 1 为什么需要虚拟环境 虚拟环境作用是将每个项目所需要的包隔离开形成一个独立的整体 每个虚拟环境互不干涉 方便与运行 因为如果有包有升级的话 可能会运行不了 2 安装使用虚拟环境 pipenv 2 1 安装虚拟环境 在系统命行
  • uview2.0自定义u-count-down倒计时

    1 效果展示 2 思路需要后端返回一个结束的时间戳 注意是毫秒时间戳 如果是秒需要在后面加3个0转为毫秒 获取当当前时间戳 当前时间戳减去商品结束的时间戳得出要倒计时的时间戳 然后再进行值得处理 3 代码展示
  • 分享一个放烟花的特效

    先看效果 再看代码
  • 如何mock系统调用

    背景 Linux下开发存储系统 网络库的时候会用到一系列Linux的系统调用 每一个系统调用都有一些出错的场景 有些场景很极端 比如内存使用达到上限 磁盘写满等 如果对其进行测试的话 很难去构造这样的一个场景 这个时候集成测试就显得力不存心
  • React如何从后端获取数据并渲染到前端?

    React js 自己的定位是 A JavaScript Library for building user interface 它的文档称许多人将它用作 MVC 的 V 因此 React js 不关心你是如何嵌入后端数据的 换句话说 我们
  • css进阶(背景颜色渐变、过渡)

    css进阶 背景颜色径向渐变 背景颜色线性渐变 过渡 折叠效果 背景颜色径向渐变 径向渐变 中间部分椭圆形 四周填充 由中间到四周渐变 background radial gradient red yellow 红椭圆填充黄色 backgr
  • 在vue3中使用富文本编辑wangEditor上传自定义图片

    富文本编辑器wangEditor的安装和使用我就不在这里做介绍了 大家可以去官网进行查看 wangeditor官网 https www wangeditor com 接下来 进入主题 上传本地图片到后端 后端返回图片地址 在插入编辑器中 c
  • Install Google-Chrome

    首先将google的yum源下载下来 google并没有直接提供yum源 而是以sh文件的方式提供 那么就下载这个文件 使用以下地址来下载文件 https dl ssl google com linux google repo setup
  • leetcode----JavaScript 详情题解(1)

    目录 2618 检查是否是类的对象实例 2619 数组原型对象的最后一个元素 2620 计数器 2621 睡眠函数 2622 有时间限制的缓存 2623 记忆函数 2625 扁平化嵌套数组 2626 数组归约运算 2627 函数防抖 261
  • mdltxdy && mjj的英语单词(4.2洛谷比赛中的字符串问题)

    enmmmm我的字符串是真的凉啊orz 导致我到现在都怀疑那位负责出题的mjj是不是故意在卡我QAQ 好啦话不多说 上题 1 mdltxdy 字符串替换问题 题意描述 mdl每天都在小分队里面被刷屏 因此她急切的找到了你希望你写一个程序屏蔽
  • 利用gdb调试nginx

    利用gdb调试nginx 1 打开nginx调试 首先修改 auto cc conf文件 将ngx compile opt c 改为ngx compile opt c g 打开debug模式进行编译 简单操作如下 sudo configur
  • 图结构的基础实现(java)

    package graph import edu princeton cs algs4 Bag import edu princeton cs algs4 In public class Graph private final int V
  • NCL windows系统安装

    http www doc88 com p 192266283281 html NCL在Linux下的安装非常容易 只需下载适当版本的文件 设置好环境变量即可使用 NCL在Windows下的安装则要麻烦一些 需要先安装一个虚拟Linux环境
  • python3 [爬虫入门实战]爬虫之scrapy爬取织梦者网站并存mongoDB

    主要爬取了编程栏目里的其他编程里的36638条数据 过程是自己一步一步的往下写的 有不懂的也是一边找笔记 一边百度 一边调试 遗憾 没有进行多栏目数据的爬取 只爬了一个栏目的数据 希望有想法的有钻研精神的可以自己去尝试爬取一下 难度应该不会
  • [Process] kill() returned unexpected error 1

    Process kill returned unexpected error 1 系统CatAlina xcode版本11 3 iOS 13 3运行的时候会出现 Process kill returned unexpected error
  • 【计算机视觉

    文章目录 一 分割 语义相关 9篇 1 1 OpenIns3D Snap and Lookup for 3D Open vocabulary Instance Segmentation 1 2 dacl10k Benchmark for S
  • CRNN+CTC实现不定长验证码识别(keras模型-示例篇)

    目录 前言 运行环境 生成数据集 构建网络模型 训练模型 测试模型 错误集锦 结语 前言 本文的重心在于如何使用以tensorflow作为后端的keras构建一个使用CTC为loss的简化版CRNN 同时指出构建过程中容易出错的地方 让像我
  • vscode使用delve调试golang程序

    环境配置 delve仓库 含有教程 https github com go delve delve golang的debugging教程 https github com golang vscode go wiki debugging gt
  • Can you answer these queries VI 【SPOJ - GSS6】【Splay 最大子段和】

    题目链接 这道题很容易看到 它是把操作给放到了Splay上面来做的 其实主要改变的就是一些pushup 里面的细节 左右子树没有节点的时候 可千万不要再去比较了 会WA死 呜呜呜 当时一直以为自己哪里没考虑到 后来一想 好像就是左右儿子节点