Submission #282200

# Submission time Handle Problem Language Result Execution time Memory
282200 2020-08-24T06:28:25 Z 문홍윤(#5760) None (JOI15_walls) C++17
100 / 100
275 ms 23508 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define svec(x) sort(x.begin(), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, int> pli;

int n, m, re;
LL l[200010], r[200010], arr[200010]={-1}, ans[200010];
vector<pli> vc;

priority_queue<pair<LL, pii>, vector<pair<LL, pii> >, greater<pair<LL, pii> > > pq;
int pr[200010], nx[200010];
LL sum, cnt;
bool ch[200010];

void calc_pq(LL num){
    while(pq.size()){
        LL d=pq.top().F;
        int f=pq.top().S.F, r=pq.top().S.S;
        if(d>=num)return;
        pq.pop();
        if(ch[f]||ch[r])continue;
        int pf=pr[f], nr=nx[r];
        if(!pf){
            nx[0]=r;
            pr[r]=0;
            sum-=d;
            cnt--;
            ch[f]=true;
            continue;
        }
        if(!nr){
            nx[f]=0;
            cnt--;
            sum-=d;
            ch[r]=true;
            continue;
        }
        ch[f]=ch[r]=true;
        nx[pf]=nr;
        pr[nr]=pf;
        cnt-=2;
        sum-=llabs(arr[f]-arr[pf]);
        sum-=d;
        sum-=llabs(arr[nr]-arr[r]);
        sum+=llabs(arr[nr]-arr[pf]);
        pq.push(mp(llabs(arr[nr]-arr[pf]), mp(pf, nr)));
    }
}

void get_naive(int num){
    int nw=nx[0];
    LL nl=l[num], nr=r[num];
    while(nw){
        if(nr<=arr[nw]){
            ans[num]+=arr[nw]-nr;
            nl+=arr[nw]-nr;
            nr=arr[nw];
        }
        if(arr[nw]<=nl){
            ans[num]+=nl-arr[nw];
            nr-=nl-arr[nw];
            nl=arr[nw];
        }
        nw=nx[nw];
    }
}

int main(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=n; i++){
        scanf("%lld %lld\n", &l[i], &r[i]);
        vc.eb(r[i]-l[i], i);
    }
    svec(vc);
    for(int i=1; i<=m; i++){
        LL a;
        scanf("%lld", &a);
        if(arr[re]==a)continue;
        if(re<2){
            arr[++re]=a;
            continue;
        }
        if(arr[re]-arr[re-1]<0&&a-arr[re]<0){
            arr[re]=a;
            continue;
        }
        if(arr[re]-arr[re-1]>0&&a-arr[re]>0){
            arr[re]=a;
            continue;
        }
        arr[++re]=a;
    }
    m=re;
    nx[0]=1;
    for(int i=2; i<=m; i++){
        pq.push(mp(llabs(arr[i]-arr[i-1]), mp(i-1, i)));
        sum+=llabs(arr[i]-arr[i-1]);
        cnt++;
        nx[i-1]=i;
        pr[i]=i-1;
    }
    for(auto i:vc){
        if(cnt<=10){
            get_naive(i.S);
            continue;
        }
        calc_pq(i.F);
        ans[i.S]=sum-cnt*i.F;
        LL nl=l[i.S], nr=r[i.S];
        int fr=nx[0];
        if(nr<=arr[fr]){
            ans[i.S]+=arr[fr]-nr;
            nl+=arr[fr]-nr;
            nr=arr[fr];
        }
        if(arr[fr]<=nl){
            ans[i.S]+=nl-arr[fr];
            nr-=nl-arr[fr];
            nl=arr[fr];
        }
        if(cnt){
            ans[i.S]-=llabs(arr[fr]-arr[nx[fr]])-i.F;
            fr=nx[fr];
            if(nr<=arr[fr]){
                ans[i.S]+=arr[fr]-nr;
                nl+=arr[fr]-nr;
                nr=arr[fr];
            }
            if(arr[fr]<=nl){
                ans[i.S]+=nl-arr[fr];
                nr-=nl-arr[fr];
                nl=arr[fr];
            }
        }
    }
    for(int i=1; i<=n; i++)printf("%lld\n", ans[i]);
}

Compilation message

walls.cpp: In function 'int main()':
walls.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   75 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
walls.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   77 |         scanf("%lld %lld\n", &l[i], &r[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
walls.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   83 |         scanf("%lld", &a);
      |         ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 664 KB Output is correct
2 Correct 69 ms 6696 KB Output is correct
3 Correct 81 ms 7272 KB Output is correct
4 Correct 72 ms 7144 KB Output is correct
5 Correct 77 ms 7272 KB Output is correct
6 Correct 64 ms 7148 KB Output is correct
7 Correct 34 ms 1536 KB Output is correct
8 Correct 34 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2160 KB Output is correct
2 Correct 108 ms 8456 KB Output is correct
3 Correct 134 ms 14172 KB Output is correct
4 Correct 247 ms 21844 KB Output is correct
5 Correct 218 ms 19924 KB Output is correct
6 Correct 248 ms 21844 KB Output is correct
7 Correct 219 ms 19924 KB Output is correct
8 Correct 246 ms 21972 KB Output is correct
9 Correct 124 ms 13532 KB Output is correct
10 Correct 197 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 664 KB Output is correct
2 Correct 69 ms 6696 KB Output is correct
3 Correct 81 ms 7272 KB Output is correct
4 Correct 72 ms 7144 KB Output is correct
5 Correct 77 ms 7272 KB Output is correct
6 Correct 64 ms 7148 KB Output is correct
7 Correct 34 ms 1536 KB Output is correct
8 Correct 34 ms 1528 KB Output is correct
9 Correct 19 ms 2160 KB Output is correct
10 Correct 108 ms 8456 KB Output is correct
11 Correct 134 ms 14172 KB Output is correct
12 Correct 247 ms 21844 KB Output is correct
13 Correct 218 ms 19924 KB Output is correct
14 Correct 248 ms 21844 KB Output is correct
15 Correct 219 ms 19924 KB Output is correct
16 Correct 246 ms 21972 KB Output is correct
17 Correct 124 ms 13532 KB Output is correct
18 Correct 197 ms 21084 KB Output is correct
19 Correct 239 ms 21588 KB Output is correct
20 Correct 270 ms 23508 KB Output is correct
21 Correct 236 ms 21460 KB Output is correct
22 Correct 256 ms 22420 KB Output is correct
23 Correct 240 ms 21332 KB Output is correct
24 Correct 275 ms 23508 KB Output is correct
25 Correct 159 ms 15708 KB Output is correct
26 Correct 166 ms 16348 KB Output is correct
27 Correct 225 ms 22996 KB Output is correct