当前位置:网站首页>St Table + two points
St Table + two points
2022-06-24 21:58:00 【Weng Weiqiang】
subject :https://codeforces.ml/contest/1549/problem/D
ST surface :
1. Online modification is not supported Preprocessing event complexity 0(nlogn), Query events o(1)
2. For example, find the maximum value of a certain section st[i][j] From i To i+2^j
Then its maximum value is determined by the first half and the second half
The first half : by i+2^(j-1), The second half is (i+2^(j-1)+1)+2^(j-1)
that st[i][j]=max(st[i][j-1],st[i][i+2^(j-1)+1][j-1])
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)<=n+1;i++)
{
st[i][j]=max[st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}Topic analysis :
Carelessness : Find the longest subsequence in an interval , The values of this sequence are congruent m,m>=2
analysis :
Congruence property :
If x ≡ y ( m o d p )
that ( x − y ) %p==0
So construct a difference array , Then, if the difference fraction group satisfying the interval is not coprime
AC Code :
#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[212100];
ll st[212100][30];
ll lg2[212100];// representative k The scope of the
ll n;
// Verify whether it is mutual prime
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll query(ll l, ll r) { return gcd(st[l][lg2[r - l + 1]], st[r - (1 << lg2[r - l + 1]) + 1][lg2[r - l + 1]]); }
ll bsearch(ll l, ll r, ll p)
{
ll mid;
ll k = 1;
while (l < r)// Find the optimal interval Meet the requirements of mutual prime
{
mid = l + r + 1 >> 1;
if (abs(query(p, mid)) < 2)// Be careful abs Where to put it
{
r = mid - 1;// Try to make the interval smaller
}
else
{
l = mid;
}
}
if (abs(query(p, l)) < 2) { return 1; }
return l;
}
int main()
{
int T;
cin >> T;
lg2[0] = -1;
for (int i = 2; i <= 210000; i++) { lg2[i] = lg2[i>>1] + 1; } // After a k The value is always greater than the previous one
while (T--)
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
// Initialize the rest
for (int i = n + 1; i <= 2 * n && i <= 200000; i++) { st[i][0] = 0; }
for (int i = 1; i <= n; i++)
{
st[i][0] = a[i] - a[i - 1];// Build a differential array
}
for (int i = 1; (1 << i) <= n; i++)//i It's state
{
for (int j = 1; j + (1 << i) - 1 <= n; j++)//j Is the position
{
st[j][i] = gcd(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
}
}
ll p = 1;
for (ll i = 1; i <= n; i++)
{
ll u = bsearch(i + 1, n, i + 1);// Third i+1 Save this location
if (u == 1)continue;// Mutual prime continues to extend
p = max(p, u - i + 1);
}
cout << p << endl;
}
}
边栏推荐
- 堆排序和快速排序原理实现
- That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
- socket done
- How to achieve energy conservation and environmental protection of full-color outdoor LED display
- 【吴恩达笔记】多变量线性回归
- 基于kruskal的最小生成树
- [notes of Wu Enda] multivariable linear regression
- 福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
- Visit Amazon memorydb and build your own redis memory database
- 栈的两种实现方式
猜你喜欢

力扣每日一题-第26天-496.下一个更大元素Ⅰ

Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme

架构实战营 第 6 期 毕业设计

降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)
![在每个树行中找最大值[分层遍历之一的扩展]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
在每个树行中找最大值[分层遍历之一的扩展]

LeetCode-513. 找树左下角的值

二叉搜索树模板

leetcode-201_ 2021_ 10_ seventeen

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作

网络层 && IP
随机推荐
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
Collapse code using region
VSCode无网环境快速迁移开发环境(VIP典藏版)
Graduation design of phase 6 of the construction practice camp
Network layer & IP
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
力扣每日一题-第26天-496.下一个更大元素Ⅰ
双链表实现
leetcode_ 191_ 2021-10-15
leetcode-201_ 2021_ 10_ seventeen
[notes of Wu Enda] convolutional neural network
最大流问题
Blender FAQs
Summary of papers on traveling salesman problem (TSP)
Transport layer UDP & TCP
[untitled]
Mysql 通过表明获取字段以及注释
Excel layout