Submission #963740

# Submission time Handle Problem Language Result Execution time Memory
963740 2024-04-15T15:08:20 Z Icelast Sails (IOI07_sails) C++17
70 / 100
1000 ms 9532 KB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 1e5+5, INF = 4e18+9;
ll n;
struct mast{
    ll h, k;
};
mast a[maxn];
bool cmp(mast a, mast b){
    return a.h < b.h;
}
void inp(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i].h >> a[i].k;
    }
}
ll N, T[4*maxn], lz[4*maxn];
void build_segtree(){
    N = 1;
    while(N < 1e5){
        N*=2;
    }
}
void down(int node, int len){
    for(int child = node*2; child <= node*2+1; child++){
        if(child >= 2*N) continue;
        lz[child] += lz[node];
    }
    T[node]+=lz[node]*len;
    lz[node] = 0;
}
void upd(int node, int low, int high, int l, int r, ll x){
    if(r < l) return;
    down(node, high-low+1);
    if(low > r || l > high){
        return;
    }
    if(low >= l && r >= high){
        lz[node] += x;
        down(node, high-low+1);

        return;
    }
    int mid = (low+high)/2;
    upd(node*2, low, mid, l, r, x);
    upd(node*2+1, mid+1, high, l, r, x);
    T[node] = T[node*2]+T[node*2+1];
}
ll get_sum(int node, int low, int high, int l, int r){
    if(r < l) r = l;
    down(node, high-low+1);
    if(low > r || l > high){
        return 0;
    }
    if(low >= l && r >= high){
        return T[node];
    }
    int mid = (low+high)/2;
    return get_sum(node*2, low, mid, l, r)+get_sum(node*2+1, mid+1, high, l, r);
}
int get_pos(ll k, ll M){
    ll l = 1, r = M, mid;
    while(l <= r){
        mid = (l+r)/2;
        if(get_sum(1, 1, N, mid, N)-get_sum(1, 1, N, mid+1, N) >= k){
            l = mid+1;
        }else{
            r = mid-1;
        }
    }
    return l-1;
}
void solve(){
    sort(a+1, a+1+n, cmp);
    build_segtree();
    ll M, k;
    for(int i = 1; i <= n; i++){
        M = a[i].h; k = a[i].k;
        ll x = get_sum(1, 1, N, M-k+1, M-k+1);

        int u = get_pos(x, M);
        int d = get_pos(x+1, M)+1;
        upd(1, 1, N, u+1, M, 1);
        upd(1, 1, N, d, d+u-(M-k+1), 1);
    }
    ll ans = 0;
    for(int i = 1; i <= M; i++){
        ll x = get_sum(1, 1, N, i, i);
        ans += x*(x-1)/2;
    }
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    inp();
    solve();
}

Compilation message

sails.cpp: In function 'void solve()':
sails.cpp:90:22: warning: 'M' may be used uninitialized in this function [-Wmaybe-uninitialized]
   90 |     for(int i = 1; i <= M; i++){
      |                    ~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 6 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6504 KB Output is correct
2 Correct 33 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 6780 KB Output is correct
2 Correct 261 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 312 ms 7400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 568 ms 8132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1014 ms 8728 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1043 ms 9104 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1022 ms 9532 KB Time limit exceeded
2 Halted 0 ms 0 KB -