QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#330964#8235. Top Clusterucup-team2000WA 866ms170200kbC++144.4kb2024-02-17 21:26:162024-02-17 21:26:16

Judging History

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

  • [2024-02-17 21:26:16]
  • 评测
  • 测评结果:WA
  • 用时:866ms
  • 内存:170200kb
  • [2024-02-17 21:26:16]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<class T>
struct RMQ {
	vector<vector<T>> jmp;
	RMQ(const vector<T>& V) : jmp(1, V) {
		for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
			jmp.emplace_back(sz(V) - pw * 2 + 1);
			rep(j,0,sz(jmp[k]))
				jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
		}
	}
	T query(int a, int b) {
		assert(a < b); // or return inf if a == b
		int dep = 31 - __builtin_clz(b - a);
		return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
	}
};

typedef int int2;
#define int long long
#define pi pair<int, int>
#define vi vector<int>
#define vii vector<vector<int>>
#define vpi vector<pi>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
const int inf = 1e18;

int t;



int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> a(n+1);
    lep(i,1,n) {
        cin >> a[i];
    }
    vector<vector<pi>> adj(n+1);
    vector<int> depth(n+1);
    lep(i,1,n-1) {
        int u, v, l;
        cin >> u >> v >> l;
        adj[u].pb({v, l});
        adj[v].pb({u, l});
    }
    int T = 0;
    vector<int> tin(n+1);
    vector<int> path, times;
    auto dfs1 = [&](auto &self, int u, int p) -> void {
        tin[u] = T++;
        for (pi pr: adj[u]) {
            int v = pr.f;
            int l = pr.s;
            if (v != p) {
                times.pb(tin[u]);
                path.pb(u);
                depth[v] = depth[u] + l;
                self(self, v, u);
            }
        }
    };

    dfs1(dfs1, 1, 0);

    RMQ<int> rmq(times);
    auto get_lca = [&](int u, int v) -> int {
        if (tin[u] > tin[v]) swap(u, v);
        if (u == v) return u;
        return path[rmq.query(tin[u], tin[v])];
    };

    auto get_dist = [&](int u, int v) -> int {
        return depth[u] + depth[v] - 2 * depth[get_lca(u, v)];
    };

    vector<pi> elts;
    lep(i,1,n) {
        elts.pb({a[i], i});
    }
    sort(elts.begin(), elts.end());
    vector<pi> diams;
    diams.pb({elts[0].s, elts[0].s});
    // lep(i,1,3) {
    //     lep(j,1,3) {
    //         cout << get_dist(i,j) << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    lep(i,1,n-1) {
        int val = elts[i].f;
        int where = elts[i].s;
        auto [u, v] = diams.back();
        // cout << "u: " << u << ", v: " << v << "\n";
        int diam = get_dist(u, v);
        if (get_dist(where, v) > diam) {
            u = where;
        } else if (get_dist(where, u) > diam) {
            v = where;
        }
        diams.pb({u, v});
    }
    // for (auto [u, v]: diams) {
    //     cout << "u: " << u << ", v: " << v << ", dist: " << get_dist(u, v) << "\n";
    // }
    vector<int> ans(q+1);
    lep(qq,1,q) {
        int u, d;
        cin >> u >> d;
        // binary search on answer
        int q_ans = -1;
        int ln = 20;
        rep(i,20,0) {
            int idx = q_ans + (1<<i);
            if (idx < (int)diams.size() and max(get_dist(u, diams[idx].f), get_dist(u, diams[idx].s)) <= d) {
                q_ans = idx;
            }
        }
        // cout << "qq: " << qq << ", q_ans: " << q_ans << "\n";
        if (q_ans == -1) {
            ans[qq] = 0;
        } else {
            ans[qq] = elts[q_ans].f + 1;
        }
    }
    // for (auto [u, v]: {mp(4LL,3LL), mp(4LL,5LL)}) {
    //     cout << "u: " << u << ", v: " << v << ", duv: " << get_dist(u,v) << "\n";
    // }
    vector<bool> exists(n+2);
    lep(i,1,n) {
        if (a[i] <= n+1) exists[a[i]] = true;
    }

    // cout << "exists:\n";
    // lep(i,0,n) {
    //     cout << exists[i] << " ";
    // }
    // cout << "\n";
    int mn = inf;
    lep(i,0,n+1) {
        if (!exists[i]) {
            mn = i;
            break;
        }
    }

    // cout << "mn: " << mn << "\n";

    lep(qq,1,q) {
        cout << min(mn, ans[qq]) << "\n";
    }




    
} 

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: 0
Accepted
time: 619ms
memory: 151228kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

