QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#307918#8016. 不休陀螺Ryan1230 0ms0kbC++142.2kb2024-01-19 11:23:582024-01-19 11:23:59

Judging History

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

  • [2024-01-19 11:23:59]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-01-19 11:23:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
#define int LL

set<int> S1, S2;

struct BIT{
    LL sum[N];

    void add(int x, int v){
        for(int i = x; i < N; i += i & (-i))sum[i] += v;
    }

    LL query(int x){
        LL ans = 0;
        for(int i = x; i > 0; i -= i & (-i))ans += sum[i];
        return ans;
    }
}tr;

int a[N], b[N];
LL sum[N], K;
int numx[N], k;
LL res;

bool check(){
    int a, b;
    if(S1.empty())a = 0;
    else {
        auto it = S1.end();
        it --;
        a = *it;
    }
    if(S2.empty())b = 0;
    else {
        auto it = S2.end();
        it --;
        b = *it;
    }

    if(res + max(a, b) <= K)return 1;
    return 0;
}

int get(LL x){
    return lower_bound(numx + 1, numx + k + 1, x) - numx;
}

signed main(){
    // freopen("top.in", "r", stdin);
    // freopen("top.out", "w", stdout);

    int n;
    scanf("%d%lld", &n, &K);

    for(int i = 1; i <= n; i ++)scanf("%d", &a[i]);
    for(int i = 1; i <= n; i ++)scanf("%d", &b[i]);

    for(int i = 1; i <= n; i ++){
        sum[i] = sum[i - 1] + b[i] - a[i];
        numx[++ k] = sum[i];
    }

    sort(numx + 1, numx + k + 1);
    k = unique(numx + 1, numx + k + 1) - numx - 1;


    int j = 1;
    LL ans = 0;
    for(int i = 1; i <= n; i ++){
        while(j <= n){
            if(a[j] >= b[j]){
                res += (a[j] - b[j]);
                S2.insert(b[j]);
            }
            else S1.insert(a[j]);
            tr.add(get(sum[j]), 1);
            if(!check()){
                if(a[j] >= b[j]){
                    res -= (a[j] - b[j]);
                    S2.erase(b[j]);
                }
                else S1.erase(a[j]);
                tr.add(get(sum[j]), -1);
                break;
            }
            j ++;
        }
        
        // printf("%d ", j - i);
        // printf("%d\n", tr.query(get(sum[i - 1]) - 1));

        ans += j - i - tr.query(get(sum[i - 1]) - 1);
        tr.add(get(sum[i]), -1);
        if(a[i] >= b[i]){
            res -= (a[i] - b[i]);
            S2.erase(b[i]);
        }
        else S1.erase(a[i]);

        // printf("%d\n", ans);
    }

    printf("%lld\n", ans);
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

5000 939255322
47952340 92329911 61615795 40122788 47258178 29326499 9822850 42767362 86610596 60318756 52429688 87502511 50194916 96377063 74322128 19511341 28794957 53813791 79075058 35555414 5249682 45174421 101856091 25257909 94697470 45853817 82945426 108415825 41731145 87133877 75167193 598696...

output:


result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #5:

score: 0
Time Limit Exceeded

input:

774484 763692678
47702350 34856775 28447988 4178162 45063720 8232662 36845607 27038945 44858289 5952529 39159657 21628528 60199611 5544054 59216841 39287087 43449994 20034684 56440004 11583811 44465341 32347476 49196492 22731571 9481143 11726859 35167370 23103544 23109378 38822668 29778048 58004104 ...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #10:

score: 0
Time Limit Exceeded

input:

1000000 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:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #14:

score: 0
Time Limit Exceeded

input:

174457 888
0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 1...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #18:

score: 0
Time Limit Exceeded

input:

343922 773619774
0 8292680 5684115 0 0 170056 5385926 0 0 1588575 0 0 10947891 170867 35145 0 0 103085 7231562 0 0 0 0 11128944 0 4872226 0 2879880 7565181 0 8631665 0 5162564 9511835 514165 0 9628987 14357934 174784 0 12400154 0 0 8198218 0 8496060 0 0 0 0 10376826 3523227 0 14548249 0 6840016 0 0 ...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #24:

score: 0
Time Limit Exceeded

input:

468676 582048177
6889433 7293342 20676061 15545414 4911497 12352219 8921719 1705801 19695926 25259227 2645394 17518171 19753552 9449377 982708 22479531 1267985 15594372 20685422 9627290 2017543 6459134 18614020 16206301 14962487 12932255 7101003 29140540 6479702 20607124 2540287 15565156 20274141 11...

output:


result: