4358: permu
2024-10-26 08:10:17
4358: permu
分析:
不删除的莫队+可撤销的并查集。
每次询问先固定左端点到一个块内,然后将这些右端点从小到大排序,然后询问的过程中,右端点不断往右走,左端点可能会撤销,但是移动区间不超过$\sqrt n$个,用带撤销的并查集维护。
复杂度$O(n \sqrt n log n)$
代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<bitset>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int bel[N], a[N], ans[N], fa[N], dep[N], B, Top, Mx, n;
bool vis[N];
struct Que{ int l, r, id; } ;
struct Node{ int x, d; } sk[N << ]; // N * 2 !!!
bool operator < (const Que &A,const Que &B) { return A.r < B.r; }
vector< Que > q[N]; int solve1(int x,int y) {
if (x == y) return ;
vector<int>vec;
for (int i = x; i <= y; ++i) vec.push_back(a[i]);
sort(vec.begin(), vec.end());
int res = , now = ;
for (int i = ; i < (int)vec.size(); ++i)
vec[i] == vec[i - ] + ? now ++ : now = , res = max(res, now); // !!!
return res;
}
int find(int x) {
return x == fa[x] ? x : find(fa[x]);
}
void Union(int x,int y) {
x = find(x), y = find(y);
if (x == y) return ;
if (dep[x] < dep[y]) swap(x, y);
Mx = max(Mx, dep[x] + dep[y]);
fa[y] = x;
sk[++Top] = (Node){x, dep[x]};
sk[++Top] = (Node){y, dep[y]};
dep[x] += dep[y];
}
void add(int x) {
vis[x] = ;
if (vis[x - ]) Union(x - , x);
if (vis[x + ]) Union(x, x + );
}
void solve(int now,vector<Que> &vec) {
Top = , Mx = ;
int pos = min(N, now * B) + , lastpos, lastmx, L = pos, R = pos - ;
for (int i = ; i <= n; ++i) fa[i] = i, dep[i] = , vis[i] = ;
for (int i = ; i < (int)vec.size(); ++i) {
Que v = vec[i];
while (R < v.r) add(a[++R]);
lastmx = Mx, lastpos = Top;
while (L > v.l) add(a[--L]);
ans[v.id] = Mx;
Mx= lastmx;
while (Top > lastpos) fa[sk[Top].x] = sk[Top].x, dep[sk[Top].x] = sk[Top].d, Top --;
while (L < pos) vis[a[L ++]] = ;
}
}
int main() {
n = read();int m = read(); B = sqrt(n);
for (int i = ; i <= n; ++i) a[i] = read(), bel[i] = (i - ) / B + ;
for (int i = ; i <= m; ++i) {
int x = read(), y = read();
if (bel[x] == bel[y]) ans[i] = solve1(x, y);
else q[bel[x]].push_back((Que){x, y, i});
}
for (int i = ; i <= bel[n]; ++i) sort(q[i].begin(), q[i].end()), solve(i, q[i]);
for (int i = ; i <= m; ++i) printf("%d\n", ans[i]);
return ;
}
最新文章
- C# new关键字
- php用正则检测某字段开头是否为字母
- Dapper学习笔记(4)-事务
- C#栈
- xmpp整理笔记:用户网络连接及好友的管理
- java集合 之 Map集合
- JavaScript中的Partial Application和Currying
- 通过枚举enum实现单例设计
- 01-UIKit
- 【ASP.NET Web API教程】3.4 HttpClient消息处理器
- mysql导出和导入命令更改数据库名称数据库
- CSS3 02. 边框、边框圆角、边框阴影、边框图片、渐变、线性渐变、径向渐变、背景、过渡transition、2D转换
- Consumer高级特性
- Liunx之xl2TP的一键搭建
- git忽略已提交的文件或目录
- HTML的常用总结
- EchoService
- MFC中不同对话框间使用SendMessage发送自定义消息的具体实现
- Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)
- C语言 &#183; 2的次幂表示 &#183; 幂方分解