ok 500000 numbers

Test #3:

score: 0
Accepted
time: 718ms
memory: 151144kb

input:

500000 500000
416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...

output:

0
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
2500...

result:

ok 500000 numbers

Test #4:

score: 0
Accepted
time: 866ms
memory: 164188kb

input:

500000 500000
161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...

output:

0
250000
250000
69
10
250000
250000
0
5
250000
250000
250000
1
33
69
10
250000
250000
250000
10
0
250000
250000
250000
1
250000
250000
0
250000
10
5
0
33
10
250000
1
66
250000
1
250000
1
250000
250000
250000
16
1
1
0
5
10
250000
250000
0
250000
250000
250000
250000
250000
250000
250000
5
1
250000
1
...

result:

ok 500000 numbers

Test #5:

score: 0
Accepted
time: 770ms
memory: 164484kb

input:

500000 500000
205699 141313 291987 999929528 111195 248994 999960789 391023 323134 92686 432395 316257 188889 219187 315056 39556 999959131 279778 30709 419259 242325 169515 137848 999905250 130641 245510 26 52987 999900052 144308 180276 162429 405263 130502 101937 108999 259339 137566 414011 304915...

output:

0
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
3
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
250000
250000
250000
250000
2
250...

result:

ok 500000 numbers

Test #6:

score: 0
Accepted
time: 844ms
memory: 170200kb

input:

500000 500000
142817 198136 999910358 236637 238445 325614 22961 999909937 354352 999984540 189454 221148 208300 169118 999945163 256203 999945857 276295 175580 481676 114118 119423 999962365 999904418 250723 333971 311979 384475 386372 11544 999956892 999960490 999936512 292089 42395 5450 326202 26...

output:

0
250004
0
4
6
5
250004
6
250004
250004
0
0
250004
250004
250004
250004
0
0
250004
250004
250004
0
0
0
0
0
6
0
0
0
0
6
0
250004
4
250004
250004
250004
250004
250004
0
0
6
250004
6
250004
0
0
6
250004
4
250004
0
6
250004
250004
4
250004
0
6
0
250004
0
0
0
250004
4
0
0
6
0
0
0
6
0
250004
4
250004
5
25...

result:

ok 500000 numbers

Test #7:

score: 0
Accepted
time: 809ms
memory: 164684kb

input:

500000 500000
273731 92547 391851 223257 245734 999907678 125748 999999710 260816 269057 173753 337309 208492 162862 160710 371998 296145 999901286 269727 36806 193730 999966618 219876 999919231 999950308 999989538 403483 27 199626 83765 58199 38349 999985326 362514 103279 96338 292486 353093 235704...

output:

0
250007
0
250007
250007
250007
250007
250007
250007
0
250007
0
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
2
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
0
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
2...

result:

ok 500000 numbers

Test #8:

score: -100
Wrong Answer
time: 815ms
memory: 159812kb

input:

500000 500000
38066 263927 999904526 64975 999942253 139690 333059 341736 101159 145885 366360 999977224 58855 20369 491696 99014 999901769 497319 464130 118700 167144 197334 22373 266338 218334 344178 201845 470679 63936 29 473097 168465 91804 999907288 213685 441541 15210 999994423 241375 262928 1...

output:

0
249999
3
3
3
3
3
0
3
3
0
249999
3
249999
249999
249999
3
249999
249999
249999
3
249999
249999
3
249999
249999
249999
249999
249999
3
249999
249999
249999
3
249999
249999
249999
3
249999
3
249999
3
0
249999
3
249999
249999
249999
0
3
249999
249999
0
249999
249999
249999
249999
3
249999
0
0
0
3
3
3
...

result:

wrong answer 3790th numbers differ - expected: '1', found: '6'