Submission #879886

# Submission time Handle Problem Language Result Execution time Memory
879886 2023-11-28T09:10:55 Z noyancanturk Sails (IOI07_sails) C++17
100 / 100
153 ms 4700 KB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
    const int lim=1e5+100;
#else
    const int lim=3e3+100;
#endif

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

#define int long long
#define pb push_back

const int mod=998244353;
using pii=pair<int,int>;

struct BIT{
    int n,tree[lim];
    BIT();
    BIT(int n):n(n){
        memset(tree,0,sizeof(int)*(n+1));
    }
    BIT(int n,int*ref){
        memset(tree,0,sizeof(int)*(n+1));
        for(int i=1;i<=n;i++){
            tree[i]+=ref[i];
            if(i+(i&-i)<=n)tree[i+(i&-i)]+=tree[i];
        }
    }
    inline void update(int p,int val){
        while(p<=n){
            tree[p]+=val;
            p+=p&-p;
        }
    }
    inline int query(int p){
        int res=0;
        while(p){
            res+=tree[p];
            p-=p&-p;
        }
        return res;
    }
};
 
struct RangeBIT{
    BIT a,b;
    RangeBIT(int n):a(n),b(n){}
    inline void update(int l,int r,int x){
        a.update(l,x);
        a.update(r+1,-x);
        b.update(l,x*(l-1));
        b.update(r+1,-x*r);
    }
    inline void update(int p,int x){
        update(p,p,x);
    }
    inline int sumy(int p){
        return p * a.query(p) - b.query(p);
    }
    inline int query(int l,int r){
        return sumy(r)-sumy(l-1);
    }
    inline int query(int p){
        return query(p,p);
    }
};

void solve(){
    int n;
    cin>>n;
    pii mast[n];
    for(int i=0;i<n;i++){
        cin>>mast[i].first>>mast[i].second;
    }
    sort(mast,mast+n);
    RangeBIT bit(lim-1);
    for(int i=0;i<n;i++){
        auto [len,toget]=mast[i];
        int indy=len-toget+1;
        int maxval=bit.query(indy);
        int l=1,r=len,firstind=-1;
        while(l<=r){
            int mid=(l+r)>>1;
            //cerr<<l<<" "<<mid<<" "<<r<<"\n";
            int valy=bit.query(mid);
            if(valy<maxval){
                r=mid-1;
            }else if(maxval<valy){
                l=mid+1;
            }else{
                firstind=mid;
                l=mid+1;
            }
        }
        //cerr<<firstind+1<<" "<<len<<"\n";
        bit.update(firstind+1,len,1);
        toget-=len-firstind;
        l=1,r=firstind;
        int lastind=-1;
        while(l<=r){
            int mid=(l+r)>>1;
            //cerr<<l<<" "<<mid<<" "<<r<<"\n";
            int valy=bit.query(mid);
            if(valy<maxval){
                r=mid-1;
            }else if(maxval<valy){
                l=mid+1;
            }else{
                lastind=mid;
                r=mid-1;
            }
        }
        bit.update(lastind,lastind+toget-1,1);
        /*
        for(int i=1;i<=10;i++){
            cerr<<bit.query(i)<<" ";
        }
        */
        //cerr<<"\n";
        //cerr<<"ok\n";
    }
    int ans=0;
    for(int i=1;i<lim;i++){
        int k=bit.query(i);
        ans+=k*(k-1)/2;
    }
    cout<<ans<<"\n";
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
#ifdef Local  
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#else
    //freopen("optmilk.in","r",stdin);
    //freopen("optmilk.out","w",stdout);
#endif
    int t=1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1880 KB Output is correct
2 Correct 4 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1884 KB Output is correct
2 Correct 3 ms 2016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1956 KB Output is correct
2 Correct 3 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1884 KB Output is correct
2 Correct 3 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1884 KB Output is correct
2 Correct 5 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 2140 KB Output is correct
2 Correct 29 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 3332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 3928 KB Output is correct
2 Correct 106 ms 4084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 4444 KB Output is correct
2 Correct 100 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 4700 KB Output is correct
2 Correct 98 ms 4440 KB Output is correct