萌新求助
查看原帖
萌新求助
143744
elimva楼主2021/11/16 11:14

我一直 TLE,但是自己随的数据跑得都比题解快,请问洛谷 TLE 还有什么其他的原因吗,还是我哪里 sb 了 /kk

#include <bits/stdc++.h>

using namespace std;

#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)

using pii = pair <int, int>;

const int INF = 0x3f3f3f3f;

const int N = 1000000 + 10;

namespace io {
	const int __SIZE = (1 << 21) + 1;
	char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
	#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
	inline void gc (char &x) { x = Gc(); }
	inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
	inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
	inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
		for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) *s = __c; *s = 0; }
	template <class I> inline bool gi (I &x) { _eof = 0;
		for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
		for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
	template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;

int n, m, t[N], askl[N], askr[N];

int lgn[N], st[N][20];
void init() {
  rep(i, 2, n) lgn[i] = lgn[i >> 1] + 1;
  rep(i, 1, n) st[i][0] = t[i];
  for (int j = 1; (1 << j) <= n; ++ j) {
    rep(i, 1, n - (1 << j) + 1) st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
  }
}
int query(int l, int r) {
  int o = lgn[r - l + 1];
  return min(st[l][o], st[r - (1 << o) + 1][o]);
}

int len[N], sz[N];
vector <int> tag[N];
vector <pii> id[N];

int c[N];
int lowbit(int x) { return x & (-x); }
int add(int p, int k) { for (; p; p -= lowbit(p)) c[p] += k; }
int ask(int p) { int res = 0; for (; p <= n; p += lowbit(p)) res += c[p]; return res; }

int main() {
  gi(n), gi(m);
  rep(i, 1, n) t[i] = INF, len[i] = -1;
  rep(i, 1, m) {
    int op; gi(op);
    if (op == 1) {
      int x; gi(x);
      if (t[x] == INF) t[x] = i;
    }
    else gi(askl[i]), gi(askr[i]);
  }
  init();
  rep(d, 1, n) {
    for (int i = 0, l = 1, r = d; l <= n; ++ i, l += d, r += d) {
      int mint = query(l, min(n, r));
      if (mint < INF) id[mint].push_back(make_pair(d, i)), sz[d] = i;
    }
  }
  rep(i, 1, n) tag[i].resize(sz[i] + 1);
  rep(i, 1, m) {
    if (askl[i]) {
      int l = askl[i], r = askr[i];
      print(ask(l) - ask(r + 1) + r - l + 1), pc('\n');
    }
    else {
      for (pii p : id[i]) {
        int d = p.first; tag[d][p.second] = true;
        for (; len[d] < sz[d] && tag[d][len[d] + 1]; ++ len[d]) add(d, 1);
      }
    }
  }
  return 0;
}
2021/11/16 11:14
加载中...