QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#1050 | #669762 | #7630. Yet Another Maximize Permutation Subarrays | NailongGroup | MiniLong | Failed. | 2024-10-23 19:40:23 | 2024-10-23 22:51:07 |
Details
Extra Test:
Accepted
time: 340ms
memory: 51172kb
input:
10 500000 33507 86288 341526 176548 269024 139744 436643 172920 11974 403605 391212 485226 337604 233207 129223 455269 405906 98404 1236 204343 142677 245017 390264 499363 250530 268048 131943 245767 284032 359344 179256 288749 116723 186609 44849 208707 73604 375369 203245 512 276437 455136 420340 ...
output:
1 387005 1 355277 1 161646 1 422079 1 463930 1 266365 1 11048 1 488306 1 1549 1 49138
result:
ok 10 cases
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#669762 | #7630. Yet Another Maximize Permutation Subarrays | MiniLong | AC ✓ | 1467ms | 128184kb | C++20 | 7.3kb | 2024-10-23 19:35:23 | 2024-10-23 19:35:23 |
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e6 + 5;
int n, a[N], p[N], L[N], R[N], f[N];
bool vis[N];
struct dsu{
int fa[N], maxn[N];
void init(){_rep(i, 1, n) fa[i] = maxn[i] = i;}
int find(int x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
int merge(int x, int y){
int fx = find(x), fy = find(y);
if(fx < fy) swap(fx, fy);
if(fx != fy) return fa[fx] = fy, maxn[fy] = maxn[fx], 1;
return 0;
}
void clr(){_rep(i, 1, n) fa[i] = maxn[i] = 0;}
}D;
namespace bit{
int c[N];
void add(int x){for(; x <= n; x += x & -x) c[x]++;}
int ask(int k){
int pos = 0;
_req(i, __lg(n), 0) if(pos + (1 << i) <= n && c[pos + (1 << i)] < k){
pos += (1 << i), k -= c[pos];
}
return pos + 1;
}
void clr(){_rep(i, 1, n) c[i] = 0;}
}
namespace sgt{
int tr[N << 2], tag[N << 2], pos[N << 2];
#define ls x << 1
#define rs x << 1 | 1
void pushtag(int x, int val){tr[x] += val, tag[x] += val;}
void pushdown(int x){
if(!tag[x]) return;
pushtag(ls, tag[x]), pushtag(rs, tag[x]);
tag[x] = 0;
}
void build(int x, int l, int r){
pos[x] = l;
if(l == r) return;
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
void modify(int x, int l, int r, int L, int R, int val){
if(l >= L && r <= R) return pushtag(x, val);
pushdown(x);
int mid = l + r >> 1;
if(L <= mid) modify(ls, l, mid, L, R, val);
if(R > mid) modify(rs, mid + 1, r, L, R, val);
tr[x] = max(tr[ls], tr[rs]);
pos[x] = tr[x] == tr[ls] ? pos[ls] : pos[rs];
}
void clr(int x, int l, int r){
tr[x] = tag[x] = pos[x] = 0;
if(l == r) return;
int mid = l + r >> 1;
clr(ls, l, mid), clr(rs, mid + 1, r);
}
}
struct node{
int l, r, v;
};
vector<node> h[N];
void ins(int l, int r, int L, int R, int val){
debug("i:[%d,%d] j:[%d,%d] val:%d\n", l, r, L, R, val);
h[l].pb({L, R, val}), h[r + 1].pb({L, R, -val});
}
void clr(){
_rep(i, 0, n + 1) L[i] = R[i] = p[i] = a[i] = vis[i] = 0, h[i].clear();
sgt::clr(1, 1, n), bit::clr(), D.clr();
}
typedef unsigned long long ull;
void check(int x, int y){
swap(a[x], a[y]);
int ans = 0;
_rep(i, 1, n){
int maxn = 0;
_rep(j, i, n) maxn = max(maxn, a[j]), ans += (maxn == j - i + 1);
}
debug("check (%d,%d) ans:%d\n", x, y, ans);
swap(a[x], a[y]);
}
int main(){
multitest(){
read(n), D.init();
_rep(i, 1, n) read(a[i]), p[a[i]] = i;
L[1] = R[1] = p[1];
int ans = 0, tot = 0, cur = 0;
_rep(i, 1, n){
vis[p[i]] = 1, bit::add(p[i]);
if(i > 1) L[i] = L[i - 1], R[i] = R[i - 1];
L[i] = min(L[i], p[i]), R[i] = max(R[i], p[i]);
cur++;
if(vis[p[i] - 1]) cur -= D.merge(p[i], p[i] - 1);
if(vis[p[i] + 1]) cur -= D.merge(p[i], p[i] + 1);
debug("i:%d\n", i);
if(R[i] - L[i] + 1 == i){
tot++;
if(i > 1){
if(L[i] > 2) ins(1, L[i] - 2, L[i], R[i], -1);
if(L[i] > 1) ins(L[i] - 1, L[i] - 1, L[i], R[i] - 1, -1);
if(R[i] + 1 < n) ins(L[i], L[i], R[i] + 2, n, -1);
if(R[i] < n) ins(L[i] + 1, R[i], R[i] + 1, n, -1);
}
}else{
if(cur > 3) continue;
if(cur == 2){
int a = L[i], b = D.maxn[a], c = D.find(R[i]), d = R[i];
if(a == b){
ins(a, a, c - 1, c - 1, 1);
if(d < n) ins(a, a, d + 1, d + 1, 1);
}else if(b + 2 == c){
ins(a, a, c - 1, c - 1, 1);
}
if(c == d){
ins(b + 1, b + 1, c, c, 1);
if(a > 1) ins(a - 1, a - 1, c, c, 1);
}else if(b + 2 == c){
ins(b + 1, b + 1, d, d, 1);
}
}
if(cur == 3){
int a = L[i], b = D.maxn[a], c = 0, d = 0, e = D.find(R[i]), f = R[i];
d = bit::ask(i - (f - e + 1)), c = D.find(d);
if(a == b && d + 2 == e) ins(a, a, d + 1, d + 1, 1);
if(e == f && b + 2 == c) ins(b + 1, b + 1, e, e, 1);
}
}
}
ans = tot; PII res = {1, 1};
sgt::build(1, 1, n);
_rep(i, 1, n){
for(auto &j : h[i]){
// debug("modify [%d,%d] %d", j.l, j.r, j.v);
sgt::modify(1, 1, n, j.l, j.r, j.v);
}
if(tot + sgt::tr[1] > ans){
// debug("???\n");
ans = tot + sgt::tr[1];
res = {i, sgt::pos[1]};
}
ans = max(ans, tot + sgt::tr[1]);
}
write(res.fi, res.se), puts("");
debug("ans:%d\n", ans); check(9, 9);
clr();
}
return 0;
}