Submission #791561

# Submission time Handle Problem Language Result Execution time Memory
791561 2023-07-24T07:26:39 Z 반딧불(#10046) Sličnost (COI23_slicnost) C++17
67 / 100
730 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++) tree.update(d, Update(d, ql, qr, 1));
        for(int d=out1+1; d<out2; d++) 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);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 7 ms 7120 KB Output is correct
17 Correct 7 ms 7632 KB Output is correct
18 Correct 2 ms 976 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 15 ms 12028 KB Output is correct
21 Correct 12 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 12 ms 11404 KB Output is correct
24 Correct 11 ms 11812 KB Output is correct
25 Correct 11 ms 9804 KB Output is correct
26 Correct 12 ms 11080 KB Output is correct
27 Correct 13 ms 12624 KB Output is correct
28 Correct 10 ms 8688 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 7 ms 7120 KB Output is correct
17 Correct 7 ms 7632 KB Output is correct
18 Correct 2 ms 976 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 15 ms 12028 KB Output is correct
21 Correct 12 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 12 ms 11404 KB Output is correct
24 Correct 11 ms 11812 KB Output is correct
25 Correct 11 ms 9804 KB Output is correct
26 Correct 12 ms 11080 KB Output is correct
27 Correct 13 ms 12624 KB Output is correct
28 Correct 10 ms 8688 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
31 Correct 203 ms 177700 KB Output is correct
32 Correct 225 ms 188456 KB Output is correct
33 Correct 26 ms 13988 KB Output is correct
34 Correct 291 ms 216764 KB Output is correct
35 Correct 442 ms 317832 KB Output is correct
36 Correct 329 ms 358856 KB Output is correct
37 Correct 41 ms 31128 KB Output is correct
38 Correct 395 ms 302640 KB Output is correct
39 Correct 303 ms 315748 KB Output is correct
40 Correct 370 ms 263660 KB Output is correct
41 Correct 425 ms 300976 KB Output is correct
42 Correct 572 ms 357600 KB Output is correct
43 Correct 317 ms 233300 KB Output is correct
44 Correct 223 ms 171424 KB Output is correct
45 Correct 55 ms 39808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 468 KB Output is correct
20 Correct 1 ms 664 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 724 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 7 ms 7120 KB Output is correct
17 Correct 7 ms 7632 KB Output is correct
18 Correct 2 ms 976 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 15 ms 12028 KB Output is correct
21 Correct 12 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 12 ms 11404 KB Output is correct
24 Correct 11 ms 11812 KB Output is correct
25 Correct 11 ms 9804 KB Output is correct
26 Correct 12 ms 11080 KB Output is correct
27 Correct 13 ms 12624 KB Output is correct
28 Correct 10 ms 8688 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 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 468 KB Output is correct
35 Correct 1 ms 664 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 724 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 32 ms 25672 KB Output is correct
47 Correct 33 ms 28008 KB Output is correct
48 Correct 3 ms 976 KB Output is correct
49 Correct 24 ms 18864 KB Output is correct
50 Correct 57 ms 44324 KB Output is correct
51 Correct 6 ms 4816 KB Output is correct
52 Correct 34 ms 33092 KB Output is correct
53 Correct 53 ms 42420 KB Output is correct
54 Correct 48 ms 42728 KB Output is correct
55 Correct 21 ms 21092 KB Output is correct
56 Correct 58 ms 44404 KB Output is correct
57 Correct 55 ms 43192 KB Output is correct
58 Correct 9 ms 7260 KB Output is correct
59 Correct 49 ms 37032 KB Output is correct
60 Correct 8 ms 6096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 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 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 7 ms 7120 KB Output is correct
17 Correct 7 ms 7632 KB Output is correct
18 Correct 2 ms 976 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 15 ms 12028 KB Output is correct
21 Correct 12 ms 12624 KB Output is correct
22 Correct 2 ms 1488 KB Output is correct
23 Correct 12 ms 11404 KB Output is correct
24 Correct 11 ms 11812 KB Output is correct
25 Correct 11 ms 9804 KB Output is correct
26 Correct 12 ms 11080 KB Output is correct
27 Correct 13 ms 12624 KB Output is correct
28 Correct 10 ms 8688 KB Output is correct
29 Correct 7 ms 6480 KB Output is correct
30 Correct 2 ms 1744 KB Output is correct
31 Correct 203 ms 177700 KB Output is correct
32 Correct 225 ms 188456 KB Output is correct
33 Correct 26 ms 13988 KB Output is correct
34 Correct 291 ms 216764 KB Output is correct
35 Correct 442 ms 317832 KB Output is correct
36 Correct 329 ms 358856 KB Output is correct
37 Correct 41 ms 31128 KB Output is correct
38 Correct 395 ms 302640 KB Output is correct
39 Correct 303 ms 315748 KB Output is correct
40 Correct 370 ms 263660 KB Output is correct
41 Correct 425 ms 300976 KB Output is correct
42 Correct 572 ms 357600 KB Output is correct
43 Correct 317 ms 233300 KB Output is correct
44 Correct 223 ms 171424 KB Output is correct
45 Correct 55 ms 39808 KB Output is correct
46 Correct 1 ms 596 KB Output is correct
47 Correct 1 ms 596 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 468 KB Output is correct
50 Correct 1 ms 664 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 724 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 32 ms 25672 KB Output is correct
62 Correct 33 ms 28008 KB Output is correct
63 Correct 3 ms 976 KB Output is correct
64 Correct 24 ms 18864 KB Output is correct
65 Correct 57 ms 44324 KB Output is correct
66 Correct 6 ms 4816 KB Output is correct
67 Correct 34 ms 33092 KB Output is correct
68 Correct 53 ms 42420 KB Output is correct
69 Correct 48 ms 42728 KB Output is correct
70 Correct 21 ms 21092 KB Output is correct
71 Correct 58 ms 44404 KB Output is correct
72 Correct 55 ms 43192 KB Output is correct
73 Correct 9 ms 7260 KB Output is correct
74 Correct 49 ms 37032 KB Output is correct
75 Correct 8 ms 6096 KB Output is correct
76 Runtime error 730 ms 524288 KB Execution killed with signal 9
77 Halted 0 ms 0 KB -