QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729963#9584. 顾影自怜333zhanTL 793ms73732kbC++204.3kb2024-11-09 18:07:422024-11-09 18:07:43

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 18:07:43]
  • 评测
  • 测评结果:TL
  • 用时:793ms
  • 内存:73732kb
  • [2024-11-09 18:07:42]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)

using namespace std;
template <class Info>
struct SegmentTree {
    vector <Info> tr;
    int n;
    
    SegmentTree () : n (0) {}
    
    SegmentTree (int N, vector <Info> a) : tr (N << 2 | 1) {
        n = N;
        auto build = [&] (auto self, int rt, int l, int r) -> void {
            if (l == r) {
                tr[rt] = a[l];
                return;
            }
            int mid = (l + r) >> 1;
            self (self, lson, l, mid); self (self, rson, mid + 1, r);
            pushup (rt);
		};
		build (build, 1, 1, n);
    }
    
    SegmentTree (int N) : tr (N << 2 | 1) {
        n = N;
    }
    
    void pushup (int rt) {
        tr[rt] = tr[lson] + tr[rson];
    }
    
    void modify (int rt, int l, int r, int x, const Info &y) {
        if (l == r) {
            tr[rt] = y;
            return;
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            modify (lson, l, mid, x, y);
        } else {
            modify (rson, mid + 1, r, x, y);
        }
        pushup (rt);
    }
    
    Info query (int rt, int l, int r, int x, int y) {
        if (l >= x && r <= y) {
            return tr[rt];
        }
        int mid = (l + r) >> 1;
        if (x <= mid && y > mid) {
            return query (lson, l, mid, x, y) + query (rson, mid + 1, r, x, y);
        }
        if (x <= mid) return query (lson, l, mid, x, y);
        return query (rson, mid + 1, r, x, y);
    }
    
    template <class F>
    int findFirst (int rt, int l, int r, int x, int y, F &&pred) {
        if (l > y || r < x) {
            return -1;
        }
        if (l >= x && r <= y && ! pred (tr[rt])) {
            return -1;
        } 
        if (l == r) {
            return l;
        }
        int mid = (l + r) / 2;
        int ans = findFirst (lson, l, mid, x, y, pred);
        if (ans == -1) {
            ans = findFirst (rson, mid + 1, r, x, y, pred);
        }
        return ans;
    }

    template <class F>
    int findLast (int rt, int l, int r, int x, int y, F &&pred) {
        if (l > y || r < x) {
            return -1;
        }
        if (l >= x && r <= y && ! pred (tr[rt])) {
            return -1;
        } 
        if (l == r) {
            return l;
        }
        int mid = (l + r) / 2;
        int ans = findLast (rson, mid + 1, r, x, y, pred);
        if (ans == -1) {
            ans = findLast (lson, l, mid, x, y, pred);
        }
        return ans;
    }
};

struct Info {
    int maxx = 0;
};

Info operator+(const Info &x, const Info &y) {
    Info c;
    c.maxx = max (x.maxx, y.maxx);
    return c;
}

void solve () {
    int n, k;
    cin >> n >> k;

    vector <int> a (n + 1);
    vector <vector <int>> id (n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        id[a[i]].push_back (i);
    }

    int ans = 0;
    SegmentTree <Info> seg (n);
    for (int i = n; i >= 1; i --) {
        const int m = id[i].size ();
        for (int j = 0; j <= m - k; j ++) {
            if (seg.query (1, 1, n, id[i][j], id[i][j + k - 1]).maxx == 0) {
                int x = seg.findLast (1, 1, n, 1, id[i][j], 
                    [&](const Info &info) {
                        return info.maxx > 0;
                    }
                );
                if (x == -1) {
                    x = 1;
                } else {
                    x ++;
                }
                int y = seg.findFirst (1, 1, n, id[i][j + k - 1], n, 
                    [&](const Info &info) {
                        return info.maxx > 0;
                    }
                );
                if (y == -1) {
                    y = n;
                } else {
                    y --;
                }
                ans += (id[i][j] - x + 1) * (y - id[i][j + k - 1] + 1);
            }
            seg.modify (1, 1, n, id[i][j], {1});
        }
        for (auto x : id[i]) {
            seg.modify (1, 1, n, x, {1});
        }
    }

    cout << ans << '\n';
}

signed main () {
    ios::sync_with_stdio (false);
    cin.tie (nullptr);

    int T = 1;
    cin >> T;

    while (T --) {
        solve ();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3592kb

input:

2
5 2
1 3 3 2 2
4 3
1 4 2 1

output:

7
0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 394ms
memory: 73488kb

input:

1
1000000 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 ...

output:

500000500000

result:

ok single line: '500000500000'

Test #3:

score: 0
Accepted
time: 105ms
memory: 3880kb

input:

158921
1 1
1
2 1
1 1
2 2
1 1
2 1
1 2
2 2
1 2
2 1
2 1
2 2
2 1
2 1
2 2
2 2
2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
3 1
1 1 2
3 2
1 1 2
3 3
1 1 2
3 1
1 1 3
3 2
1 1 3
3 3
1 1 3
3 1
1 2 1
3 2
1 2 1
3 3
1 2 1
3 1
1 2 2
3 2
1 2 2
3 3
1 2 2
3 1
1 2 3
3 2
1 2 3
3 3
1 2 3
3 1
1 3 1
3 2
1 3 1
3 3
1 3 1
3 1
1 3 2
3 2...

output:

1
3
1
3
0
3
0
3
1
6
3
1
6
1
0
6
1
0
6
0
0
6
2
0
6
0
0
6
0
0
6
0
0
6
2
0
6
1
0
6
1
0
6
0
0
6
2
0
6
3
1
6
1
0
6
0
0
6
0
0
6
2
0
6
1
0
6
0
0
6
1
0
6
0
0
6
1
0
6
1
0
6
2
0
6
2
0
6
3
1
10
6
3
1
10
3
1
0
10
3
1
0
10
3
1
0
10
1
0
0
10
4
0
0
10
1
0
0
10
1
0
0
10
1
0
0
10
1
0
0
10
4
0
0
10
1
0
0
10
1
0
0
10
...

result:

ok 158921 lines

Test #4:

score: 0
Accepted
time: 409ms
memory: 73732kb

input:

1
1000000 1000
1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 100...

output:

498003996002

result:

ok single line: '498003996002'

Test #5:

score: 0
Accepted
time: 793ms
memory: 47444kb

input:

24
217838 1
11125 61539 156386 82530 15545 110745 20051 71328 216682 107717 24565 71482 196259 23920 79507 74383 81434 99615 50571 31499 93241 126374 205674 57290 166999 83044 89340 72667 55001 143151 30341 98608 191197 96249 176106 113045 116438 34417 92531 200248 38119 106697 24629 117110 129552 1...

output:

23726806041
1590954891
782239681
139362697540
3608208775
205782590
1992948
1449
78
2701
6
1
720
4660
34
9
91
21
1
18
3
10
1
1

result:

ok 24 lines

Test #6:

score: -100
Time Limit Exceeded

input:

26
946385 1
670117 545657 843923 412448 720179 557739 318687 474032 740066 816184 884900 216879 154424 237010 571714 191989 697715 453717 521834 329605 911786 112304 586798 162144 800808 303697 80404 576671 153923 684638 852686 842726 624241 934235 466016 691917 89781 33743 257181 791555 572665 1112...

output:

447822757305
14857662
13995820
5722
8260080
51681
7918210
1898855
99360
12597690
16005
2
4
1
2
1
6
2
66
1
205
6
1
3
3
1

result: