Submission #2697162


Source Code Expand

#include <cstdio>
#include <algorithm>
#include <set>

using namespace std;

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define DUMP(a, n) REP(i, n) printf("%d%c", a[i], i + 1 == n ? '\n' : ' ')
#define ITR(c,it)  for(auto it = c.begin(); it != c.end(); it++)

typedef long long ll;

const int N_MAX = 100000;

int N;
int l[N_MAX];
int r[N_MAX];
int w[N_MAX];

multiset<ll> ls, rs;
ll lf, rf;
ll ans;

void dumpDP(int i) {
  printf("i: %d\n", i);
  printf("  ls: {"); ITR(ls, it) if (*it + lf >= 0) printf("%lld, ", *it + lf); printf("}\n");
  printf("  rs: {"); ITR(rs, it) if (*it + rf <= 1e9)printf("%lld, ", *it + rf); printf("}\n");
}

void erase_one(multiset<ll> &st, ll v) {
  auto it = st.find(v);
  if (it != st.end()) st.erase(it);
}

void solve() {
  REP(i, N) w[i] = r[i] - l[i];
  ls.insert(l[0]); rs.insert(l[0]);
  ans = 0;
  lf = rf = 0;
  FOR(i, 1, N - 1) {
    // dumpDP(i);
    lf -= w[i]; rf += w[i - 1];
    ll le = *ls.rbegin() + lf;
    ll re = *rs.begin() + rf;
    ll l3 = l[i];
    // printf("  e: (%lld, %lld), l3: %lld\n", le, re, l3);
    if (l3 > re) {
      erase_one(rs, re - rf);
      ls.insert(re - lf);
      rs.insert(l3 - rf);
      rs.insert(l3 - rf);
      ans += l3 - re;
      // printf("  rs->ls: %lld, rs << %lld, ans += %lld\n", re, l3, l3 - re);
    } else if (l3 < le) {
      erase_one(ls, le - lf);
      rs.insert(le - rf);
      ls.insert(l3 - lf);
      ls.insert(l3 - lf);
      ans += le - l3;
      // printf("  ls->rs: %lld, ls << %lld, ans += %lld\n", le, l3, le - l3);
    } else {
      ls.insert(l3 - lf);
      rs.insert(l3 - rf);
      // printf("  ls << %lld rs << %lld, ans += 0\n", l3, l3);
    }
  }

  // dumpDP(N);

  printf("%lld\n", ans);
}

void input() {
  scanf("%d", &N);
  REP(i, N) {
    scanf("%d%d", l + i, r + i);
  }
}

int main() {
  input();
  solve();
  return 0;
}

Submission Info

Submission Time
Task E - NarrowRectangles
User nejiko96
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 1907 Byte
Status AC
Exec Time 98 ms
Memory 10752 KB

Compile Error

./Main.cpp: In function ‘void input()’:
./Main.cpp:75:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &N);
                  ^
./Main.cpp:77:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", l + i, r + i);
                                ^

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 300 / 300 700 / 700
Status
AC × 5
AC × 13
AC × 37
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt
Subtask 0_000, 0_001, 0_004, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 2_018.txt, 2_019.txt, 2_020.txt, 2_021.txt, 2_022.txt, 2_023.txt, 2_024.txt, 2_025.txt, 2_026.txt, 2_027.txt, 2_028.txt, 2_029.txt, 2_030.txt, 2_031.txt, 2_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt
Case Name Status Exec Time Memory
0_000.txt AC 1 ms 256 KB
0_001.txt AC 1 ms 256 KB
0_002.txt AC 1 ms 256 KB
0_003.txt AC 1 ms 256 KB
0_004.txt AC 1 ms 256 KB
1_005.txt AC 1 ms 256 KB
1_006.txt AC 1 ms 256 KB
1_007.txt AC 1 ms 256 KB
1_008.txt AC 1 ms 256 KB
1_009.txt AC 1 ms 256 KB
1_010.txt AC 1 ms 256 KB
1_011.txt AC 1 ms 256 KB
1_012.txt AC 1 ms 256 KB
1_013.txt AC 1 ms 256 KB
1_014.txt AC 1 ms 256 KB
1_015.txt AC 1 ms 256 KB
1_016.txt AC 1 ms 256 KB
1_017.txt AC 1 ms 256 KB
2_018.txt AC 98 ms 10752 KB
2_019.txt AC 85 ms 10752 KB
2_020.txt AC 74 ms 10752 KB
2_021.txt AC 53 ms 10752 KB
2_022.txt AC 55 ms 10752 KB
2_023.txt AC 52 ms 10752 KB
2_024.txt AC 53 ms 10752 KB
2_025.txt AC 52 ms 10752 KB
2_026.txt AC 52 ms 10752 KB
2_027.txt AC 53 ms 10752 KB
2_028.txt AC 52 ms 10752 KB
2_029.txt AC 53 ms 10752 KB
2_030.txt AC 53 ms 10752 KB
2_031.txt AC 89 ms 10752 KB
2_032.txt AC 90 ms 10752 KB
2_033.txt AC 89 ms 10752 KB
2_034.txt AC 88 ms 10752 KB
2_035.txt AC 60 ms 10752 KB
2_036.txt AC 77 ms 10752 KB