Submission #92559

# Submission time Handle Problem Language Result Execution time Memory
92559 2019-01-03T11:59:19 Z Retro3014 Sails (IOI07_sails) C++17
70 / 100
1000 ms 10848 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>

using namespace std;
#define MAX_N 100000
#define INF 100000
typedef long long ll;

struct S{
    ll sum;
    int min, max, lazy;
    int s, e;
    int l, r;
};
vector<S> seg;
int N;
struct M{
    int h, k;
    bool operator <(const M &a)const{
        return h<a.h;
    }
};
vector<M> m;

deque<int> dq;


void init(){
    dq.push_back(0);
    int x=0;
    while(!dq.empty()){
        x=dq.front(); dq.pop_front();
        if(seg[x].s!=seg[x].e){
            if(seg[x].l==-1){
                seg[x].l=(int)seg.size();
                seg.push_back({0, 0, 0, 0, seg[x].s, (seg[x].s+seg[x].e)/2, -1, -1});
                dq.push_back(seg[x].l);
            }
            if(seg[x].r==-1){
                seg[x].r=(int)seg.size();
                seg.push_back({0, 0, 0, 0, (seg[x].s+seg[x].e)/2+1, seg[x].e, -1, -1});
                dq.push_back(seg[x].r);
            }
        }
    }
}

void calc1(int idx){
    if(seg[idx].lazy>0){
        if(seg[idx].l!=-1){
            seg[seg[idx].l].max+=seg[idx].lazy; seg[seg[idx].l].min+=seg[idx].lazy; seg[seg[idx].l].lazy+=seg[idx].lazy;
            seg[seg[idx].l].sum+=(ll)(seg[seg[idx].l].e-seg[seg[idx].l].s+1) * seg[idx].lazy;
        }
        if(seg[idx].r!=-1){
            seg[seg[idx].r].max+=seg[idx].lazy; seg[seg[idx].r].min+=seg[idx].lazy; seg[seg[idx].r].lazy+=seg[idx].lazy;
            seg[seg[idx].r].sum+=(ll)(seg[seg[idx].r].e-seg[seg[idx].r].s+1) * seg[idx].lazy;
        }
        seg[idx].lazy=0;
    }
}

void update(int idx, int x, int y){
    calc1(idx);
    if(seg[idx].s>y || seg[idx].e<x)    return;
    if(seg[idx].s>=x && seg[idx].e<=y)  {
        seg[idx].lazy++; seg[idx].max++; seg[idx].min++; seg[idx].sum+=(ll)(seg[idx].e-seg[idx].s+1);    return;
    }
    update(seg[idx].l, x, y);
    update(seg[idx].r, x, y);
    seg[idx].max=max(seg[seg[idx].l].max, seg[seg[idx].r].max);
    seg[idx].min=min(seg[seg[idx].l].min, seg[seg[idx].r].min);
    seg[idx].sum=seg[seg[idx].l].sum+seg[seg[idx].r].sum;
}

int min(int idx, int x, int y){
    calc1(idx);
    if(seg[idx].s>y || seg[idx].e<x)    return INF;
    if(seg[idx].s>=x && seg[idx].e<=y)  return seg[idx].min;
    return min(min(seg[idx].l, x, y), min(seg[idx].r, x, y));
}

int max(int idx, int x, int y){
    calc1(idx);
    if(seg[idx].s>y || seg[idx].e<x)    return 0;
    if(seg[idx].s>=x && seg[idx].e<=y)  return seg[idx].max;
    return max(max(seg[idx].l, x, y), max(seg[idx].r, x, y));
}


ll sum(int idx, int x, int y){
    calc1(idx);
    if(seg[idx].s>y || seg[idx].e<x)    return 0;
    if(seg[idx].s>=x && seg[idx].e<=y)  return seg[idx].sum;
    return sum(seg[idx].l, x, y)+sum(seg[idx].r, x, y);
}

int get(int x){
    dq.push_back(0);
    int idx=0;
    while(!dq.empty()){
        idx=dq.front(); dq.pop_front();
        calc1(idx);
        if(seg[idx].s==seg[idx].e)  return seg[idx].max;
        if((seg[idx].s+seg[idx].e)/2>=x){
            dq.push_back(seg[idx].l);
        }else   dq.push_back(seg[idx].r);
    }
    return 0;
}

int find_l(int x, int y, int t){
    int s=x, e=y, mid;
    while(s<e){
        mid=(s+e)/2;
        if(max(0, mid, y)==t){
            e=mid;
        }else{
            s=mid+1;
        }
    }
    return s;
}
int find_r(int x, int y, int t){
    int s=x, e=y, mid;
    while(s<e){
        mid=(s+e)/2+1;
        if(min(0, x, mid)==t){
            s=mid;
        }else   e=mid-1;
    }
    return s;
}


int main(){
    //freopen("in.txt", "r", stdin);
    scanf("%d", &N);
    seg.push_back({0, 0, 0, 0, 1, 100000, -1, -1});
    init();
    for(int i=1; i<=N; i++){
        int a, b;
        scanf("%d%d", &a, &b);m.push_back({a, b});
    }sort(m.begin(), m.end());
    int l, r, s, e, t;
    ll ans=0;
    for(int i=0; i<N; i++){
        e=m[i].h; s=m[i].h-m[i].k+1;
        ans+=sum(0, s, e);
        t=get(s);
        l=find_l(1, s, t);
        r=find_r(s, e, t);
        //printf("%d %d %d %d %d\n", s, e, l, r, t);
        update(0, l, l+r-s);
        update(0, r+1, e);
        
    }
    printf("%lld\n", ans);
    return 0;
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
sails.cpp:144:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);m.push_back({a, b});
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 10848 KB Output is correct
2 Correct 15 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 10844 KB Output is correct
2 Correct 15 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 10848 KB Output is correct
2 Correct 15 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 10844 KB Output is correct
2 Correct 19 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 10848 KB Output is correct
2 Correct 36 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 10816 KB Output is correct
2 Correct 289 ms 10836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 765 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 10844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 10848 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 10848 KB Time limit exceeded
2 Halted 0 ms 0 KB -