답안 #1114523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114523 2024-11-19T07:00:05 Z epicci23 Sličnost (COI23_slicnost) C++17
100 / 100
767 ms 48612 KB
#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;

const int N = 1e5 + 5;

struct LazySeg{
  vector<array<int,2>> seg;
  vector<int> Lazy;
 
  LazySeg(int n){
  	seg.resize(4*n+5);
  	Lazy.assign(4*n+5,0);
  	build(1,1,n);
  }

  inline array<int,2> merge(array<int,2> a,array<int,2> b){
    array<int,2> res;
    res[0]=max(a[0],b[0]);
    res[1]=0;
    if(res[0]==a[0]) res[1]+=a[1];
    if(res[0]==b[0]) res[1]+=b[1];
    return res;
  }

  inline void push(int rt, int l, int r){
  	if(Lazy[rt] == 0) return;
    int u = Lazy[rt];
    Lazy[rt] = 0;
    seg[rt][0] += u;
    if(l==r) return;
    Lazy[rt*2] += u;
    Lazy[rt*2+1] += u;
  }

  void build(int rt,int l,int r){
  	if(l==r){
  	  seg[rt]={0,1};
  	  return;
  	}
  	int m=(l+r)/2;
  	build(rt*2,l,m),build(rt*2+1,m+1,r);
  	seg[rt] = merge(seg[rt*2],seg[rt*2+1]);
  }

  void upd(int rt,int l,int r,int gl,int gr,int u){
  	push(rt,l,r);
  	if(r<gl || l>gr) return;
  	if(l>=gl && r<=gr){
  	  Lazy[rt] += u;
  	  push(rt,l,r);
  	  return;
  	}
  	int m = (l+r)/2;
  	upd(rt*2,l,m,gl,gr,u),upd(rt*2+1,m+1,r,gl,gr,u);
  	seg[rt] = merge(seg[rt*2],seg[rt*2+1]);
  }
};


struct Answer{
  vector<array<int,2>> seg,Lazy;

  Answer(int n){
  	seg.assign(4*n+5,{-1,0});
  	Lazy.assign(4*n+5,{-1,0});
  }

  inline array<int,2> merge(array<int,2> a,array<int,2> b){
    array<int,2> res;
    res[0]=max(a[0],b[0]);
    res[1]=0;
    if(res[0]==a[0]) res[1]+=a[1];
    if(res[0]==b[0]) res[1]+=b[1];
    return res;
  }
  
  inline void push(int rt, int l, int r){
    if(Lazy[rt][0] == -1) return;
    array<int,2> U = Lazy[rt];
    Lazy[rt] = {-1, 0};
    seg[rt] = merge(seg[rt],U);
    if(l==r) return;
    Lazy[rt*2] = merge(Lazy[rt*2],U);
    Lazy[rt*2+1] = merge(Lazy[rt*2+1],U);
  }

  void upd(int rt,int l,int r,int gl,int gr,array<int,2> u){
  	push(rt,l,r);
  	if(r<gl || l>gr) return;
  	if(l>=gl && r<=gr){
  	  Lazy[rt] = merge(Lazy[rt], u);
  	  push(rt,l,r);
  	  return;
  	}
    int m = (l + r) / 2;
    upd(rt*2,l,m,gl,gr,u),upd(rt*2+1,m+1,r,gl,gr,u);
  }
  
  array<int,2> get(int rt,int l,int r,int ind){
  	push(rt,l,r);
  	if(l==r) return seg[rt];
  	int m = (l+r)/2;
  	if(ind<=m) return get(rt*2,l,m,ind);
  	else return get(rt*2+1,m+1,r,ind);
  }
};


void _(){
  int n,k,q;
  cin >> n >> k >> q;
  int a[n+5],b[n+5],c[n+5];
  for(int i=1;i<=n;i++){
  	cin >> a[i];
  	c[i] = a[i];
  }
  for(int i=1;i<=n;i++) cin >> b[i];
  
  int ind[n+5];
  for(int i=1;i<=n;i++) ind[b[i]]=i;
  
  LazySeg T(n);
  Answer Ans(q+5);
  vector<int> delta[n+5];
  
  vector<array<int,2>> Versions[n+5];
  for(int i=1;i<=n;i++) Versions[i].push_back({0,a[i]});
 
  for(int i=1;i<=q;i++){
    int ind; cin >> ind;
    delta[ind].push_back(i);
    swap(c[ind],c[ind+1]);
    Versions[ind].push_back({i,c[ind]});
    Versions[ind+1].push_back({i,c[ind+1]});
  }

  auto Get_Val = [&](int ind,int ti){
    int it = lower_bound(all(Versions[ind]),array<int,2>{ti+1,-1}) - Versions[ind].begin();
    return Versions[ind][it-1][1];
  };
  
  for(int i=1;i<=n;i++){
    int p = ind[a[i]]; // open element
    T.upd(1,1,n,max(1LL,p-k+1),min(n-k+1,p),1);
    if(i<k) continue;

    if(i > k){ // close element
      p = ind[a[i-k]];
      T.upd(1,1,n,max(1LL,p-k+1),min(n-k+1,p),-1);
    }

    vector<array<int,2>> v;
  	for(int x:delta[i]) v.push_back({x,i});
  	if(i>k) for(int x:delta[i-k]) v.push_back({x,i-k+1});
    sort(all(v));
    v.erase(unique(all(v)),v.end());
    int Time = 0;
    vector<array<int,2>> roll;
   
    // now time to do some updates and update answer array
    for(auto x:v){
      Ans.upd(1,0,q,Time,x[0]-1,T.seg[1]);
      Time = x[0];
      // close old value
      p = ind[Get_Val(x[1],x[0]-1)];
      T.upd(1,1,n,max(1LL,p-k+1),min(n-k+1,p),-1);
      roll.push_back({p,-1});
      // open new value
      p = ind[Get_Val(x[1],x[0])];
      T.upd(1,1,n,max(1LL,p-k+1),min(n-k+1,p),1);
      roll.push_back({p,1});
    }

    Ans.upd(1,0,q,Time,q,T.seg[1]);
    // rollback them
    while(!roll.empty()){
      auto xd = roll.back();
      roll.pop_back();
      p = xd[0];
      T.upd(1,1,n,max(1LL,p-k+1),min(n-k+1,p),-xd[1]);
    }
  }

  for(int i=0;i<=q;i++){
   auto U = Ans.get(1,0,q,i);
   cout << U[0] << ' ' << U[1] << '\n';
  }
}
int32_t main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 4 ms 1360 KB Output is correct
17 Correct 4 ms 1384 KB Output is correct
18 Correct 3 ms 1360 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 6 ms 1376 KB Output is correct
21 Correct 4 ms 1360 KB Output is correct
22 Correct 2 ms 1360 KB Output is correct
23 Correct 6 ms 1616 KB Output is correct
24 Correct 5 ms 1492 KB Output is correct
25 Correct 5 ms 1360 KB Output is correct
26 Correct 5 ms 1360 KB Output is correct
27 Correct 6 ms 1360 KB Output is correct
28 Correct 4 ms 1360 KB Output is correct
29 Correct 4 ms 1360 KB Output is correct
30 Correct 4 ms 1360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 4 ms 1360 KB Output is correct
17 Correct 4 ms 1384 KB Output is correct
18 Correct 3 ms 1360 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 6 ms 1376 KB Output is correct
21 Correct 4 ms 1360 KB Output is correct
22 Correct 2 ms 1360 KB Output is correct
23 Correct 6 ms 1616 KB Output is correct
24 Correct 5 ms 1492 KB Output is correct
25 Correct 5 ms 1360 KB Output is correct
26 Correct 5 ms 1360 KB Output is correct
27 Correct 6 ms 1360 KB Output is correct
28 Correct 4 ms 1360 KB Output is correct
29 Correct 4 ms 1360 KB Output is correct
30 Correct 4 ms 1360 KB Output is correct
31 Correct 75 ms 21332 KB Output is correct
32 Correct 78 ms 21256 KB Output is correct
33 Correct 46 ms 21320 KB Output is correct
34 Correct 86 ms 21320 KB Output is correct
35 Correct 127 ms 21836 KB Output is correct
36 Correct 96 ms 21072 KB Output is correct
37 Correct 39 ms 21832 KB Output is correct
38 Correct 120 ms 20816 KB Output is correct
39 Correct 83 ms 21584 KB Output is correct
40 Correct 98 ms 20808 KB Output is correct
41 Correct 120 ms 21584 KB Output is correct
42 Correct 143 ms 21072 KB Output is correct
43 Correct 88 ms 20984 KB Output is correct
44 Correct 71 ms 21576 KB Output is correct
45 Correct 50 ms 21416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 416 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 4 ms 1360 KB Output is correct
17 Correct 4 ms 1384 KB Output is correct
18 Correct 3 ms 1360 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 6 ms 1376 KB Output is correct
21 Correct 4 ms 1360 KB Output is correct
22 Correct 2 ms 1360 KB Output is correct
23 Correct 6 ms 1616 KB Output is correct
24 Correct 5 ms 1492 KB Output is correct
25 Correct 5 ms 1360 KB Output is correct
26 Correct 5 ms 1360 KB Output is correct
27 Correct 6 ms 1360 KB Output is correct
28 Correct 4 ms 1360 KB Output is correct
29 Correct 4 ms 1360 KB Output is correct
30 Correct 4 ms 1360 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 416 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct
46 Correct 15 ms 2384 KB Output is correct
47 Correct 16 ms 2516 KB Output is correct
48 Correct 5 ms 2384 KB Output is correct
49 Correct 14 ms 2384 KB Output is correct
50 Correct 24 ms 2536 KB Output is correct
51 Correct 8 ms 2556 KB Output is correct
52 Correct 15 ms 2552 KB Output is correct
53 Correct 23 ms 2384 KB Output is correct
54 Correct 20 ms 2384 KB Output is correct
55 Correct 10 ms 2652 KB Output is correct
56 Correct 25 ms 2384 KB Output is correct
57 Correct 26 ms 2384 KB Output is correct
58 Correct 9 ms 2384 KB Output is correct
59 Correct 21 ms 2384 KB Output is correct
60 Correct 8 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 4 ms 1360 KB Output is correct
17 Correct 4 ms 1384 KB Output is correct
18 Correct 3 ms 1360 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 6 ms 1376 KB Output is correct
21 Correct 4 ms 1360 KB Output is correct
22 Correct 2 ms 1360 KB Output is correct
23 Correct 6 ms 1616 KB Output is correct
24 Correct 5 ms 1492 KB Output is correct
25 Correct 5 ms 1360 KB Output is correct
26 Correct 5 ms 1360 KB Output is correct
27 Correct 6 ms 1360 KB Output is correct
28 Correct 4 ms 1360 KB Output is correct
29 Correct 4 ms 1360 KB Output is correct
30 Correct 4 ms 1360 KB Output is correct
31 Correct 75 ms 21332 KB Output is correct
32 Correct 78 ms 21256 KB Output is correct
33 Correct 46 ms 21320 KB Output is correct
34 Correct 86 ms 21320 KB Output is correct
35 Correct 127 ms 21836 KB Output is correct
36 Correct 96 ms 21072 KB Output is correct
37 Correct 39 ms 21832 KB Output is correct
38 Correct 120 ms 20816 KB Output is correct
39 Correct 83 ms 21584 KB Output is correct
40 Correct 98 ms 20808 KB Output is correct
41 Correct 120 ms 21584 KB Output is correct
42 Correct 143 ms 21072 KB Output is correct
43 Correct 88 ms 20984 KB Output is correct
44 Correct 71 ms 21576 KB Output is correct
45 Correct 50 ms 21416 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 336 KB Output is correct
49 Correct 1 ms 336 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 1 ms 336 KB Output is correct
52 Correct 1 ms 336 KB Output is correct
53 Correct 1 ms 336 KB Output is correct
54 Correct 1 ms 336 KB Output is correct
55 Correct 1 ms 416 KB Output is correct
56 Correct 1 ms 336 KB Output is correct
57 Correct 1 ms 336 KB Output is correct
58 Correct 1 ms 336 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 336 KB Output is correct
61 Correct 15 ms 2384 KB Output is correct
62 Correct 16 ms 2516 KB Output is correct
63 Correct 5 ms 2384 KB Output is correct
64 Correct 14 ms 2384 KB Output is correct
65 Correct 24 ms 2536 KB Output is correct
66 Correct 8 ms 2556 KB Output is correct
67 Correct 15 ms 2552 KB Output is correct
68 Correct 23 ms 2384 KB Output is correct
69 Correct 20 ms 2384 KB Output is correct
70 Correct 10 ms 2652 KB Output is correct
71 Correct 25 ms 2384 KB Output is correct
72 Correct 26 ms 2384 KB Output is correct
73 Correct 9 ms 2384 KB Output is correct
74 Correct 21 ms 2384 KB Output is correct
75 Correct 8 ms 2552 KB Output is correct
76 Correct 428 ms 42056 KB Output is correct
77 Correct 465 ms 42056 KB Output is correct
78 Correct 97 ms 41544 KB Output is correct
79 Correct 377 ms 41812 KB Output is correct
80 Correct 648 ms 42736 KB Output is correct
81 Correct 147 ms 41544 KB Output is correct
82 Correct 411 ms 43336 KB Output is correct
83 Correct 640 ms 41076 KB Output is correct
84 Correct 570 ms 42220 KB Output is correct
85 Correct 233 ms 48612 KB Output is correct
86 Correct 767 ms 41920 KB Output is correct
87 Correct 634 ms 41032 KB Output is correct
88 Correct 194 ms 41544 KB Output is correct
89 Correct 533 ms 42304 KB Output is correct
90 Correct 188 ms 42312 KB Output is correct