답안 #791562

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
791562 2023-07-24T07:28:04 Z 반딧불(#10046) Sličnost (COI23_slicnost) C++17
67 / 100
753 ms 524288 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Update{
    int t, l, r, v;
    Update(){}
    Update(int t, int l, int r, int v): t(t), l(l), r(r), v(v){}

    bool operator<(const Update &nxt)const{
        return t<nxt.t;
    }
};

struct PST{
    struct Node{
        Node *lc, *rc;
        int val, maxSum, maxCnt; /// �� ������ ���� +��, �� ���������� �ִ�

        Node(){
            lc = rc = nullptr;
            val = maxSum = 0, maxCnt = 1;
        }
        Node(Node *lc, Node *rc, int val, int maxSum, int maxCnt):
            lc(lc), rc(rc), val(val), maxSum(maxSum), maxCnt(maxCnt){}

        void init(int l, int r){
            if(l==r) return;
            int m = (l+r)>>1;
            lc = new Node();
            lc->init(l, m);
            rc = new Node();
            rc->init(m+1, r);
            maxCnt = lc->maxCnt + rc->maxCnt;
        }

        Node* update(int l, int r, int s, int e, int v){
            if(s<=l && r<=e){
                Node* tmp = new Node(lc, rc, val, maxSum, maxCnt);
                tmp->val += v;
                if(l==r) tmp->maxSum = tmp->val;
                else{
                    tmp->maxSum = tmp->val + max(tmp->lc->maxSum, tmp->rc->maxSum);
                    tmp->maxCnt = (tmp->lc->maxSum >= tmp->rc->maxSum ? tmp->lc->maxCnt : 0) +
                                  (tmp->rc->maxSum >= tmp->lc->maxSum ? tmp->rc->maxCnt : 0);
                }
                return tmp;
            }
            int m = (l+r)>>1;
            Node* tmp = new Node(lc, rc, val, maxSum, maxCnt);
            if(s<=m) tmp->lc = lc->update(l, m, s, e, v);
            if(m<e)  tmp->rc = rc->update(m+1, r, s, e, v);
            tmp->maxSum = tmp->val + max(tmp->lc->maxSum, tmp->rc->maxSum);
            tmp->maxCnt = (tmp->lc->maxSum >= tmp->rc->maxSum ? tmp->lc->maxCnt : 0) +
                          (tmp->rc->maxSum >= tmp->lc->maxSum ? tmp->rc->maxCnt : 0);
            return tmp;
        }
    } *root[200002];
    int L;
    map<int, ll> mp;

    inline void mpAdd(int x, ll v){
        mp[x] += v;
    }

    inline void mpErase(int x, ll v){
        mp[x] -= v;
        if(!mp[x]) mp.erase(x);
    }

    void init(int _L, vector<Update> &vec){
        L = _L;
        root[0] = new Node();
        root[0]->init(1, L);

        int pnt = 0;
        for(int t=1; t<=L*2; t++){
            root[t] = root[t-1];
            while(pnt < (int)vec.size() && vec[pnt].t == t){
                root[t] = root[t]->update(1, L, vec[pnt].l, vec[pnt].r, vec[pnt].v);
//                printf("Root %d: add %d %d %d\n", vec[pnt].t, vec[pnt].l, vec[pnt].r, vec[pnt].v);
                pnt++;
            }
            if(t%2) mpAdd(root[t]->maxSum, root[t]->maxCnt);
        }
    }

    void update(int t, Update p){
        if(t%2) mpErase(root[t]->maxSum, root[t]->maxCnt);
        root[t] = root[t]->update(1, L, p.l, p.r, p.v);
        if(t%2) mpAdd(root[t]->maxSum, root[t]->maxCnt);
    }

    void printAns(){
        printf("%d %lld\n", mp.rbegin()->first, mp.rbegin()->second);
    }
} tree;

int n, k, q;
int a[100002], idx[100002];
int in[100002], out[100002];

int main(){
    scanf("%d %d %d", &n, &k, &q);
    for(int i=1; i<=n; i++) scanf("%d", &a[i]);
    for(int i=1; i<=n; i++){
        int x;
        scanf("%d", &x);
        idx[x] = i;
    }

    /// �ʱ� ���� ���� �����
    vector<Update> updateVec;
    for(int i=1; i<=n; i++){
        in[i] = max(1, i-k+1);
        out[i] = min(i, n-k+1);
    }
    for(int i=1; i<=n; i++){
        updateVec.push_back(Update(in[i]*2-1, in[idx[a[i]]], out[idx[a[i]]], 1));
        updateVec.push_back(Update(out[i]*2, in[idx[a[i]]], out[idx[a[i]]], -1));
    }
    sort(updateVec.begin(), updateVec.end());
    tree.init(n-k+1, updateVec);
    tree.printAns();

    /// ���� ó���ϱ�
    while(q--){
        int x;
        scanf("%d", &x);
        /// x�� x+1�� ������ �۾�
        int in1 = in[x]*2-1, in2 = in[x+1]*2-1;
        int out1 = out[x]*2, out2 = out[x+1]*2;
        int ql = in[idx[a[x]]], qr = out[idx[a[x]]];

        for(int d=in1; d<in2; d++) tree.update(d, Update(d, ql, qr, -1));
        for(int d=out1+1; d<out2; d++) tree.update(d, Update(d, ql, qr, 1));

        /// x+1�� x�� ������ �۾�
        ql = in[idx[a[x+1]]], qr = out[idx[a[x+1]]];
        for(int d=in1; d<in2; d++) if(d&1) tree.update(d, Update(d, ql, qr, 1));
        for(int d=out1+1; d<out2; d++) if(d&1) tree.update(d, Update(d, ql, qr, -1));

        /// �� ��ȯ
        swap(a[x], a[x+1]);
        tree.printAns();
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |     scanf("%d %d %d", &n, &k, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:107:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |     for(int i=1; i<=n; i++) scanf("%d", &a[i]);
      |                             ~~~~~^~~~~~~~~~~~~
Main.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
Main.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 8 ms 7120 KB Output is correct
17 Correct 8 ms 7632 KB Output is correct
18 Correct 2 ms 996 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 13 ms 11984 KB Output is correct
21 Correct 11 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 13 ms 11472 KB Output is correct
24 Correct 11 ms 11736 KB Output is correct
25 Correct 13 ms 9792 KB Output is correct
26 Correct 13 ms 11088 KB Output is correct
27 Correct 14 ms 12680 KB Output is correct
28 Correct 12 ms 8656 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 8 ms 7120 KB Output is correct
17 Correct 8 ms 7632 KB Output is correct
18 Correct 2 ms 996 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 13 ms 11984 KB Output is correct
21 Correct 11 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 13 ms 11472 KB Output is correct
24 Correct 11 ms 11736 KB Output is correct
25 Correct 13 ms 9792 KB Output is correct
26 Correct 13 ms 11088 KB Output is correct
27 Correct 14 ms 12680 KB Output is correct
28 Correct 12 ms 8656 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
31 Correct 216 ms 177636 KB Output is correct
32 Correct 218 ms 188436 KB Output is correct
33 Correct 33 ms 14108 KB Output is correct
34 Correct 289 ms 216684 KB Output is correct
35 Correct 422 ms 317804 KB Output is correct
36 Correct 363 ms 358852 KB Output is correct
37 Correct 43 ms 31192 KB Output is correct
38 Correct 393 ms 302648 KB Output is correct
39 Correct 295 ms 315788 KB Output is correct
40 Correct 422 ms 263736 KB Output is correct
41 Correct 429 ms 301020 KB Output is correct
42 Correct 536 ms 357532 KB Output is correct
43 Correct 314 ms 233296 KB Output is correct
44 Correct 265 ms 171552 KB Output is correct
45 Correct 56 ms 39792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 8 ms 7120 KB Output is correct
17 Correct 8 ms 7632 KB Output is correct
18 Correct 2 ms 996 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 13 ms 11984 KB Output is correct
21 Correct 11 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 13 ms 11472 KB Output is correct
24 Correct 11 ms 11736 KB Output is correct
25 Correct 13 ms 9792 KB Output is correct
26 Correct 13 ms 11088 KB Output is correct
27 Correct 14 ms 12680 KB Output is correct
28 Correct 12 ms 8656 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 596 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 720 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 596 KB Output is correct
42 Correct 1 ms 596 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 1 ms 596 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 26 ms 22516 KB Output is correct
47 Correct 29 ms 24656 KB Output is correct
48 Correct 3 ms 956 KB Output is correct
49 Correct 22 ms 17132 KB Output is correct
50 Correct 51 ms 38856 KB Output is correct
51 Correct 5 ms 4356 KB Output is correct
52 Correct 30 ms 29400 KB Output is correct
53 Correct 48 ms 37304 KB Output is correct
54 Correct 42 ms 37508 KB Output is correct
55 Correct 19 ms 17816 KB Output is correct
56 Correct 63 ms 39032 KB Output is correct
57 Correct 53 ms 37904 KB Output is correct
58 Correct 10 ms 6732 KB Output is correct
59 Correct 44 ms 32764 KB Output is correct
60 Correct 8 ms 5688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 8 ms 7120 KB Output is correct
17 Correct 8 ms 7632 KB Output is correct
18 Correct 2 ms 996 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 13 ms 11984 KB Output is correct
21 Correct 11 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 13 ms 11472 KB Output is correct
24 Correct 11 ms 11736 KB Output is correct
25 Correct 13 ms 9792 KB Output is correct
26 Correct 13 ms 11088 KB Output is correct
27 Correct 14 ms 12680 KB Output is correct
28 Correct 12 ms 8656 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
31 Correct 216 ms 177636 KB Output is correct
32 Correct 218 ms 188436 KB Output is correct
33 Correct 33 ms 14108 KB Output is correct
34 Correct 289 ms 216684 KB Output is correct
35 Correct 422 ms 317804 KB Output is correct
36 Correct 363 ms 358852 KB Output is correct
37 Correct 43 ms 31192 KB Output is correct
38 Correct 393 ms 302648 KB Output is correct
39 Correct 295 ms 315788 KB Output is correct
40 Correct 422 ms 263736 KB Output is correct
41 Correct 429 ms 301020 KB Output is correct
42 Correct 536 ms 357532 KB Output is correct
43 Correct 314 ms 233296 KB Output is correct
44 Correct 265 ms 171552 KB Output is correct
45 Correct 56 ms 39792 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 596 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 596 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 1 ms 596 KB Output is correct
53 Correct 1 ms 596 KB Output is correct
54 Correct 1 ms 720 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 596 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 1 ms 596 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 26 ms 22516 KB Output is correct
62 Correct 29 ms 24656 KB Output is correct
63 Correct 3 ms 956 KB Output is correct
64 Correct 22 ms 17132 KB Output is correct
65 Correct 51 ms 38856 KB Output is correct
66 Correct 5 ms 4356 KB Output is correct
67 Correct 30 ms 29400 KB Output is correct
68 Correct 48 ms 37304 KB Output is correct
69 Correct 42 ms 37508 KB Output is correct
70 Correct 19 ms 17816 KB Output is correct
71 Correct 63 ms 39032 KB Output is correct
72 Correct 53 ms 37904 KB Output is correct
73 Correct 10 ms 6732 KB Output is correct
74 Correct 44 ms 32764 KB Output is correct
75 Correct 8 ms 5688 KB Output is correct
76 Runtime error 753 ms 524288 KB Execution killed with signal 9
77 Halted 0 ms 0 KB -