QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#655150#9313. Make Maxgrey_wolfTL 0ms3844kbC++203.6kb2024-10-19 00:51:002024-10-19 00:51:02

Judging History

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

  • [2024-10-19 00:51:02]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3844kb
  • [2024-10-19 00:51:00]
  • 提交

answer

#include <bits/stdc++.h>
#define vi vector<int>
#define vl vector<long long>
#define debug cout << "muhuuhuh" << endl
#define int long long
#define endl '\n'
#define F first
#define inv(a) for(auto &it : a) cin >> it;
#define outv(a) for(auto &it : a) cout << it << " ";
#define S second
#define eps 0.0000001
#define pb push_back
#define all(v) v.begin(), v.end()
// #define int long long
using namespace std;

class sgtree {
    vector<int> seg,lazy;
public:
    sgtree(int n) {
        seg.resize(4*n+5);
        lazy.resize(4*n+5, -1);
    }
    void build(int ind, int low, int high, vector<int> arr) {
        if(low==high) {
            seg[ind]=arr[low];
            return;
        }
        int mid = (low+high)/2;
        build(2*ind+1, low, mid, arr);
        build(2*ind+2, mid+1, high, arr);
        seg[ind] = max(seg[2*ind+1], seg[2*ind+2]);
    }

    int query(int ind, int low, int high, int l, int r) {
        if(lazy[ind]!=-1) {
            seg[ind] = (high-low+1)*lazy[ind];
            if(low!=high) {
                lazy[2*ind+1] = lazy[ind];
                lazy[2*ind+2] = lazy[ind];
            } 
            lazy[ind]=-1;
        }
        // 1. NO overlap
        // l r low high || low high l r
        if(r<low || high<l) return INT_MIN;
        // 2.  complete overlap
        if(low>=l && high<=r) return seg[ind];
        // 3. partial overlap
        int mid = (low+high)/2;
        int left = query(2*ind+1, low, mid, l, r);
        int right = query(2*ind+2, mid+1, high, l, r);
        return max(left, right);
    }
    void update(int ind, int low, int high, int l, int r, int val) {
        if(lazy[ind]!=-1) {
            seg[ind] = (high-low+1)*lazy[ind];
            if(low!=high) {
                lazy[2*ind+1] = lazy[ind];
                lazy[2*ind+2] = lazy[ind];
            } 
            lazy[ind]=-1;
        }
        if(high<l || r<low) return;
        if(low>=l && high<=r) {
            seg[ind] = (high-low+1)*val;
            if(low!=high) {
                lazy[2*ind+1] = val;
                lazy[2*ind+2] = val;
            } 
            return;
        }
        int mid = (low + high)/2;
        update(2*ind+1, low, mid, l, r, val);
        update(2*ind+2, mid+1, high, l, r, val);
        seg[ind]  = max(seg[2*ind+1] , seg[2*ind+2]);
    }
};

void solve() {
    int n;
    cin>>n;
    vi v(n);
    inv(v);
    sgtree tr(n);
    tr.build(0,0,n-1,v);

    // sgtree tr1(n);
    // tr1.build(0,0,n-1,v);
    int ans = 0;
    for (int i = 1; i < n; i++)
    {
        int lo = 0,hi=i-1,now=INT_MAX;
        while(lo<=hi) {
            int mid = (lo+hi)/2;
            int x = tr.query(0,0,n-1,mid,i-1);
            if(x<v[i]) {
                hi = mid - 1;
                now = min(now, mid);
            }
            else lo = mid +1;
        }
        if(now==INT_MAX) continue;
        
        ans+=abs(i-now);
        tr.update(0,0,n-1,now,i-1,v[i]);
    }
    reverse(all(v));

    for (int i = 1; i < n; i++)
    {
        int lo = 0,hi=i-1,now=INT_MAX;
        while(lo<=hi) {
            int mid = (lo+hi)/2;
            int x = tr.query(0,0,n-1,mid,i-1);
            if(x<v[i]) {
                hi = mid - 1;
                now = min(now, mid);
            }
            else lo = mid +1;
        }
        if(now==INT_MAX) continue;
        
        ans+=abs(i-now);
        tr.update(0,0,n-1,now,i-1,v[i]);
    }
    
    cout<<ans<<endl;
}
int32_t main() {
  
    
    int t;
    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: 3844kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Time Limit Exceeded

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:


result: