# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
377076 | ansol4328 | Sails (IOI07_sails) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>#define F first#define S secondusing namespace std;typedef long long ll;typedef pair<int,int> P;int n;P m[100005];int A[100005];bool in[100005];set<int> St;int main(){ scanf("%d",&n); for(int i=1 ; i<=n ; i++) scanf("%d %d",&m[i].F,&m[i].S); sort(m+1,m+1+n); int H=m[n].F; St.insert(H+1); in[H+1]=true; A[H+1]=-1; for(int i=1 ; i<=n ; i++){ int cur=H-m[i].F+1; if(m[i].F!=m[i-1].F){ int tmp=*(St.begin()); if(A[tmp]==0 && in[tmp]) St.erase(tmp), in[tmp]=false; St.insert(cur), in[cur]=true; } auto it=St.upper_bound(cur+m[i].S-1); int fn=*it; it--; int st=*it; if(fn!=cur+m[i].S){ int x=(fn-1)-(cur+m[i].S-1-st); // x~fn-1, cur~st-1 A[cur]++, A[st]--; A[x]++, A[fn]--; if(A[cur]==1 && !in[cur]) St.insert(cur), in[cur]=true; if(A[x]==1 && !in[x]) St.insert(x), in[x]=true; if(cur!=st && A[st]==0 && in[st]) St.erase(st), in[st]=false; if(A[fn]==0 && in[fn]) St.erase(fn), in[fn]=false; } else{ //cur~fn-1 A[cur]++; A[fn]--; if(A[cur]==1 && !in[cur]) St.insert(cur), in[cur]=true; if(A[fn]==0 && in[fn]) St.erase(fn), in[fn]=false; } } //calculating ll ans=0, v=0; for(int i=1 ; i<=H ; i++){ v+=A[i]; ans+=v*(v-1)/2LL; } printf("%lld",ans); return 0;}