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 (b&1) res = (res + ans) % mod;
 15         ans = (ans + ans) % mod;
 16         b>>=1;
 17     }
 18     return res;
 19 }
 20
 21 LL pow(LL a, LL b, LL mod)
 22 {
 23     LL res = 1;
 24     LL ans = a % mod;
 25     while (b)
 26     {
 27         if (b&1) res = res * ans % mod;
 28         ans = ans * ans % mod;
 29         b>>=1;
 30     }
 31     return res;
 32 }
 33
 34 struct node {
 35     int l,r;
 36     mutable LL v;
 37     node(int L, int R=-1, LL V=0):l(L), r(R), v(V){}
 38     bool operator<(const node& o) const {
 39         return l < o.l;
 40     }
 41 };
 42
 43 set<node> s;
 44
 45 set<node>::iterator split(int pos)
 46 {
 47     auto it = s.lower_bound(node(pos));
 48     if (it != s.end() && it->l == pos) return it;
 49     --it;
 50     if (pos > it->r) return s.end();
 51     int L = it->l, R = it->r;
 52     LL V = it->v;
 53     s.erase(it);
 54     s.insert(node(L, pos-1, V));
 55     return s.insert(node(pos, R, V)).first;
 56     // pair<iterator, bool> insert(const value_type& val);
 57 }
 58
 59 void add(int l, int r, LL val=1)
 60 {
 61     split(l);
 62     auto itr = split(r+1), itl = split(l);
 63     for (; itl != itr; ++itl) itl->v += val;
 64 }
 65
 66 void assign_val(int l, int r, LL val=0)
 67 {
 68     split(l);
 69     auto itr = split(r+1), itl = split(l);
 70     s.erase(itl, itr);
 71     s.insert(node(l, r, val));
 72 }
 73
 74 LL rank_(int l, int r, int k, bool reversed=0)
 75 {
 76     vector<pair<LL, int>> vp;
 77     if (reversed) k = r - l + 2 - k;
 78     split(l);
 79     auto itr = split(r+1), itl = split(l);
 80     vp.clear();
 81     for (; itl != itr; ++itl)
 82         vp.push_back({itl->v, itl->r - itl->l + 1});
 83     sort(vp.begin(), vp.end());
 84     for (auto i: vp)
 85     {
 86         k -= i.second;
 87         if (k <= 0) return i.first;
 88     }
 89     return -1LL;
 90 }
 91
 92 LL sum(int l, int r, int ex, int mod)
 93 {
 94     split(l);
 95     auto itr = split(r+1), itl = split(l);
 96     LL res = 0;
 97     for (; itl != itr; ++itl)
 98         res = (res + (LL)(itl->r - itl->l + 1) * pow(itl->v, LL(ex), LL(mod))) % mod;
 99     return res;
100 }
101
102 int n, m;
103 LL seed, vmax;
104
105 LL rnd()
106 {
107     LL ret = seed;
108     seed = (seed * 7 + 13) % MOD7;
109     return ret;
110 }
111
112 LL a[imax_n];
113
114 int main()
115 {
116     cin>>n>>m>>seed>>vmax;
117     for (int i=1;i<=n;++i)
118     {
119             a[i] = (rnd() % vmax) + 1;
120             s.insert(node(i,i,a[i]));
121     }
122     s.insert(node(n+1, n+1, 0));
123     int lines = 0;
124     for (int i =1; i <= m; ++i)
125     {
126         int op = int(rnd() % 4) + 1;
127         int l = int(rnd() % n) + 1;
128         int r = int(rnd() % n) + 1;
129         if (l > r)
130             swap(l,r);
131         int x, y;
132         // cout<<i<<"   op = "<<op<<"    ";
133         if (op == 3)
134             x = int(rnd() % (r-l+1)) + 1;
135         else
136             x = int(rnd() % vmax) +1;
137
138         if (op == 4)
139             y = int(rnd() % vmax) + 1;
140         if (op == 1)
141             add(l, r, LL(x));
142         else if (op == 2)
143             assign_val(l, r, LL(x));
144         else if (op == 3)
145         {
146             // cout<<"lines"<<++lines<<endl;
147             cout<<rank_(l, r, x)<<endl;
148         }
149         else
150         {
151             // cout<<"lines"<<++lines<<endl;
152             cout<<sum(l, r, x, y)<<endl;
153         }
154     }
155     return 0;
156 }

原文地址:https://www.cnblogs.com/LeeSongt/p/9303031.html

时间: 2024-08-01 23:45:58

ODT (Old Driver Tree)珂朵莉树的相关文章

[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

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){

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&

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

珂朵莉树

将一段区间的值全变成c 区间加 区间第k小的数 区间幂次和 传送门 模板 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <set> #include <algorithm> #define IT set<node>::iterator using namespace std; const int max

[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 我不会用珂朵莉树,但是