답안 #282202

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
282202 2020-08-24T06:30:11 Z 문홍윤(#5760) 방벽 (JOI15_walls) C++17
100 / 100
274 ms 17748 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];
        }
        for(int t=1; t<=3; t++){
            if(!nx[fr])break;
            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);
      |         ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 664 KB Output is correct
2 Correct 65 ms 6632 KB Output is correct
3 Correct 75 ms 7272 KB Output is correct
4 Correct 71 ms 7144 KB Output is correct
5 Correct 76 ms 7272 KB Output is correct
6 Correct 55 ms 7148 KB Output is correct
7 Correct 29 ms 384 KB Output is correct
8 Correct 36 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 2160 KB Output is correct
2 Correct 97 ms 7284 KB Output is correct
3 Correct 128 ms 11612 KB Output is correct
4 Correct 251 ms 17620 KB Output is correct
5 Correct 228 ms 15572 KB Output is correct
6 Correct 248 ms 17620 KB Output is correct
7 Correct 229 ms 15576 KB Output is correct
8 Correct 252 ms 17620 KB Output is correct
9 Correct 127 ms 9692 KB Output is correct
10 Correct 203 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 664 KB Output is correct
2 Correct 65 ms 6632 KB Output is correct
3 Correct 75 ms 7272 KB Output is correct
4 Correct 71 ms 7144 KB Output is correct
5 Correct 76 ms 7272 KB Output is correct
6 Correct 55 ms 7148 KB Output is correct
7 Correct 29 ms 384 KB Output is correct
8 Correct 36 ms 384 KB Output is correct
9 Correct 19 ms 2160 KB Output is correct
10 Correct 97 ms 7284 KB Output is correct
11 Correct 128 ms 11612 KB Output is correct
12 Correct 251 ms 17620 KB Output is correct
13 Correct 228 ms 15572 KB Output is correct
14 Correct 248 ms 17620 KB Output is correct
15 Correct 229 ms 15576 KB Output is correct
16 Correct 252 ms 17620 KB Output is correct
17 Correct 127 ms 9692 KB Output is correct
18 Correct 203 ms 17236 KB Output is correct
19 Correct 245 ms 15656 KB Output is correct
20 Correct 268 ms 17748 KB Output is correct
21 Correct 238 ms 15700 KB Output is correct
22 Correct 256 ms 16984 KB Output is correct
23 Correct 247 ms 15700 KB Output is correct
24 Correct 274 ms 17748 KB Output is correct
25 Correct 154 ms 9820 KB Output is correct
26 Correct 155 ms 10460 KB Output is correct
27 Correct 227 ms 17200 KB Output is correct