답안 #904824

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
904824 2024-01-12T09:14:16 Z JakobZorz Sails (IOI07_sails) C++17
100 / 100
820 ms 5356 KB
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second

const int TREE_SIZE=1<<17;

ll tree[2*TREE_SIZE];
ll lazy[2*TREE_SIZE];
int beg=TREE_SIZE;

void push(int node,int len){
    if(lazy[node]){
        tree[node]+=(ll)len*lazy[node];
        if(node<TREE_SIZE){
            lazy[2*node]+=lazy[node];
            lazy[2*node+1]+=lazy[node];
        }
        lazy[node]=0;
    }
}

ll get_sum(int node,int l,int r,int rl,int rr){
    push(node,rr-rl);
    
    if(r<=rl||rr<=l)
        return 0;
    if(l<=rl&&rr<=r)
        return tree[node];
    
    int m=(rl+rr)/2;
    return get_sum(2*node,l,r,rl,m)+get_sum(2*node+1,l,r,m,rr);
}

ll get_sum(int l,int r){
    return get_sum(1,l,r,0,TREE_SIZE);
}

void update(int node,int l,int r,int rl,int rr,int v){
    push(node,rr-rl);
    if(r<=rl||rr<=l)
        return;
    if(l<=rl&&rr<=r){
        lazy[node]+=v;
        push(node,rr-rl);
        return;
    }
    
    int m=(rl+rr)/2;
    update(2*node,l,r,rl,m,v);
    update(2*node+1,l,r,m,rr,v);
    tree[node]=tree[2*node]+tree[2*node+1];
}

void update(int l,int r,int v){
    update(1,l,r,0,TREE_SIZE,v);
}

int get_first(int v){
    int l=beg-1,r=TREE_SIZE;
    while(l<r-1){
        int m=(l+r)/2;
        if(get_sum(m,m+1)<v)
            l=m;
        else
            r=m;
    }
    return r;
}

int get_last(int v){ // get one after last
    int l=beg-1,r=TREE_SIZE;
    while(l<r-1){
        int m=(l+r)/2;
        if(get_sum(m,m+1)<=v)
            l=m;
        else
            r=m;
    }
    return r;
}

void add(){
    beg--;
}

ll get(int k){
    if(k==0)
        return 0;
    int mv=(int)get_sum(beg+k-1,beg+k);
    int i1=get_first(mv);
    ll res=get_sum(beg,i1);
    update(beg,i1,1);
    k-=i1-beg;
    int i2=get_last(mv);
    res+=get_sum(i2-k,i2);
    update(i2-k,i2,1);
    return res;
}

void solve(){
    int n;
    cin>>n;
    vector<pair<int,int>>arr(n);
    for(auto&i:arr)
        cin>>i.first>>i.second;
    sort(arr.begin(),arr.end());
    int ph=0;
    multiset<int>s;
    ll r=0;
    
    for(auto [h,k]:arr){
        for(int i=ph;i<h;i++)
            add();
        r+=get(k);
        ph=h;
    }
    cout<<r<<"\n";
}

int main(){
    ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
    //freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
    int t=1;//cin>>t;
    while(t--)solve();
    return 0;
}

/*

6
3 2
5 3
4 1
2 1
4 3
3 2

10
 
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 604 KB Output is correct
2 Correct 12 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 1424 KB Output is correct
2 Correct 178 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 1824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 2356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 636 ms 4208 KB Output is correct
2 Correct 606 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 672 ms 4332 KB Output is correct
2 Correct 570 ms 4692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 820 ms 4396 KB Output is correct
2 Correct 676 ms 3180 KB Output is correct