QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163064#7103. Red Black Treehos_lyricAC ✓386ms32708kbC++148.8kb2023-09-03 20:00:312023-09-03 20:00:32

Judging History

你现在查看的是最新测评结果

  • [2023-09-03 20:00:32]
  • 评测
  • 测评结果:AC
  • 用时:386ms
  • 内存:32708kb
  • [2023-09-03 20:00:31]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

// fast IO by yosupo
// sc.read(string &) appends the input
struct Scanner {
    FILE* fp = nullptr;
    char line[(1 << 15) + 1];
    size_t st = 0, ed = 0;
    void reread() {
        memmove(line, line + st, ed - st);
        ed -= st;
        st = 0;
        ed += fread(line + ed, 1, (1 << 15) - ed, fp);
        line[ed] = '\0';
    }
    bool succ() {
        while (true) {
            if (st == ed) {
                reread();
                if (st == ed) return false;
            }
            while (st != ed && isspace(line[st])) st++;
            if (st != ed) break;
        }
        if (ed - st <= 50) reread();
        return true;
    }
    template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        while (true) {
            size_t sz = 0;
            while (st + sz < ed && !isspace(line[st + sz])) sz++;
            ref.append(line + st, sz);
            st += sz;
            if (!sz || st != ed) break;
            reread();            
        }
        return true;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        bool neg = false;
        if (line[st] == '-') {
            neg = true;
            st++;
        }
        ref = T(0);
        while (isdigit(line[st])) {
            ref = 10 * ref + (line[st++] - '0');
        }
        if (neg) ref = -ref;
        return true;
    }
    template <class T> bool read_single(vector<T>& ref) {
        for (auto& d : ref) {
            if (!read_single(d)) return false;
        }
        return true;
    }
    void read() {}
    template <class H, class... T> void read(H& h, T&... t) {
        bool f = read_single(h);
        assert(f);
        read(t...);
    }
    Scanner(FILE* _fp) : fp(_fp) {}
};

struct Printer {
  public:
    template <bool F = false> void write() {}
    template <bool F = false, class H, class... T>
    void write(const H& h, const T&... t) {
        if (F) write_single(' ');
        write_single(h);
        write<true>(t...);
    }
    template <class... T> void writeln(const T&... t) {
        write(t...);
        write_single('\n');
    }
    void flush() {
        fwrite(line, 1, pos, fp);
        pos = 0;
    }

    Printer(FILE* _fp) : fp(_fp) {}
    ~Printer() { flush(); }

