[Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)

无聊学了一下珂朵莉树

珂朵莉树好哇,是可以维护区间x次方和查询的高效数据结构。

思想大致就是一个暴力(相对而言)的树形数据结构

lxl毒瘤太强了,发明了ODT算法(Old Driver Tree老司机算法)

这里有一个大佬ACDreamer的题解

附上链接https://www.luogu.org/blog/ACdreamer/solution-cf896c

还有一个B站的讲解视频

附上链接https://www.bilibili.com/video/av21651173

我不会用珂朵莉树,但是就是大致了解了一下吧

毕竟普及蒟蒻听不懂省选算法。

这里附上大佬的程序

#include<cstdio>
#include<set>
#include<vector>
#include<utility>
#include<algorithm>
#define IT set<node>::iterator
using std::set;
using std::vector;
using std::pair;
typedef long long LL;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int imax_n = 1e5 + 7;

LL pow(LL a, LL b, LL mod)
{
    LL res = 1;
    LL ans = a % mod;
    while (b)
    {
        if (b&1) res = res * ans % mod;
        ans = ans * ans % mod;
        b>>=1;
    }
    return res;
}

struct node
{
    int l,r;
    mutable LL v;
    node(int L, int R=-1, LL V=0):l(L), r(R), v(V) {}
    bool operator<(const node& o) const
    {
        return l < o.l;
    }
};

set<node> s;

IT split(int pos)
{
    IT it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) return it;
    --it;
    int L = it->l, R = it->r;
    LL V = it->v;
    s.erase(it);
    s.insert(node(L, pos-1, V));
    return s.insert(node(pos, R, V)).first;
}

void add(int l, int r, LL val=1)
{
    IT itl = split(l),itr = split(r+1);
    for (; itl != itr; ++itl) itl->v += val;
}

void assign_val(int l, int r, LL val=0)
{
    IT itl = split(l),itr = split(r+1);
    s.erase(itl, itr);
    s.insert(node(l, r, val));
}

LL rank(int l, int r, int k)
{
    vector<pair<LL, int> > vp;
    IT itl = split(l),itr = split(r+1);
    vp.clear();
    for (; itl != itr; ++itl)
        vp.push_back(pair<LL,int>(itl->v, itl->r - itl->l + 1));
    std::sort(vp.begin(), vp.end());
    for (vector<pair<LL,int> >::iterator it=vp.begin();it!=vp.end();++it)
    {
        k -= it->second;
        if (k <= 0) return it->first;
    }
    return -1LL;
}

LL sum(int l, int r, int ex, int mod)
{
    IT itl = split(l),itr = split(r+1);
    LL res = 0;
    for (; itl != itr; ++itl)
        res = (res + (LL)(itl->r - itl->l + 1) * pow(itl->v, LL(ex), LL(mod))) % mod;
    return res;
}

int n, m;
LL seed, vmax;

LL rnd()
{
    LL ret = seed;
    seed = (seed * 7 + 13) % MOD7;
    return ret;
}

LL a[imax_n];

int main()
{
    scanf("%d %d %lld %lld",&n,&m,&seed,&vmax);
    for (int i=1; i<=n; ++i)
    {
        a[i] = (rnd() % vmax) + 1;
        s.insert(node(i,i,a[i]));
    }
    s.insert(node(n+1, n+1, 0));
    int lines = 0;
    for (int i =1; i <= m; ++i)
    {
        int op = int(rnd() % 4) + 1;
        int l = int(rnd() % n) + 1;
        int r = int(rnd() % n) + 1;
        if (l > r)
            std::swap(l,r);
        int x, y;
        if (op == 3)
            x = int(rnd() % (r-l+1)) + 1;
        else
            x = int(rnd() % vmax) +1;
        if (op == 4)
            y = int(rnd() % vmax) + 1;
        if (op == 1)
            add(l, r, LL(x));
        else if (op == 2)
            assign_val(l, r, LL(x));
        else if (op == 3)
            printf("%lld\n",rank(l, r, x));
        else
            printf("%lld\n",sum(l, r, x, y));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fsy2017/p/9781111.html

时间: 2024-08-24 04:24:53

[Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)的相关文章

CF896C Willem, Chtholly and Seniorious 珂朵莉树

问题描述 CF896C LG-CF896C 题解 我expect就是T飞,从这里跳下去,也不碰和珂朵莉相关的任何东西. 珂朵莉树真好使. 珂朵莉树模板. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #define int long long #define IT set<node>::iterator template <typename Tp> void read(Tp &x){

Solution: 题解 CF896C Willem, Chtholly and Seniorious(线段树解珂朵莉树)

Intro: 珂朵莉树模板题 怎么所有题解都是珂朵莉树啊啊啊啊 于是本蒟蒻决定来一发中(feng)规(kuang)中(luan)矩(gao)的线段树 首先这棵线段树只维护懒标记 来一发定义 线段树节点\(u\)维护区间\([l_u,r_u]\)的内容 懒标记\(t_u\):当\(t_u\not=0\)时表示区间\([l_u,r_u]\)全是\(t_u\),\(t_u=0\)就是没有懒标记 建立线段树 在建立时顺便处理\(l_u,r_u\),只要当\(l_u=r_u\)时就打上标记 P.s \(L

[SHOI2015]脑洞治疗仪(线段树?珂朵莉树)

题面 这道题超级可爱呢,珂朵莉最可爱了,不,小哀才是最可爱的呢 很好的题,可以考虑用线段树维护,hale表示线段树思路很难,而且难打,不如滚去写珂朵莉树哦 对于操作一:直接将set修改插入即可 对于操作三:最大连续子段和(线段树里面是这样叫的吧)维护即可 对于操作二:我们发现可以考虑先将这段区间里面的1 全部取出来,然后暴力合并区间为0,插入会set里面 之后枚举要修改的区间,从左端点开始搞起,一直后搜索,最后加一个判断,是否已经完全ok即可,具体可参见代码 好了,这道题就解决了 我的代码好像l

P2787 语文1(chin1)- 理理思维(珂朵莉树)

珂朵莉树模板,区间排序就暴力地取二十六个字母出来并且计数,然后重新从小到大插入即可 代码: #include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld"

模板—珂朵莉树

其实本质上是优化暴力. 网上都说构造的数据可以卡掉珂朵莉树,是因为在修改的时候要遍历set导致很容易卡掉,所以珂朵莉树可能比较有局限性. 但是如果用来维护区间用于求交求并,复杂度是严格的log的,常数好像稍大,但是还是非常有用的. 放个板子: 1 #include<iostream> 2 #include<cstdio> 3 #include<set> 4 #define re register 5 #define co const 6 #define cor co r

好Van的珂朵莉树

珂朵莉树 珂朵莉树的主要操作是区间覆盖,即将区间\([l,r]\)全部染色为\(c\). EXAMPLE EXAMPLE 1 给出一个长度为\(n\)的序列,一共\(q\)次询问,每次询问给出\(m\)个区间,求这些区间并集的权值和. \(n \leq 10^5,\sum m \leq 10^5\) SOLUTION 1 显然能用珂朵莉树做 珂朵莉树是一种基于std::set的暴力数据结构. 这个set维护若干区间,这些区间没有交集,且按左端点从小到大有序. struct node { int

LG4979 矿洞:坍塌 珂朵莉树

问题描述 LG4979 题解 珂朵莉树+O2简直就是绝配 对于操作 A ,直接 \(\mathrm{assign}\) 推平就完事了. 对于操作 B ,如果它左右端点有在边界上的,直接把区间 \([l,r]\)撕出来判断就完了,如果不在边界上,先把单点 \({l-1,r+1}\) 撕出来判,如果符合条件,再撕 \([l,r]\) 出来判. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #define IT set&

cf896C. Willem, Chtholly and Seniorious(ODT)

题意 题目链接 Sol ODT板子题.就是用set维护连续段的大暴力.. 然鹅我抄的板子本题RE提交AC??.. 具体来说,用50 50 658073485 946088556这个数据测试下面的代码,然后在79行停住,看一下bg和i的值会发生神奇的事情.. 问题已解决,确实是那位博主写错了, 只要把split(l)和split(r + 1)反过来就行了 #include<bits/stdc++.h> #define LL long long #define int long long #def

ODT (Old Driver Tree)珂朵莉树

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int MOD7 = 1e9 + 7; 6 const int MOD9 = 1e9 + 9; 7 const int imax_n = 1e5 + 7; 8 LL add(LL a, LL b, LL mod) 9 { 10 LL res = 0; 11 LL ans = a; 12 while (b) 13 { 14 if