Submission #879880

# Submission time Handle Problem Language Result Execution time Memory
879880 2023-11-28T09:02:50 Z noyancanturk Sails (IOI07_sails) C++17
0 / 100
27 ms 8020 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);
    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();
    }
}

Compilation message

In file included from /usr/include/string.h:495,
                 from /usr/include/c++/10/cstring:42,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:48,
                 from sails.cpp:8:
In function 'void* memset(void*, int, size_t)',
    inlined from 'BIT::BIT(long long int)' at sails.cpp:21:15,
    inlined from 'RangeBIT::RangeBIT(long long int)' at sails.cpp:48:29,
    inlined from 'void solve()' at sails.cpp:77:21:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:71:33: warning: 'void* __builtin___memset_chk(void*, int, long unsigned int, long unsigned int)' forming offset [1601616, 1601623] is out of the bounds [0, 1601616] of object 'bit' with type 'RangeBIT' [-Warray-bounds]
   71 |   return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
      |          ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp: In function 'void solve()':
sails.cpp:77:14: note: 'bit' declared here
   77 |     RangeBIT bit(lim);
      |              ^~~
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3672 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3676 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3676 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3672 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3676 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 3932 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 4900 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 13 ms 5720 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 19 ms 7004 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 21 ms 7516 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 8020 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -