QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#288027 | #7969. 套娃 | PYD1 | AC ✓ | 39ms | 17856kb | C++14 | 5.1kb | 2023-12-21 16:16:49 | 2023-12-21 16:16:51 |
Judging History
answer
#include <set>
#include <map>
#include <list>
#include <queue>
#include <cmath>
#include <time.h>
#include <random>
#include <bitset>
#include <vector>
#include <cstdio>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mk make_pair
#define fi first
#define se second
inline int read(){
int t = 0,f = 1;
register char c = getchar();
while (c < 48 || c > 57) f = (c == '-') ? (-1) : (f),c = getchar();
while (c >= 48 && c <= 57) t = (t << 1) + (t << 3) + (c ^ 48),c = getchar();
return f * t;
}
const int N = 1e5 + 10,INF = 0x3f3f3f3f;
int n,v[N],ans[N];
struct Node{
int l,r;
bool operator < (const Node &rhs) const {return this->l != rhs.l ? this->l < rhs.l : this->r > rhs.r;}
}ns[N],tmp[N];
vector <int> vec[N];
struct DSU{
int fa[N];
void init(int n) {for (int i = 1;i <= n;i++) fa[i] = i;}
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void merge(int x,int y){
int a = find(x),b = find(y);
if (a != b) fa[a] = b;
}
}dsu;
struct Segment_tree{
int tag[N << 2],pmn[N << 2],resmn[N << 2];
void pushup(int i,int l,int r){
pmn[i] = min(pmn[i << 1],pmn[i << 1 | 1]);
resmn[i] = min(resmn[i << 1],resmn[i << 1 | 1]);
}
void spread(int i,int l,int r,int v) {tag[i] = pmn[i] = v,resmn[i] = l - v + 1;}
void pushdown(int i,int l,int r){
if (!tag[i]) return ;
int mid = (l + r) >> 1;
spread(i << 1,l,mid,tag[i]),spread(i << 1 | 1,mid + 1,r,tag[i]);
tag[i] = 0;
}
void build(int i,int l,int r){
tag[i] = 0;
if (l == r) {pmn[i] = l,resmn[i] = 1;return ;}
int mid = (l + r) >> 1;
build(i << 1,l,mid),build(i << 1 | 1,mid + 1,r);
pushup(i,l,r);
}
void modify(int i,int l,int r,int ql,int qr,int v){
if (l >= ql && r <= qr) {spread(i,l,r,v);return ;}
int mid = (l + r) >> 1;pushdown(i,l,r);
if (ql <= mid) modify(i << 1,l,mid,ql,qr,v);
if (qr > mid) modify(i << 1 | 1,mid + 1,r,ql,qr,v);
pushup(i,l,r);
}
int query(int i,int l,int r,int ql,int qr){
if (l >= ql && r <= qr) return resmn[i];
int mid = (l + r) >> 1,ans = INF;pushdown(i,l,r);
if (ql <= mid) ans = min(ans,query(i << 1,l,mid,ql,qr));
if (qr > mid) ans = min(ans,query(i << 1 | 1,mid + 1,r,ql,qr));
return ans;
}
int bs(int i,int l,int r,int ql,int qr,int v){
if (r < ql || l > qr) return INF;
if (l == r) return pmn[i] > v ? l : INF;
int mid = (l + r) >> 1;pushdown(i,l,r);
if (qr > mid){
if (pmn[i << 1 | 1] > v) return min(mid + 1,bs(i << 1,l,mid,ql,qr,v));
else return bs(i << 1 | 1,mid + 1,r,ql,qr,v);
}
return bs(i << 1,l,mid,ql,qr,v);
}
}TR;
int change(int p,int v){
if (ans[p] != -1) return dsu.find(p);
ans[p] = v,dsu.merge(p,p + 1);
return dsu.find(p);
}
void upd(int x,int m){
// printf("upd(%d)\n",x);
if (!m){
int cur = 1;
while (cur <= n) cur = change(cur,x);
return ;
}
sort(ns + 1,ns + m + 1);
int tmpcnt = 0,ncnt = 0;Node cur = (Node){-1,-1};
for (int i = 1;i <= m;i++){
if (cur.l == -1) cur = ns[i];
else if (ns[i].l > cur.r) tmp[++tmpcnt] = cur,cur = ns[i];
else cur.l = min(cur.l,ns[i].l),cur.r = max(cur.r,ns[i].r);
}
tmp[++tmpcnt] = cur;
sort(tmp + 1,tmp + tmpcnt + 1);
// puts("tmp:");
// for (int i = 1;i <= tmpcnt;i++) printf("(%d,%d)\n",tmp[i].l,tmp[i].r);
int lst = 0;
for (int i = 1;i <= tmpcnt;i++){
if (lst + 1 < tmp[i].l) ns[++ncnt] = (Node){lst + 1,tmp[i].l - 1};
lst = tmp[i].r;
}
if (lst + 1 <= n) ns[++ncnt] = (Node){lst + 1,n};
// puts("ns:");
// for (int i = 1;i <= ncnt;i++) printf("(%d,%d)\n",ns[i].l,ns[i].r);
for (int i = 1;i <= ncnt;i++){
int cur = ns[i].l;
while (cur <= ns[i].r) cur = change(cur,x);
}
}
void solve(int x){
// printf("solve(%d)\n",x);
int lst = 0,ncnt = 0;
for (int p : vec[x]){
if (lst + 1 <= p - 1){
int tl = TR.bs(1,1,n,lst + 1,p - 1,lst);
int l = tl > p - 1 ? INF : TR.query(1,1,n,tl,p - 1),r = p - lst - 1;
if (l <= r) ns[++ncnt] = (Node){l,r};
}
lst = p;
}
if (lst + 1 <= n){
int tl = TR.bs(1,1,n,lst + 1,n,lst);
int l = tl > n ? INF : TR.query(1,1,n,tl,n),r = n - lst;
if (l <= r) ns[++ncnt] = (Node){l,r};
}
// for (int i = 1;i <= ncnt;i++) printf("(%d,%d)\n",ns[i].l,ns[i].r);
upd(x,ncnt);
lst = 0;int lstv = -INF;
for (int p : vec[x]){
if (lst + 1 <= p - 1){
int l = TR.bs(1,1,n,lst + 1,p - 1,lstv),r = p - 1;
if (l <= r) TR.modify(1,1,n,l,r,lstv);
}
lst = lstv = p;
}
if (lst + 1 <= n){
int l = TR.bs(1,1,n,lst + 1,n,lstv),r = n;
if (l <= r) TR.modify(1,1,n,l,r,lstv);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
#endif
n = read();
for (int i = 1;i <= n;i++) vec[v[i] = read()].emplace_back(i);
TR.build(1,1,n),dsu.init(n + 1),memset(ans,-1,sizeof(ans));
for (int i = 0;i <= n;i++){
solve(i);
if (vec[i].empty()){
int cur = 1;
while (cur <= n) cur = change(cur,i + 1);
}
}
for (int i = 1;i <= n;i++) printf("%d ",ans[i]);puts("");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 12368kb
input:
6 0 0 0 1 2 3
output:
2 3 4 0 0 0
result:
ok single line: '2 3 4 0 0 0 '
Test #2:
score: 0
Accepted
time: 1ms
memory: 10356kb
input:
100 0 10 21 9 4 1 14 0 8 1 4 6 10 0 0 4 18 0 12 5 2 5 15 1 6 2 0 4 14 5 1 2 23 1 8 1 24 8 8 9 5 2 12 2 3 7 6 11 12 12 6 12 4 4 5 0 7 4 9 12 1 7 4 7 12 2 10 2 4 8 7 1 4 0 13 9 13 2 2 3 9 14 7 9 15 10 10 6 2 12 3 11 6 3 4 7 9 6 11 1
output:
2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok single line: '2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '
Test #3:
score: 0
Accepted
time: 28ms
memory: 14476kb
input:
100000 127 145 474 139 36 135 75 670 76 433 206 214 283 56 214 440 147 280 244 190 181 565 31 550 261 93 526 404 125 390 17 552 5 364 53 337 52 506 277 279 15 248 46 61 826 69 166 297 171 289 150 175 111 151 317 342 166 13 199 152 308 19 156 347 205 166 45 115 177 235 422 425 109 4 658 232 347 370 4...
output:
2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...
result:
ok single line: '2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '
Test #4:
score: 0
Accepted
time: 24ms
memory: 15532kb
input:
100000 360 34 204 156 183 404 21 3 20 122 170 193 347 144 51 464 94 265 190 88 284 437 538 392 661 397 839 208 83 191 42 16 194 515 374 53 617 502 307 504 348 175 219 63 2 130 289 223 135 440 284 189 104 142 87 117 316 218 301 14 87 405 293 489 763 197 678 196 173 96 257 17 190 525 243 161 220 178 8...
output:
2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...
result:
ok single line: '2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '
Test #5:
score: 0
Accepted
time: 32ms
memory: 15620kb
input:
100000 322 408 29 271 168 161 265 412 230 177 325 60 331 226 298 143 343 83 274 706 47 234 287 46 329 248 174 351 235 38 172 171 251 355 274 307 468 11 222 309 666 137 18 440 1209 7 103 354 496 336 183 602 240 316 442 253 32 486 308 18 115 125 110 65 268 502 148 793 91 759 313 269 31 63 250 90 143 6...
output:
2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4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 0 0 0 0 0 0 0 0 0 '
Test #7:
score: 0
Accepted
time: 39ms
memory: 17856kb
input:
100000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...
output:
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok single line: '2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 '
Test #8:
score: 0
Accepted
time: 18ms
memory: 15808kb
input:
100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...
result:
ok single line: '2 3 4 5 6 7 8 9 10 11 12 13 14... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '