当前位置:网站首页>St table, weighted and search set
St table, weighted and search set
2022-07-26 20:38:00 【xingxg.】
RMQ
Range Minimunm/Maximum Query

f[i][j] : With i As a starting point The length is
Maximum value of interval / minimum value
Finally, I use 2 Segment interval to piece together into an interval we want to query , There will be interval repetition , So use ST The precondition of the table is the requirement , The interval of repeated calculation will not affect the result , For example, please :
Maximum 、 minimum value 、 & operation 、| operation 、 gcd
and , The interval is preprocessed O(nlogn) , Inquire about O(1) , Therefore, the interval cannot be modified .
Using the segment tree, you can also operate on the interval , And it can maintain more complex information , But if you just make a query , Efficiency is not ST Table high .
// problem :
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int, int> PII;
#define pb push_back
const int N = 1e6 + 5;
typedef unsigned int ui;
unsigned int A, B, C, n, q, f[N][22], a[N];
inline unsigned int rng61() {
A ^= A << 16;
A ^= A >> 5;
A ^= A << 1;
unsigned int t = A;
A = B;
B = C;
C ^= t ^ A;
return C;
}
int main(){
scanf("%d%d%u%u%u", &n, &q, &A, &B, &C);
for (int i = 1; i <= n; i++) {
a[i] = rng61();
f[i][0] = a[i];
}
for (int j = 1; j <= 20; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
ui ans = 0;
for (int i = 1; i <= q; i++) {
unsigned int l = rng61() % n + 1, r = rng61() % n + 1;
if (l > r) swap(l, r);
// int x = log2(r - l + 1);
int x = __lg(r - l + 1);
ans ^= max(f[l][x], f[r - (1 <<x) + 1][x]);
}
printf("%u\n", ans);
}Take the right and check the collection

// problem :
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int, int> PII;
#define pb push_back
const int N = 2E5 + 5;
int f[N];
ll w[N];
int n, q;
int find(int x) {
if(f[x] == x) return x;
int p = f[x];
find(p); // q
/*
old : w[x] : a[x] - a[p]
f[p] = q w[p] : a[p] - a[q]
new f[x] = q w[x] : a[x] - a[q] The updated w[x]
w[x] = a[x] - a[q] = a[x] - a[p] + a[p] - a[q];
*/
w[x] = w[x] + w[p];
return f[x] = f[p];
}
int main(){
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; ++i) {
f[i] = i;
w[i] = 0;
}
ll t = 0;
for (int i = 1; i <= q; ++i) {
int ty, l, r;
scanf("%d %d %d", &ty, &l, &r);
l = (l + t) % n + 1;
r = (r + t) % n + 1;
if(ty == 2) {
if(find(l) != find(r)) continue;
printf("%lld\n", w[l] - w[r]);
t = abs(w[l] - w[r]);
} else {
int x; scanf("%d", &x);
if(find(l) == find(r)) continue;
w[f[l]] = x - w[l] + w[r];
f[f[l]] = f[r];
/*
w[f[l]] = a[f[l]] - a[f[r]]
a[l] - a[r] = x
w[l] = a[l] - a[f[l]], w[r] = a[r] - a[f[r]]
==> w[f[l]] = a[l] - w[l] - (a[r] - w[r])
*/
}
}
return 0;
}边栏推荐
- SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)
- Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
- 连接池快速入门
- 【刷题记录】22. 括号生成
- BGP的基本配置和聚合
- Single core A7 plays with face recognition, and NXP "crossover processor" plays a new trick!
- Small scenes bring great improvement! Baidu PaddlePaddle easydl helps AI upgrade of manufacturing assembly line
- 传惠普/戴尔/微软/亚马逊考虑将部分硬件生产线转到大陆以外
- 7岁男童因下棋太快?机器人竟夹断其手指
- 小场景带来大提升!百度飞桨EasyDL助力制造业流水线AI升级
猜你喜欢
随机推荐
如何组装一个注册中心?
韩国计划每年砸1万亿韩元,研发半导体材料及设备
Software testing - development test content specification (project test template)
一文读懂 Kubernetes的四种服务类型!
Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
BGP--边界网关协议
【刷题记录】22. 括号生成
Bean注入和生命周期
Read the four service types of kubernetes!
为什么 ThreadLocal 可以做到线程隔离?
Fitting the new direction of curriculum standards, ape guidance, creating a characteristic new concept content system
This points to the simplest rule remember it
QT信号与槽连接(松耦合)
【微信小程序】零基础学 | 小程序语法
【PyQt5基本控件使用解析】
7.25 simulation summary
让华为吃了败诉的CNEX Labs到底有何来头?
[基础服务] [数据库] ClickHouse的安装和配置
查询字段较多时可以添加普通查询和高级查询两种情况
NVIDIA Canvas 初体验~
Maximum value of interval / minimum value 








