結果

問題 No.1008 Bench Craftsman
ユーザー LayCurseLayCurse
提出日時 2020-03-06 21:51:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 10,446 bytes
コンパイル時間 3,031 ms
コンパイル使用メモリ 226,308 KB
実行使用メモリ 20,880 KB
最終ジャッジ日時 2024-10-14 06:26:32
合計ジャッジ時間 6,966 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,620 KB
testcase_01 AC 3 ms
7,616 KB
testcase_02 AC 3 ms
9,664 KB
testcase_03 AC 3 ms
7,752 KB
testcase_04 AC 3 ms
7,620 KB
testcase_05 TLE -
testcase_06 AC 368 ms
14,692 KB
testcase_07 AC 3 ms
6,820 KB
testcase_08 AC 9 ms
6,816 KB
testcase_09 AC 198 ms
8,520 KB
testcase_10 AC 1,752 ms
14,700 KB
testcase_11 AC 1,517 ms
12,936 KB
testcase_12 AC 1,709 ms
15,128 KB
testcase_13 AC 1,426 ms
15,260 KB
testcase_14 AC 1,326 ms
15,260 KB
testcase_15 AC 725 ms
14,680 KB
testcase_16 AC 1,233 ms
14,316 KB
testcase_17 AC 1,425 ms
15,260 KB
testcase_18 AC 1,097 ms
14,700 KB
testcase_19 AC 1,200 ms
15,264 KB
testcase_20 AC 529 ms
10,620 KB
testcase_21 AC 625 ms
9,228 KB
testcase_22 AC 1,114 ms
9,904 KB
testcase_23 AC 1,449 ms
12,956 KB
testcase_24 AC 1,433 ms
10,652 KB
testcase_25 AC 469 ms
14,652 KB
testcase_26 AC 345 ms
9,116 KB
testcase_27 AC 1,069 ms
9,524 KB
testcase_28 AC 995 ms
12,852 KB
testcase_29 AC 1,568 ms
13,068 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'T segtree_ChangeP1add_Sum<T>::getSum(int, int) [with T = long long int]',
    inlined from 'int main()' at main.cpp:511:18:
main.cpp:442:11: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
  442 |       res = res + tmp;
      |       ~~~~^~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:404:7: note: 'tmp' was declared here
  404 |     T tmp;
      |       ^~~
In member function 'T segtree_ChangeP1add_Sum<T>::getSum(int, int) [with T = long long int]',
    inlined from 'int main()' at main.cpp:511:18:
main.cpp:442:11: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  442 |       res = res + tmp;
      |       ~~~~^~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:403:7: note: 'res' was declared here
  403 |     T res;
      |       ^~~

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class T> struct segtree_ChangeP1add_Sum{
  int N;
  int logN;
  T *sum;
  T *fixval;
  char *fixed;
  T *addval;
  T *p1addval;
  void malloc(int maxN, int once = 0){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    sum = new T[2*i];
    fixval = new T[i];
    addval = new T[i];
    p1addval = new T[i];
    fixed = new char[i];
    if(once){
      setN(maxN);
    }
  }
  void walloc(int maxN, int once = 0, void **mem = &wmem){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    walloc1d(&sum, 2*i, mem);
    walloc1d(&fixval, i, mem);
    walloc1d(&addval, i, mem);
    walloc1d(&p1addval, i, mem);
    walloc1d(&fixed, i, mem);
    if(once){
      setN(maxN);
    }
  }
  void free(void){
    delete [] sum;
    delete [] fixval;
    delete [] addval;
    delete [] p1addval;
    delete [] fixed;
  }
  T& operator[](int i){
    return sum[N+i];
  }
  void setN(int n, int zerofill = 1, int dobuild = 1){
    int i;
    for(i=1,logN=0;i<n;i*=2,logN++){
      ;
    }
    N = i;
    if(zerofill){
      for(i=(0);i<(N);i++){
        sum[N+i] = 0;
      }
    }
    if(dobuild){
      build();
    }
  }
  void build(void){
    int i;
    for(i=N-1;i;i--){
      sum[i] = sum[2*i] + sum[2*i+1];
    }
    int lQU550vz = N;
    for(i=(1);i<(lQU550vz);i++){
      fixed[i] = 0;
    }
    int HOAyP4rA = N;
    for(i=(1);i<(HOAyP4rA);i++){
      addval[i] = p1addval[i] = 0;
    }
  }
  inline void push_one(int a, int sz, int st){
    if(fixed[a]){
      if(sz > 1){
        fixed[a*2] = fixed[a*2+1] = 1;
        fixval[a*2] = fixval[a*2+1] = fixval[a];
        addval[a*2] = p1addval[a*2] = 0;
        addval[a*2+1] = p1addval[a*2+1] = 0;
        sum[a*2] = sum[a*2+1] = sz * fixval[a];
      }
      else{
        sum[a*2] = sum[a*2+1] = sz * fixval[a];
      }
      fixed[a] = 0;
    }
    if(addval[a] != 0 || p1addval[a] != 0){
      if(sz > 1){
        addval[a*2] += addval[a];
        addval[a*2+1] += addval[a] + sz * p1addval[a];
        p1addval[a*2] += p1addval[a];
        p1addval[a*2+1] += p1addval[a];
      }
      sum[a*2] += sz * addval[a] + (long long) sz * (sz-1) / 2 * p1addval[a];
      sum[a*2+1] += sz * (addval[a] + sz * p1addval[a]) + (long long) sz * (sz-1) / 2 * p1addval[a];
      addval[a] = p1addval[a] = 0;
    }
  }
  inline void push(int a){
    int i;
    int aa = a - N;
    int nd;
    int sz;
    int st;
    for(i=logN;i;i--){
      nd = a>>i;
      sz = 1<<(i-1);
      st = 2 * sz * (aa>>i);
      push_one(nd, sz, st);
    }
  }
  inline void build(int a){
    int sz = 1;
    int st = a - N;
    while(a > 1){
      if(a%2){
        st += sz;
      }
      a /= 2;
      sz *= 2;
      if(fixed[a]){
        sum[a] = sz * fixval[a];
      }
      else{
        sum[a] = sum[a*2] + sum[a*2+1];
      }
      if(addval[a] != 0 || p1addval[a] != 0){
        sum[a] += sz * addval[a] + (long long) sz * (sz-1) / 2 * p1addval[a];
      }
    }
  }
  inline void change(int a, int b, T val){
    int sz = 1;
    int aa;
    int bb;
    int st_a = a;
    int st_b = b;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    if(a%2){
      sum[a] = val;
      a++;
      st_a += sz;
    }
    if(b%2){
      b--;
      st_b -= sz;
      sum[b] = val;
    }
    a /= 2;
    b /= 2;
    while(a < b){
      sz *= 2;
      if(a%2){
        fixed[a]=1;
        fixval[a]=val;
        addval[a] = p1addval[a] = 0;
        sum[a] = sz * val;
        a++;
        st_a += sz;
      }
      if(b%2){
        b--;
        st_b -= sz;
        fixed[b]=1;
        fixval[b]=val;
        addval[b] = p1addval[b] = 0;
        sum[b] = sz * val;
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline void add(int a, int b, T val){
    int sz = 1;
    int aa;
    int bb;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    if(a%2){
      sum[a] += val;
      a++;
    }
    if(b%2){
      b--;
      sum[b] += val;
    }
    a /= 2;
    b /= 2;
    while(a < b){
      sz *= 2;
      if(a%2){
        addval[a] += val;
        sum[a] += sz * val;
        a++;
      }
      if(b%2){
        b--;
        addval[b] += val;
        sum[b] += sz * val;
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline void p1add(int a, int b, T x1, T x0){
    int sz = 1;
    int aa;
    int bb;
    T xa;
    T xb;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    xa = x0;
    xb = x1 * (b-a) + x0;
    if(a%2){
      sum[a] += xa;
      a++;
      xa += sz * x1;
    }
    if(b%2){
      b--;
      xb -= sz * x1;
      sum[b] += xb;
    }
    a /= 2;
    b /= 2;
    while(a < b){
      sz *= 2;
      if(a%2){
        addval[a] += xa;
        p1addval[a] += x1;
        sum[a] += sz * xa + (long long) sz * (sz-1) / 2 * x1;
        a++;
        xa += sz * x1;
      }
      if(b%2){
        b--;
        xb -= sz * x1;
        addval[b] += xb;
        p1addval[b] += x1;
        sum[b] += sz * xb + (long long) sz * (sz-1) / 2 * x1;
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline T getSum(int a, int b){
    T res;
    T tmp;
    int fga = 0;
    int fgb = 0;
    a += N;
    b += N;
    push(a);
    push(b-1);
    while(a < b){
      if(a%2){
        if(fga){
          res = res + sum[a];
        }
        else{
          res = sum[a];
          fga = 1;
        }
        a++;
      }
      if(b%2){
        b--;
        if(fgb){
          tmp = sum[b] + tmp;
        }
        else{
          tmp = sum[b];
          fgb = 1;
        }
      }
      a /= 2;
      b /= 2;
    }
    if(fga==1 && fgb==0){
      return res;
    }
    if(fga==0 && fgb==1){
      return tmp;
    }
    if(fga==1 && fgb==1){
      res = res + tmp;
      return res;
    }
    return res;
  }
}
;
int N;
int M;
int X[100000];
long long A[100000];
long long W[100000];
int main(){
  wmem = memarr;
  int res;
  int ok;
  int wid;
  int s;
  int len;
  segtree_ChangeP1add_Sum<long long> t;
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(M);e98WHCEY++){
      rd(X[e98WHCEY]);X[e98WHCEY] += (-1);
      rd(W[e98WHCEY]);
    }
  }
  t.walloc(N);
  t.setN(N);
  long long FmcKpFmN;
  long long xr20shxY;
  long long WYIGIcGE;
  FmcKpFmN = 0;
  xr20shxY = 1073709056;
  while(FmcKpFmN < xr20shxY){
    int i;
    if((FmcKpFmN + xr20shxY)%2==0){
      WYIGIcGE = (FmcKpFmN + xr20shxY) / 2;
    }
    else{
      WYIGIcGE = (FmcKpFmN + xr20shxY - 1) / 2;
    }
    for(i=(0);i<(N);i++){
      t[i] = 0;
    }
    t.build();
    for(i=(0);i<(M);i++){
      if(WYIGIcGE==0){
        t.add(0, N, W[i]);
        continue;
      }
      wid = W[i] / WYIGIcGE;
      s =max_L(0, X[i] - wid);
      len = X[i] - s;
      t.p1add(s, s+len, WYIGIcGE, W[i] - len*WYIGIcGE);
      s = X[i];
      len =min_L(N-1, X[i] + wid)- X[i] + 1;
      t.p1add(s, s+len, -WYIGIcGE, W[i]);
    }
    ok = 1;
    for(i=(0);i<(N);i++){
      if(t.getSum(i,i+1) >= A[i]){
        ok = 0;
        break;
      }
    }
    if(ok){
      xr20shxY = WYIGIcGE;
    }
    else{
      FmcKpFmN = WYIGIcGE + 1;
    }
  }
  res =xr20shxY;
  if(res==1073709056){
    res = -1;
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200303-1 [beta]

// --- original code ---
// int N, M, X[1d5];
// ll A[1d5], W[1d5];
// {
//   int res, ok, wid, s, len;
//   segtree_ChangeP1add_Sum<ll> t;
// 
//   rd(N,M,A(N),(X--,W)(M));
//   t.walloc(N);
//   t.setN(N);
// 
//   res = bsearch_min[ll,c,0,int_inf][
//     rep(i,N) t[i] = 0;
//     t.build();
// 
//     rep(i,M){
//       if(c==0){
//         t.add(0, N, W[i]);
//         continue;
//       }
// 
//       wid = W[i] / c;
//       
//       s = max(0, X[i] - wid);
//       len = X[i] - s;
//       t.p1add(s, s+len, c, W[i] - len*c);
// 
//       s = X[i];
//       len = min(N-1, X[i] + wid) - X[i] + 1;
//       t.p1add(s, s+len, -c, W[i]);
//     }
// 
//     ok = 1;
//     rep(i,N) if(t.getSum(i,i+1) >= A[i]) ok = 0, break;
//   ](ok);
// 
//   if(res==int_inf) res = -1;
//   wt(res);
// }
0