Submission #546679

# Submission time Handle Problem Language Result Execution time Memory
546679 2022-04-08T05:59:35 Z Soul234 Sails (IOI07_sails) C++14
100 / 100
119 ms 4664 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

const int MX = 1e5;
int fst[2<<17], lst[2<<17], D[MX+5];
int N;
pi A[MX];

void upd(int id, int val, int ind = 1, int L = 0, int R = MX+1) {
    if(L+1 == R) {
        D[id] += val;
        if(D[id]) fst[ind] = lst[ind] = id;
        else fst[ind] = lst[ind] = -1;
    }
    else {
        int mid = (L + R) >> 1;
        if(id>=mid) upd(id, val, ind<<1|1, mid, R);
        else upd(id,val, ind<<1, L, mid);
        fst[ind] = fst[ind<<1] != -1 ? fst[ind<<1] : fst[ind<<1|1];
        lst[ind] = lst[ind<<1|1] != -1 ? lst[ind<<1|1] : lst[ind<<1];
    }
}

int queryFst(int l, int r, int ind = 1, int L = 0, int R = MX+1) {
    if(L >= r || R <= l) return -1;
    if(l <= L && R <= r) return fst[ind];
    int mid = (L + R) >> 1;
    int res = queryFst(l, r, ind<<1, L, mid);
    if(res != -1) return res;
    return queryFst(l, r, ind<<1|1, mid, R);
}

int queryLst(int l, int r, int ind = 1, int L = 0, int R = MX+1) {
    if(L >= r || R <= l) return -1;
    if(l <= L && R <= r) return lst[ind];
    int mid = (L + R) >> 1;
    int res = queryLst(l, r, ind<<1|1, mid, R);
    if(res != -1) return res;
    return queryLst(l, r, ind<<1, L, mid);
}

ll ans,cur;

void solve() {
    memset(fst, -1, sizeof fst);
    memset(lst, -1, sizeof lst);
    upd(0, MX+5);
    cin>>N;
    F0R(i,N) cin>>A[i].first>>A[i].second;
    sort(A,A+N);
    F0R(i,N) {
        int h0 = A[i].first - A[i].second;
        int gr1 = queryFst(h0, MX+1);
        int gr2 = queryLst(0, h0 + 1);
        if(gr1 == -1) gr1 = A[i].first;
        upd(A[i].first, 1);
        upd(gr1, -1);
        upd(gr2+gr1-h0, 1);
        upd(gr2, -1);
    }
    ROF(i,1,MX+1) {
        cur += D[i];
        ans += cur*(cur-1)/2;
    }
    cout << ans << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

sails.cpp: In function 'void setIO(std::string)':
sails.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2388 KB Output is correct
2 Correct 2 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2388 KB Output is correct
2 Correct 1 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2260 KB Output is correct
2 Correct 2 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2388 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2516 KB Output is correct
2 Correct 30 ms 2876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 3040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 3484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 4184 KB Output is correct
2 Correct 82 ms 4220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 4392 KB Output is correct
2 Correct 92 ms 4120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 4664 KB Output is correct
2 Correct 82 ms 4256 KB Output is correct