Submission #44913915


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using ui = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdi = pair<double, int>;
using ull = unsigned long long;

#define ppc(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)

bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnd(20060130);
int rd(int l, int r) {return rnd() % (r - l + 1) + l;}

constexpr int mod = 1e9 + 7;
void addt(int &x, int y) {x += y, x >= mod && (x -= mod);}
int add(int x, int y) {return x += y, x >= mod && (x -= mod), x;}
int ksm(int a, int b) {
  int s = 1;
  while(b) {
    if(b & 1) s = 1ll * s * a % mod;
    a = 1ll * a * a % mod, b >>= 1;
  }
  return s;
}

char buf[1 << 20], *p1 = buf, *p2 = buf;
#define getc() (p1 == p2 && (p2 = (p1 = buf) + \
  fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline int read() {
  int x = 0;
  char s = getc();
  while(!isdigit(s)) s = getc();
  while(isdigit(s)) x = x * 10 + s - '0', s = getc();
  return x;
}

#define putc(x) putchar(x)
inline void print(ui x) {
  if(x >= 10) print(x / 10);
  putc(x % 10 + '0');
}

// ---------- templates above ----------

constexpr int N = 2e5 + 5;
constexpr int inf = 2e9 + 7;


int n, m, st;
struct pt {
  int a, b, id;
  bool operator < (const pt &z) {
    return b < z.b;
  }
} p[N];

pii operator + (const pii &x, const int &z) {
  return {x.first + z, x.second};
}
struct dat {
  pii b, ab;
  dat operator + (const dat &z) const {
    return {min(b, z.b), min(ab, z.ab)};
  }
  dat operator + (const int &z) const {
    return {b, min(ab, b + z)};
  }
} val[N << 2];
int laz[N << 2];

void tag(int x, int v) {laz[x] = min(laz[x], v), val[x] = val[x] + v;}
void down(int x) {if(laz[x] < inf) tag(x << 1, laz[x]), tag(x << 1 | 1, laz[x]), laz[x] = inf;}
void build(int l, int r, int x) {
  laz[x] = inf;
  if(l == r) {
    val[x].b = {p[l].b, l};
    val[x].ab = {l == st ? 0 : inf, l};
    return;
  }
  int m = l + r >> 1;
  build(l, m, x << 1);
  build(m + 1, r, x << 1 | 1);
  val[x] = val[x << 1] + val[x << 1 | 1];
}
void modify(int l, int r, int ql, int qr, int x, int v) {
  if(ql <= l && r <= qr) return tag(x, v);
  int m = l + r >> 1;
  down(x);
  if(ql <= m) modify(l, m, ql, qr, x << 1, v);
  if(m < qr) modify(m + 1, r, ql, qr, x << 1 | 1, v);
  val[x] = val[x << 1] + val[x << 1 | 1];
}
void update(int l, int r, int p, int x) {
  if(l == r) {
    val[x].b = val[x].ab = {inf, p};
    return;
  }
  int m = l + r >> 1;
  down(x);
  if(p <= m) update(l, m, p, x << 1);
  else update(m + 1, r, p, x << 1 | 1);
  val[x] = val[x << 1] + val[x << 1 | 1];
}

void mian() {
  cin >> n >> m;
  for(int i = 1; i <= n; i++) cin >> p[i].a, p[i].id = i;
  for(int i = 1; i <= n; i++) cin >> p[i].b;
  sort(p + 1, p + n + 1);
  for(int i = 1; i <= n; i++) if(p[i].id == 1) st = i;
  build(1, n, 1);
  for(int i = 1; i <= n; i++) {
    pii res = val[1].ab;
    int id = res.second;
    if(p[id].id == n) cout << res.first << "\n", exit(0);
    int pos = lower_bound(p + 1, p + n + 1, (pt) {0, m - p[id].a}) - p;
    update(1, n, id, 1);
    if(pos <= n) modify(1, n, pos, n, 1, res.first + p[id].a - m);
    if(pos > 1) modify(1, n, 1, pos - 1, 1, res.first + p[id].a);
  }
}


bool Med;
signed main() {
  // fprintf(stderr, "%.3lf MB\n", (&Mbe - &Med) / 1048576.0);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  #ifdef ALEX_WEI
    FILE* IN = freopen("1.in", "r", stdin); 
    FILE* OUT = freopen("1.out", "w", stdout);
  #endif
  int T = 1;
  while(T--) mian();
  // fprintf(stderr, "%d ms\n", int(1e3 * clock() / CLOCKS_PER_SEC));
  return 0;
}

/*
g++ a.cpp -o a -std=c++14 -O2 -DALEX_WEI
*/

Submission Info

