QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#787168#6404. Shuttle TourWaterSunRE 142ms71436kbC++144.8kb2024-11-27 10:12:292024-11-27 10:12:37

Judging History

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

  • [2024-11-27 10:12:37]
  • 评测
  • 测评结果:RE
  • 用时:142ms
  • 内存:71436kb
  • [2024-11-27 10:12:29]
  • 提交

answer

#include <bits/stdc++.h>
#define re register
#define int long long

using namespace std;

const int N = 2e5 + 10,M = N * 2;
const int inf = 1e9 + 10;
int n,q;
char s[N]; bool st[N];
int idx,h[N],ne[M],e[M],w[M];
int lf = 1,tim,id[N],dfn[N],pid[N],dep[N];

inline int read(){
    int r = 0,w = 1;
    char c = getchar();
    while (c < '0' || c > '9'){
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9'){
        r = (r << 3) + (r << 1) + (c ^ 48);
        c = getchar();
    }
    return r * w;
}

inline void add(int a,int b,int c){
    ne[idx] = h[a];
    e[idx] = b; w[idx] = c;
    h[a] = idx++;
}

inline void dfs(int u,int fa){
    id[u] = lf; pid[dfn[u] = ++tim] = u;
    bool falg = true;
    for (re int i = h[u];~i;i = ne[i]){
        int v = e[i];
        if (v == fa) continue;
        dep[v] = dep[u] + w[i];
        dfs(v,u); falg = false;
    }
    if (falg) lf++;
}

namespace LCA{
    #define pot(x) (1 << (x))

    const int M = 24;
    int tim,lg[N],dfn[N],dp[N][M];

    inline void dfs(int u,int fa){
        dp[dfn[u] = ++tim][0] = dep[u];
        for (re int i = h[u];~i;i = ne[i]){
            int v = e[i];
            if (v == fa) continue;
            dfs(v,u); dp[++tim][0] = dep[u];
        }
    }

    inline void init(){
        dfs(1,0);
        for (re int i = 2;i <= tim;i++) lg[i] = lg[i >> 1] + 1;
        for (re int j = 1;j <= lg[tim];j++){
            for (re int i = 1;i + pot(j) - 1 <= tim;i++) dp[i][j] = min(dp[i][j - 1],dp[i + pot(j - 1)][j - 1]);
        }
    }

    inline int lca(int x,int y){
        int sx = dfn[x],sy = dfn[y];
        if (sx > sy) swap(sx,sy);
        int t = lg[sy - sx + 1];
        return min(dp[sx][t],dp[sy - pot(t) + 1][t]);
    }

    inline int dist(int x,int y){
        return (dep[x] + dep[y] - 2 * lca(x,y));
    }

    #undef pot
}
using LCA::dist;

struct{
    #define ls(u) (u << 1)
    #define rs(u) (u << 1 | 1)
    #define val(u) (tr[u].val)

    struct value{
        int Max,Min;

        value friend operator +(const value &a,const value &b){
            return {max(a.Max,b.Max),min(a.Min,b.Min)};
        }
    };

    struct node{
        int l,r;
        value val;
    }tr[N << 2];

    inline void pushup(int u){
        val(u) = val(ls(u)) + val(rs(u));
    }

    inline void build(int u,int l,int r,int typ){
        tr[u] = {l,r,-inf,inf};
        if (l == r){
            if (st[l] && id[l] == typ) val(u) = {dfn[l],dfn[l]};
            return;
        }
        int mid = l + r >> 1;
        build(ls(u),l,mid,typ); build(rs(u),mid + 1,r,typ);
        pushup(u);
    }

    inline void update(int u,int x){
        if (tr[u].l == tr[u].r){
            if (st[x]) val(u) = {dfn[x],dfn[x]};
            else val(u) = {-inf,inf};
            return;
        }
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) update(ls(u),x);
        else update(rs(u),x);
        pushup(u);
    }

    inline value query(int u,int l,int r){
        if (l <= tr[u].l && tr[u].r <= r) return val(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid && r > mid) return query(ls(u),l,r) + query(rs(u),l,r);
        else if (l <= mid) return query(ls(u),l,r);
        else return query(rs(u),l,r);
    }

    #undef ls
    #undef rs
    #undef val
}T[51];

