Submission #109738

# Submission time Handle Problem Language Result Execution time Memory
109738 2019-05-07T20:38:41 Z nikolapesic2802 Sails (IOI07_sails) C++14
100 / 100
234 ms 6396 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T>>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

const int N=1e5+5;
vector<pair<int,int> > sails;
vector<int> minn(4*N),maxx(4*N),lazy(4*N);
void prop(int i)
{
    minn[2*i]+=lazy[i];
    minn[2*i+1]+=lazy[i];
    maxx[2*i]+=lazy[i];
    maxx[2*i+1]+=lazy[i];
    lazy[2*i]+=lazy[i];
    lazy[2*i+1]+=lazy[i];
    lazy[i]=0;
}
void update(int i)
{
    minn[i]=min(minn[2*i],minn[2*i+1]);
    maxx[i]=max(maxx[2*i],maxx[2*i+1]);
}
void inc(int qs,int qe,int l=0,int r=N-1,int i=1)
{
    if(qs>qe)
        return;
    if(qs>r||qe<l)
        return;
    if(qs<=l&&qe>=r)
    {
        lazy[i]++;
        minn[i]++;
        maxx[i]++;
        return;
    }
    prop(i);
    int m=(l+r)>>1;
    inc(qs,qe,l,m,2*i);
    inc(qs,qe,m+1,r,2*i+1);
    update(i);
}
int get(int pos,int l=0,int r=N-1,int i=1)
{
    if(l>pos||r<pos)
        return 0;
    if(l==pos&&r==pos)
        return minn[i];
    prop(i);
    int m=(l+r)>>1;
    return get(pos,l,m,2*i)+get(pos,m+1,r,2*i+1);
}
int getfirst(int vr,int l=0,int r=N-1,int i=1)
{
    if(l==r)
        return l;
    prop(i);
    int m=(l+r)>>1;
    if(maxx[2*i]>=vr)
        return getfirst(vr,l,m,2*i);
    else
        return getfirst(vr,m+1,r,2*i+1);
}
int getlast(int vr,int l=0,int r=N-1,int i=1)
{
    if(l==r)
        return l;
    prop(i);
    int m=(l+r)>>1;
    if(minn[2*i+1]<=vr)
        return getlast(vr,m+1,r,2*i+1);
    else
        return getlast(vr,l,m,2*i);
}
void add(int h,int k)
{
    int start=N-h;
    int koji=get(start+k-1);
    int l=max(start,getfirst(koji)),r=getlast(koji);
    int ostalo=start+k-l;
    inc(start,l-1);
    inc(r-ostalo+1,r);
}
ll getres(int l=0,int r=N-1,int i=1)
{
    if(l==r)
        return (ll)minn[i]*(minn[i]-1)/2;
    prop(i);
    int m=(l+r)>>1;
    return getres(l,m,2*i)+getres(m+1,r,2*i+1);
}
int main()
{
    int n;
	scanf("%i",&n);
	for(int i=0;i<n;i++)
    {
        int a,b;
        scanf("%i %i",&a,&b);
        sails.pb({a,b});
    }
    sort(all(sails));
    for(auto p:sails)
        add(p.f,p.s);
    printf("%lld\n",getres());
    return 0;
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
sails.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i %i",&a,&b);
         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 4992 KB Output is correct
2 Correct 9 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 5168 KB Output is correct
2 Correct 10 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 5148 KB Output is correct
2 Correct 57 ms 5492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 5628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 6136 KB Output is correct
2 Correct 158 ms 6396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 6232 KB Output is correct
2 Correct 113 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 234 ms 6128 KB Output is correct
2 Correct 154 ms 6136 KB Output is correct