Submission Time
Task G - Modulo Shortest Path
User Alex_Wei
Language C++ (GCC 9.2.1)
Score 600
Code Size 3841 Byte
Status AC
Exec Time 345 ms
Memory 28780 KiB

Compile Error

./Main.cpp: In function ‘void build(long long int, long long int, long long int)’:
./Main.cpp:85:13: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
   85 |   int m = l + r >> 1;
      |           ~~^~~
./Main.cpp: In function ‘void modify(long long int, long long int, long long int, long long int, long long int, long long int)’:
./Main.cpp:92:13: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
   92 |   int m = l + r >> 1;
      |           ~~^~~
./Main.cpp: In function ‘void update(long long int, long long int, long long int, long long int)’:
./Main.cpp:103:13: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
  103 |   int m = l + r >> 1;
      |           ~~^~~
./Main.cpp: In function ‘void mian()’:
./Main.cpp:121:65: warning: missing initializer for member ‘pt::id’ [-Wmissing-field-initializers]
  121 |     int pos = lower_bound(p + 1, p + n + 1, (pt) {0, m - p[id].a}) - p;
      |                                                                 ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 74
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, 064.txt, 065.txt, 066.txt, 067.txt, 068.txt, 069.txt, 070.txt, 071.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 6 ms 3512 KiB
001.txt AC 79 ms 28720 KiB
002.txt AC 2 ms 3604 KiB
003.txt AC 182 ms 28676 KiB
004.txt AC 182 ms 28704 KiB
005.txt AC 196 ms 28720 KiB
006.txt AC 302 ms 28736 KiB
007.txt AC 303 ms 28684 KiB
008.txt AC 307 ms 28676 KiB
009.txt AC 304 ms 28680 KiB
010.txt AC 301 ms 28724 KiB
011.txt AC 78 ms 28744 KiB
012.txt AC 77 ms 28616 KiB
013.txt AC 77 ms 28708 KiB
014.txt AC 78 ms 28676 KiB
015.txt AC 79 ms 28676 KiB
016.txt AC 88 ms 28680 KiB
017.txt AC 162 ms 28744 KiB
018.txt AC 171 ms 28616 KiB
019.txt AC 193 ms 28740 KiB
020.txt AC 172 ms 28680 KiB
021.txt AC 181 ms 28780 KiB
022.txt AC 177 ms 28736 KiB
023.txt AC 183 ms 28712 KiB
024.txt AC 256 ms 28704 KiB
025.txt AC 86 ms 28716 KiB
026.txt AC 135 ms 28724 KiB
027.txt AC 243 ms 28708 KiB
028.txt AC 195 ms 28648 KiB
029.txt AC 187 ms 28708 KiB
030.txt AC 28 ms 6504 KiB
031.txt AC 117 ms 16620 KiB
032.txt AC 8 ms 3928 KiB
033.txt AC 69 ms 15612 KiB
034.txt AC 23 ms 9920 KiB
035.txt AC 7 ms 3988 KiB
036.txt AC 166 ms 28316 KiB
037.txt AC 5 ms 3868 KiB
038.txt AC 248 ms 28720 KiB
039.txt AC 304 ms 28680 KiB
040.txt AC 253 ms 28776 KiB
041.txt AC 345 ms 28740 KiB
042.txt AC 272 ms 28776 KiB
043.txt AC 138 ms 28680 KiB
044.txt AC 92 ms 28720 KiB
045.txt AC 233 ms 28676 KiB
046.txt AC 4 ms 3508 KiB
047.txt AC 2 ms 3512 KiB
048.txt AC 2 ms 3508 KiB
049.txt AC 2 ms 3612 KiB
050.txt AC 2 ms 3540 KiB
051.txt AC 2 ms 3576 KiB
052.txt AC 2 ms 3512 KiB
053.txt AC 2 ms 3584 KiB
054.txt AC 3 ms 3556 KiB
055.txt AC 2 ms 3532 KiB
056.txt AC 1 ms 3524 KiB
057.txt AC 3 ms 3580 KiB
058.txt AC 3 ms 3528 KiB
059.txt AC 2 ms 3592 KiB
060.txt AC 3 ms 3560 KiB
061.txt AC 203 ms 28684 KiB
062.txt AC 101 ms 28716 KiB
063.txt AC 204 ms 28780 KiB
064.txt AC 76 ms 28740 KiB
065.txt AC 111 ms 28676 KiB
066.txt AC 92 ms 28712 KiB
067.txt AC 88 ms 28720 KiB
068.txt AC 165 ms 28776 KiB
069.txt AC 133 ms 28652 KiB
070.txt AC 61 ms 28684 KiB
071.txt AC 67 ms 28620 KiB
example0.txt AC 2 ms 3612 KiB
example1.txt AC 3 ms 3612 KiB