BZOJ1251 序列终结者

BZOJ1251 序列终结者


题目:1. 将\([L,R]\)这个区间内的所有数加上\(V\)。 2. 将\([L,R]\)这个区间翻转。 3. 求\([L,R]\)这个区间中的最大值。 最开始所有元素都是\(0\)

存个模板

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
typedef long long ll;
const int N = 100010;
const int inf = 0x3f3f3f3f;
template<class T> inline void read(T &x) {
    x = 0; char c = getchar(); T f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {x = x * 10 + c - '0'; c = getchar();}
    x *= f;
}
using namespace std;
int n, q, opt, l, r, v, k;
class Splay {
private :
    struct Node {
        int fa, ch[2], sz, rev, tag, mx, v; // rev 翻转标记,tag 加法标记
    } T[N];
    int root;

    #define lc T[p].ch[0]
    #define rc T[p].ch[1]
    #define pa T[p].fa

    inline int LR(int p) {return T[pa].ch[1] == p;}
    inline void PushUp(int p) {
        T[p].sz = T[lc].sz + T[rc].sz + 1;
        T[p].mx = max(T[p].v, max(T[lc].mx, T[rc].mx));
    }
    inline void PushDown(int p) {
        if(T[p].rev) {
            swap(lc,rc);
            if(lc) T[lc].rev ^= 1;
            if(rc) T[rc].rev ^= 1;
            T[p].rev = 0;
        }
        if(T[p].tag) {
            int &A = T[p].tag;
            if(lc) T[lc].tag += A, T[lc].mx += A, T[lc].v += A;
            if(rc) T[rc].tag += A, T[rc].mx += A, T[rc].v += A;
            A = 0;
        }
    }
    int Build(int l,int r,int f) {
        if(l > r) return 0;
        int p = (l + r) >> 1;
        lc = Build(l,p-1, p); rc = Build(p+1, r, p);
        T[p].fa = f;
        T[p].mx = T[p].v = T[p].rev = T[p].tag = 0;
        PushUp(p);
        return p;
    }
    inline void rotate(int p) {
        int f = T[p].fa, g = T[f].fa, c = LR(p);
        if(g) T[g].ch[LR(f)] = p; T[p].fa = g;
        T[f].ch[c] = T[p].ch[c^1]; T[T[f].ch[c]].fa=f;
        T[p].ch[c^1] = f; T[f].fa = p;
        PushUp(f); PushUp(p);
    }
    void splay(int p, int ed) {
        for(;T[p].fa != ed; rotate(p))
            if(T[pa].fa != ed) rotate(LR(p)==LR(pa)?pa:p);
        if(ed == 0) root = p;
    }
    inline int kth(int k) {
        int ls = 0, p = root;
        while(p) {
            PushDown(p);
            int tmp = T[lc].sz + ls;
            if( tmp < k && k <= tmp + 1) return p;
            if(k <= tmp) p = lc;
            else ls = tmp+1, p = rc;
        }
        return -1;
    }
public:
    int Init(int n) {
        T[0].mx = -inf;
        root = Build(1, n+2, 0);
    }
    void Add(int l, int r, int v) {
    // 操作[l,r],l-1splay到根,r+1splay到根的rc,r+1的lc就是我们要操作的序列
        int p = kth(l); splay(p, 0);
        p = kth(r+2), splay(p, root); 
        
        T[lc].tag += v; T[lc].mx += v; T[lc].v += v;
    }
    void Rev(int l, int r) {
        int p = kth(l); splay(p, 0);
        p = kth(r+2), splay(p, root);
        T[lc].rev ^= 1;  //先打标记,pushdown时进行翻转
    }
    void Ask(int l, int r) {
        int p = kth(l); splay(p, 0);
        p = kth(r+2); splay(p, root);
        printf("%d\n", T[lc].mx);
    }
} tree;
int main() {
    read(n), read(q);
    tree.Init(n);
    while(q--) {
        read(opt), read(l), read(r);
        if(opt == 1) read(v), tree.Add(l, r, v);
        else if(opt == 2) tree.Rev(l, r);
        else if(opt == 3) tree.Ask(l, r);
    }
}

转载于:https://www.cnblogs.com/RRRR-wys/p/10527814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值