답안 #127943

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
127943 2019-07-10T08:51:45 Z RockyB Worst Reporter 3 (JOI18_worst_reporter3) C++17
12 / 100
2000 ms 96992 KB
#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

#define rep(a, b, c) for (int a = (b); (a) <= (c); ++a)
#define per(a, b, c) for (int a = (b); (a) >= (c); --a) 

#define nl '\n'
#define ioi exit(0);

using namespace std;

typedef long long ll;

const int MAX_N = (int)5e5 + 7;

int n, q;
int d[MAX_N], l[MAX_N], r[MAX_N], t[MAX_N], ans[MAX_N];

map <int, vector <int> > off;

vector <int> d1;

struct fenwick {
  pair <ll, int> f[MAX_N];

  void upd(int p, ll df, int t) {
    for (; p < MAX_N; p |= p + 1) {
      f[p].f += df;
      f[p].s += t;
    }
  }
  pair <ll, int> get(int p) {
    pair <ll, int> res = {0, 0};
    for (; p >= 0; p = (p & (p + 1)) - 1) {
      res.f += f[p].f;
      res.s += f[p].s;
    }
    return res;
  }
} F; 

pair <ll, int> pref[MAX_N];

int S = -1, E = -1;

pair <int, int> inter(pair <int, int> x, pair <int, int> y) {
  return {max(x.f, y.f), min(x.s, y.s)};
}
ll get_val(int p, int T) {
  pair <ll, int> cur = F.get(p);
  int one = p - cur.s; 
  if (S != -1) {
    pair <int, int> rng = inter({1, p}, {S, E});
    if (rng.f <= rng.s) {
      // cerr << cur.f << ' ' << one << nl;
      one -= pref[rng.s].s - pref[rng.f - 1].s;
      cur.f += pref[rng.s].f - pref[rng.f - 1].f;
    }
  }


  return T - (cur.f + one);
}

void solve(int T, vector <int> &qr) {
  vector <int> a;
  a.pb(T);
  int add = T;

  vector < pair <int, ll > > del;
  S = -1, E = -1;
  int last = 1;
  int mx = 1;
  int sum = 0;
  rep(i, 1, n) {
    int df = (d[i] + last - 1) / last;
    //assert(df == (d[i] + d[i - 1] - 2) / d[i - 1]);
    //if (add == 1 && S == -1) S = i;
    
    if (S == -1) {
      F.upd(i, 1 + d[i - 1] * (add % df), 1);
      del.pb({i, 1 + d[i - 1] * (add % df)});
    }

    sum += 1 + last * (add % df);
    a.pb(T - sum);

    //cerr << add << ' ' << df << nl;
    // cerr << 1 + last * (add % df) << ' ' << last << nl;
    last *= df;
    add /= df;
    if (!add) {
      E = i;
      // break;
    }
  }
 /*  ioi
  S = -1;
  if (E == -1) E = n;
  cerr << nl;
  cerr << get_val(2, T);
  ioi
  rep(i, 1, n) {
    cerr << get_val(i, T) << ' ';
  } 
  ioi */

  reverse(all(a));

  for (auto it : qr) {
    /* int L = 0, R = n;
    int lo = 1, hi = n;
    while (lo <= hi) {
      int mid = lo + hi >> 1;
      if (get_val(mid, T) >= l[it]) L = mid, lo = mid + 1;
      else hi = mid - 1;
    }
    lo = 1, hi = n;
    while (lo <= hi) {
      int mid = lo + hi >> 1;
      if (get_val(mid, T) <= r[it]) R = mid, hi = mid - 1;
      else lo = mid + 1;
    }
    if (R <= L) ans[it] += L - R + 1;
    if (l[it] <= T && T <= r[it]) ans[it]++; */
    ans[it] = upper_bound(all(a), r[it]) - lower_bound(all(a), l[it]);
  }

  // cout << ans[2] << nl;
  for (auto it : del) {
    F.upd(it.f, -it.s, -1);
  }
}
int main() {
  #ifdef IOI
    freopen ("in.txt", "r", stdin);
  #endif
  ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  d[0] = 1;
  rep(i, 1, n) {
    cin >> d[i];
    if (d[i] > 1) d1.pb(i);

    int df = (d[i] + d[i - 1] - 1) / d[i - 1];
    pref[i] = pref[i - 1];
    if (df > 1) pref[i].f += 1 + d[i - 1], pref[i].s++;
  }
  rep(i, 1, q) {
    cin >> t[i] >> l[i] >> r[i];
    off[t[i]].pb(i);
  }

  /* solve(1000, off[1000]);
  ioi
 */
  for (auto it : off) {
    solve(it.f, it.s);  
  }

  rep(i, 1, q) {
    cout << ans[i] << nl;
  }

  ioi
}

Compilation message

worst_reporter3.cpp: In function 'void solve(int, std::vector<int>&)':
worst_reporter3.cpp:81:7: warning: unused variable 'mx' [-Wunused-variable]
   int mx = 1;
       ^~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2052 ms 96992 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 632 KB Output is correct
2 Correct 94 ms 564 KB Output is correct
3 Correct 96 ms 652 KB Output is correct
4 Correct 94 ms 760 KB Output is correct
5 Correct 98 ms 632 KB Output is correct
6 Correct 93 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2052 ms 96992 KB Time limit exceeded
2 Halted 0 ms 0 KB -