signed main(){
    memset(h,-1,sizeof(h));
    n = read(),q = read();
    scanf("%s",s + 1);
    for (re int i = 1;i <= n;i++) st[i] = s[i] - '0';
    for (re int i = 1,a,b,c;i < n;i++){
        a = read(),b = read(),c = read();
        add(a,b,c); add(b,a,c);
    }
    dfs(1,0); LCA::init();
    for (re int i = 1;i < lf;i++) T[i].build(1,1,n,i);
    // for (re int i = 1;i <= n;i++) cerr << dep[i] << " "; cerr << "\n";
    // for (re int i = 1;i <= n;i++){
    //     for (re int j = i + 1;j <= n;j++) cerr << i << " " << j << " " << lca(i,j) << " ???\n";
    // }
    while (q--){
        int op; op = read();
        if (op == 1){
            int x;
            st[x = read()] ^= 1;
            T[id[x]].update(1,x);
        }
        else{
            int l,r;
            l = read(),r = read();
            vector<int> pt;
            for (re int i = 1;i < lf;i++){
                auto val = T[i].query(1,l,r);
                if (val.Max != -inf) pt.push_back(val.Max);
                if (val.Min != inf) pt.push_back(val.Min);
            }
            sort(pt.begin(),pt.end());
            pt.erase(unique(pt.begin(),pt.end()),pt.end());
            // for (int x:pt) cerr << pid[x] << " "; cerr << "\n";
            int ans = 0,sz = pt.size();
            for (re int i = 0;i < sz;i++) ans += dist(pid[pt[i]],pid[pt[(i + 1) % sz]]);
            if (pt.empty()) puts("-1");
            else printf("%lld\n",ans);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 6
10110
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 5
1 3
2 1 5
2 2 4
2 5 5
2 1 1

output:

222
202
0
-1
0

result:

ok 5 number(s): "222 202 0 -1 0"

Test #2:

score: 0
Accepted
time: 142ms
memory: 69388kb

input:

50 200000
00100100100001101010110100000111100011101110011010
14 47 940241413
11 43 331483591
37 38 496247070
47 46 832459694
7 15 16479291
1 30 402388666
30 8 513064064
46 50 739311480
5 4 761894947
32 41 631669659
17 24 715786564
35 20 763151642
32 33 492466005
39 11 186023146
9 7 805081251
3 42 25...

output:

17149847378
-1
26540740138
29613692754
21307948558
27003443184
11893407374
18332625722
20412196350
20281573062
29727468956
9593307160
9380710234
23682775470
16688997988
2856655228
14982588156
0
-1
9140136614
26193602866
22558599316
24807163344
19126284190
8533875940
7695830712
18079494744
0
27004673...

result:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 139ms
memory: 71436kb

input:

50 200000
10010001110011101101101011010011100101101010111001
1 25 560163135
2 7 696374536
33 39 14485266
39 22 456690113
4 5 267811886
18 6 657680382
3 43 865617406
25 14 885515220
41 34 919525052
42 50 410634144
8 3 487403277
3 30 701549676
2 43 54223104
7 34 691078766
14 23 323352590
26 48 4936558...

output:

17177676326
31373486130
15290175638
8192974494
22734716092
27802380120
6315957348
0
15780401446
15929384564
26423899248
9639532596
21199366790
26413065782
-1
29063665908
29925313598
28651879288
17144176262
16526083688
28206620550
26873163342
14840246174
32414950818
29985336496
0
11937422088
11131990...

result:

ok 100000 numbers

Test #4:

score: 0
Accepted
time: 141ms
memory: 69348kb

input:

50 200000
01011001001010000100001101100111011001101011101000
34 29 654100339
33 5 947063765
45 25 962067151
11 13 509130797
26 47 988757358
49 22 75267557
7 11 431530851
46 1 531969122
22 43 449891945
34 6 526679193
50 17 499004364
22 23 226725950
26 48 203414490
11 44 900725507
10 29 451714017
35 2...

output:

24080362406
0
0
21418182584
28358635244
28257750030
24520294678
21418182584
0
15335211126
28621468542
18664505530
19335499776
32374868794
23618866752
26801803500
24116134918
27676993638
30222353942
25612316674
20504702130
28400398734
29472795250
24400110084
20586186786
25612316674
0
30067400886
1297...

result:

ok 100000 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 22240kb

input:

2 10
01
1 2 340134039
2 1 2
1 1
2 1 2
1 2
2 1 1
1 2
2 2 2
1 2
2 2 2
1 1

output:

0
680268078
0
0
-1

result:

ok 5 number(s): "0 680268078 0 0 -1"

Test #6:

score: -100
Runtime Error

input:

200000 100
1100001001001101010000111111011011101010001011011011011010101111010001111010101100101001010000110100100011111010011100101010010001011010111100101110010000101010000100111100000011001100111101110011000011010011011011100101100111101010111101110111001111111010001010010000110110100000111000111...

output:


result: