当前位置:网站首页>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;
}边栏推荐
- The first training course was a perfect success (๑ㅂ•) و*
- 7岁男童因下棋太快?机器人竟夹断其手指
- 传惠普/戴尔/微软/亚马逊考虑将部分硬件生产线转到大陆以外
- Leetcode-300 最长递增子序列
- 【微信小程序】零基础学 | 小程序语法
- Gbase learning - install gbase 8A MPP cluster v95
- Vs how to read data in MySQL (by the way, the problem of Chinese garbled code is solved through code)
- The 50 Smartest Companies in the world announced that Chinese manufacturers account for nearly half, and Huawei ranks first
- Single core A7 plays with face recognition, and NXP "crossover processor" plays a new trick!
- A super simple neural network code with 5 coordinates for one layer node training
猜你喜欢

BUU刷题记3

Software testing - development test content specification (project test template)

营销与销售文件管理以及工作流程解决方案
![[record of question brushing] 22. bracket generation](/img/0d/8881fcbcd0e963875dff2946b95865.png)
[record of question brushing] 22. bracket generation

Easycvr device management list page, paging data does not display problem repair

QT signal and slot connection (loose coupling)

Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业

Summary of message queue knowledge points

Quick start to connection pooling

MPLS 多协议标签交换技术
随机推荐
EasyCVR设备管理列表页面,分页数据不显示的问题修复
【微信小程序】零基础学 | 小程序语法
[基础服务] [数据库] ClickHouse的安装和配置
任务一 报告
Solve attributeerror: module 'win32com.gen_ py. 00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
连接池快速入门
[wechat applet] zero basics | applet syntax
App Uploader下载安装
猿辅导的科技硬实力:让AI从读懂孩子作业开始
Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
Auto.js 旋转图标
QT信号与槽连接(松耦合)
易基因|宏病毒组测序技术介绍
CentOS7关于Oracle RAC 11GR2部署磁盘分区问题
BUU刷题记2
数据块的存储系统中缓存的作用是什么?
【刷题记录】22. 括号生成
This points to the simplest rule remember it
BUU刷题记1
深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积
Maximum value of interval / minimum value