  private:
    static constexpr size_t SIZE = 1 << 15;
    FILE* fp;
    char line[SIZE], small[50];
    size_t pos = 0;
    void write_single(const char& val) {
        if (pos == SIZE) flush();
        line[pos++] = val;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void write_single(T val) {
        if (pos > (1 << 15) - 50) flush();
        if (val == 0) {
            write_single('0');
            return;
        }
        if (val < 0) {
            write_single('-');
            val = -val;  // todo min
        }
        size_t len = 0;
        while (val) {
            small[len++] = char('0' + (val % 10));
            val /= 10;
        }
        for (size_t i = 0; i < len; i++) {
            line[pos + i] = small[len - 1 - i];
        }
        pos += len;
    }
    void write_single(const string& s) {
        for (char c : s) write_single(c);
    }
    void write_single(const char* s) {
        size_t len = strlen(s);
        for (size_t i = 0; i < len; i++) write_single(s[i]);
    }
    template <class T> void write_single(const vector<T>& val) {
        auto n = val.size();
        for (size_t i = 0; i < n; i++) {
            if (i) write_single(' ');
            write_single(val[i]);
        }
    }
    void write_single(long double d){
        {
            long long v=d;
            write_single(v);
            d-=v;
        }
        write_single('.');
        for(int _=0;_<8;_++){
            d*=10;
            long long v=d;
            write_single(v);
            d-=v;
        }
    }
};

Scanner sc(stdin);
Printer pr(stdout);


constexpr Int INF = 1001001001001001001LL;

int N, M, Q;
vector<int> R;
vector<int> A, B;
vector<Int> C;

vector<vector<int>> G;
vector<int> dep, poss;
vector<int> seq;

vector<int> on;
vector<int> rs;
vector<Int> cs;
void dfs(int r, int u, int p) {
  dep[u] = ~p ? (dep[p] + 1) : 0;
  poss[u] = seq.size();
  seq.push_back(u);
  if (on[u]) {
    r = u;
    cs[u] = 0;
  }
  rs[u] = r;
  for (const int i : G[u]) {
    const int v = A[i] ^ B[i] ^ u;
    if (p != v) {
      cs[v] = cs[u] + C[i];
      dfs(r, v, u);
      seq.push_back(u);
    }
  }
}

// [bsr(2 N - 1) + 1][2 N - 1]
int E;
int mns[18][200'010];
int shallower(int u, int v) {
  return (dep[u] <= dep[v]) ? u : v;
}
int lca(int u, int v) {
  int j0 = poss[u], j1 = poss[v];
  if (j0 > j1) swap(j0, j1);
  ++j1;
  const int e = 31 - __builtin_clz(j1 - j0);
  return shallower(mns[e][j0], mns[e][j1 - (1 << e)]);
}


int main() {
  // for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
  int numCases;
  sc.read(numCases);
  for (; numCases--; ) {
    sc.read(N, M, Q);
    R.resize(M);
    for (int m = 0; m < M; ++m) {
      sc.read(R[m]);
      --R[m];
    }
    A.resize(N - 1);
    B.resize(N - 1);
    C.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      sc.read(A[i], B[i], C[i]);
      --A[i];
      --B[i];
    }
    
    G.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      G[A[i]].push_back(i);
      G[B[i]].push_back(i);
    }
    dep.resize(N);
    poss.resize(N);
    seq.clear();
    
    on.assign(N, 0);
    for (const int r : R) {
      on[r] = 1;
    }
    rs.assign(N, -1);
    cs.assign(N, 0);
    dfs(0, 0, -1);
// cerr<<"rs = "<<rs<<endl;
// cerr<<"cs = "<<cs<<endl;
    
    E = (31 - __builtin_clz(2 * N - 1)) + 1;
    for (int j = 0; j < 2 * N - 1; ++j) {
      mns[0][j] = seq[j];
    }
    for (int e = 0; e < E - 1; ++e) {
      for (int i = 0; i + (1 << (e + 1)) <= 2 * N - 1; ++i) {
        mns[e + 1][i] = shallower(mns[e][i], mns[e][i + (1 << e)]);
      }
    }
    
    for (; Q--; ) {
      int K;
      sc.read(K);
      vector<int> V(K);
      for (int k = 0; k < K; ++k) {
        sc.read(V[k]);
        --V[k];
      }
      
      sort(V.begin(), V.end(), [&](int u, int v) -> bool {
        return ((rs[u] != rs[v]) ? (rs[u] < rs[v]) : (cs[u] < cs[v]));
      });
      vector<pair<Int, Int>> ps;
      for (int k = 0, l = 0; k < K; k = l) {
        for (l = k; l < K && rs[V[k]] == rs[V[l]]; ++l) {}
        const Int cost0 = cs[V[l - 1]];
        Int cost1 = cost0;
        int w = -1;
        for (int h = l; --h >= k; ) {
          w = (~w) ? lca(w, V[h]) : V[h];
          Int tmp = cost0 - cs[w];
          if (k < h) {
            chmax(tmp, cs[V[h - 1]]);
          }
          chmin(cost1, tmp);
        }
        ps.emplace_back(cost0, cost1);
      }
// cerr<<"ps = "<<ps<<endl;
      const int psLen = ps.size();
      vector<Int> pre(psLen + 1, -INF), suf(psLen + 1, -INF);
      for (int j = 0; j < psLen; ++j) pre[j + 1] = max(pre[j], ps[j].first);
      for (int j = psLen; --j >= 0; ) suf[j] = max(ps[j].first, suf[j + 1]);
      Int ans = INF;
      for (int j = 0; j < psLen; ++j) {
        Int cost = -INF;
        chmax(cost, pre[j]);
        chmax(cost, ps[j].second);
        chmax(cost, suf[j + 1]);
        chmin(ans, cost);
      }
      printf("%lld\n", ans);
    }
  }
// #ifndef LOCAL
  // break;
// #endif
  // }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 6108kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 386ms
memory: 32708kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed