提出 #39856692


ソースコード 拡げる

#include <bits/stdc++.h>
#define int long long
#ifndef use_ios11
#define use_ios11
using namespace std;
#define log(a) cerr << "\033[32m[DEBUG] " << #a << '=' << (a) << " @ line " << __LINE__ << "\033[0m" << endl
typedef long long ll;
#define pb push_back
typedef pair<int, int> pii;

#define mem(p) memset(&p, 0, sizeof(p))
typedef pair<long long, long long> pll;
#define ir(x) \
    int x;    \
    yin >> x
#define foor(action, actionx2, actionx4, width)         \
    do                                                  \
    {                                                   \
        unsigned long __width = (unsigned long)(width); \
        unsigned long __increment = __width >> 2;       \
        for (; __increment > 0; __increment--)          \
        {                                               \
            actionx4;                                   \
        }                                               \
        switch (__width & 3)                            \
        {                                               \
        case 2:                                         \
            actionx2;                                   \
            break;                                      \
        case 3:                                         \
            actionx2;                                   \
        case 1:                                         \
            action;                                     \
            break;                                      \
        }                                               \
    } while (0)
struct ins
{
    int ans;
    ins()
    {
        ans = 1;
    }
#define endl '\n'
    void read()
    {
    }
    void read1(char &s)
    {
        char c = getchar();
        for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
            ;
        s = c;
        if (c == EOF)
            ans = 0;
    }
    void read1(string &s)
    {
        s = "";
        char c = getchar();
        for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
            ;
        for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
            s += c;
        if (c == EOF)
            ans = 0;
    }
    void read1(char *s)
    {
        char c = getchar();
        int tt = 0;
        for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
            ;
        for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
            s[tt++] = c;
        s[tt] = '\0';
        if (c == EOF)
            ans = 0;
    }

    template <typename T>
    void read1(T &n)
    {
        T x = 0;
        int f = 1;
        char c = getchar();
        for (; !isdigit(c); c = getchar())
        {
            if (c == '-')
                f = -1;
            if (c == EOF)
            {
                ans = 0;
                return;
            }
        }
        for (; isdigit(c); c = getchar())
            x = x * 10 + c - 48;
        n = x * f;
        if (c == EOF)
            ans = 0;
        if (c != '.')
            return;
        T l = 0.1;
        while ((c = getchar()) <= '9' && c >= '0')
            x = x + (c & 15) * l, l *= 0.1;
        n = x * f;
        if (c == EOF)
            ans = 0;
    }
    void write() {}
    void write1(string s)
    {
        int n = s.size();
        for (int i = 0; i < n; i++)
            putchar(s[i]);
    }
    void write1(const char *s)
    {
        int n = strlen(s);
        for (int i = 0; i < n; i++)
            putchar(s[i]);
    }
    void write1(char *s)
    {
        int n = strlen(s);
        for (int i = 0; i < n; i++)
            putchar(s[i]);
    }

    void write1(char s)
    {
        putchar(s);
    }
    void write1(float s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%df", x);
        printf(y, s);
    }
    void write1(double s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%dlf", x);
        printf(y, s);
    }
    void write1(long double s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%dLf", x);
        printf(y, s);
    }
    template <typename T>
    void write1(T n)
    {
        if (n < 0)
            n = -n, putchar('-');
        if (n > 9)
            write1(n / 10);
        putchar('0' + n % 10);
    }
    friend ins operator>>(ins x, char *n);
    template <typename T>
    friend ins operator>>(ins x, T &n);
    template <typename T>
    friend ins operator<<(ins x, T n);
    operator bool()
    {
        return ans;
    }
};

ins operator>>(ins x, char *n)
{
    if (!x.ans)
        return x;
    x.read1(n);
    return x;
}

template <typename T>
ins operator>>(ins x, T &n)
{
    if (!x.ans)
        return x;
    x.read1(n);
    return x;
}
template <typename T>
ins operator<<(ins x, T n)
{
    x.write1(n);
    return x;
}
ins yin;
ins yout;
#endif
const int maxn = 5e5 + 10;
int n, x[maxn], y[maxn], z[maxn], fa[maxn], dep[maxn], sz[maxn], son[maxn], dd[maxn], top[maxn], dfn[maxn], cnt, val[maxn], Ldfn[maxn], Rdfn[maxn];
struct ZhkSegmentTree
{
#define ls num << 1
#define rs num << 1 | 1
    int sum[maxn << 2], tag[maxn << 2], len[maxn << 2];
    inline void pushup(int num)
    {
        sum[num] = sum[ls] + sum[rs];
    }
    inline void down(int num, int x)
    {
        tag[num] = tag[num] + x;
        sum[num] = sum[num] + (ll)len[num] * x;
    }
    inline void pushdown(int num)
    {
        if (tag[num])
        {
            down(ls, tag[num]);
            down(rs, tag[num]);
            tag[num] = 0;
        }
    }
    inline void build(int num, int l, int r)
    {
        len[num] = r - l + 1;
        if (l == r)
        {
            sum[num] = val[dd[l]];
            return;
        }
        int mid = (l + r) >> 1;
        build(ls, l, mid);
        build(rs, mid + 1, r);
        pushup(num);
    }
    inline void change(int num, int l, int r, int L, int R, int x)
    {
        // cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
        if (L <= l && r <= R)
        {
            down(num, x);
            return;
        }
        pushdown(num);
        int mid = (l + r) >> 1;
        if (mid >= L)
            change(ls, l, mid, L, R, x);
        if (mid < R)
            change(rs, mid + 1, r, L, R, x);
        pushup(num);
    }
    inline int query(int num, int l, int r, int L, int R)
    {
        if (L <= l && r <= R)
            return sum[num];
        pushdown(num);
        int mid = (l + r) >> 1;
        if (mid < L)
            return query(rs, mid + 1, r, L, R);
        if (mid >= R)
            return query(ls, l, mid, L, R);
        return query(ls, l, mid, L, R) + query(rs, mid + 1, r, L, R);
    }
} T;
vector<int> v[maxn];
inline void dfs1(int x, int f)
{
    sz[x] = 1;
    fa[x] = f;
    dep[x] = dep[f] + 1;
    for (auto i : v[x])
        if (i != f)
        {
            dfs1(i, x);
            sz[x] += sz[i];
            if (sz[i] > sz[son[x]])
                son[x] = i;
        }
}
inline void dfs2(int x, int f)
{
    top[x] = f;
    Ldfn[x] = dfn[x] = ++cnt, Rdfn[x] = Ldfn[x] + sz[x] - 1;
    dd[cnt] = x;
    if (!son[x])
        return;
    dfs2(son[x], f);
    for (auto i : v[x])
        if (i != fa[x] && i != son[x])
            dfs2(i, i);
}
inline int LCA(int x, int y)
{
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    return x;
}
inline int answer(int x, int y)
{
    int ans = 0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        ans = ans + T.query(1, 1, n, dfn[top[x]], dfn[x]);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    ans = ans + T.query(1, 1, n, dfn[x], dfn[y]);
    return ans;
}
signed main()
{
    int Q;
    yin >> n;
    for (int i = 1; i < n; i++)
    {
        yin >> x[i] >> y[i] >> z[i];
        v[x[i]].push_back(y[i]);
        v[y[i]].push_back(x[i]);
    }
    dfs1(1, 0);
    for (int i = 1; i < n; i++)
    {
        if (dep[x[i]] < dep[y[i]])
            swap(x[i], y[i]);
        val[x[i]] = z[i];
    }
    dfs2(1, 1);
    T.build(1, 1, n);
    yin >> Q;
    while (Q--)
    {
        int flg, x, y;
        yin >> flg >> x;
        if (flg == 1)
        {
            yin >> y;
            T.change(1, 1, n, dfn[::x[x]], dfn[::x[x]], y - val[::x[x]]);
            val[::x[x]] = y;
        }
        if (flg == 2)
        {
            yin >> y;
            int k = LCA(x, y);
            yout << (answer(x, y) - T.query(1, 1, n, dfn[k], dfn[k])) << endl;
        }
    }
    return 0;
}

提出情報

提出日時
問題 G - Distance Queries on a Tree
ユーザ Zmx_Yxy
言語 C++ (GCC 9.2.1)
得点 600
コード長 8996 Byte
結果 AC
実行時間 636 ms
メモリ 65272 KiB

コンパイルエラー

./Main.cpp: In member function ‘void ins::write1(float, long long int)’:
./Main.cpp:138:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long long int’ [-Wformat=]
  138 |         sprintf(y, "%%.%df", x);
      |                        ~^    ~
      |                         |    |
      |                         int  long long int
      |                        %lld
./Main.cpp: In member function ‘void ins::write1(double, long long int)’:
./Main.cpp:144:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long long int’ [-Wformat=]
  144 |         sprintf(y, "%%.%dlf", x);
      |                        ~^     ~
      |                         |     |
      |                         int   long long int
      |                        %lld
./Main.cpp: In member function ‘void ins::write1(long double, long long int)’:
./Main.cpp:150:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long long int’ [-Wformat=]
  150 |         sprintf(y, "%%.%dLf", x);
      |                        ~^     ~
      |                         |     |
      |                         int   long long int
      |                        %lld
./Main.cpp: In function ‘int main()’:
./Main.cpp:349:9: warning: ‘flg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  349 |         if (flg == 2)
      |         ^~
./Main.cpp:346:70: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  346 |             T.change(1, 1, n, dfn[::x[x]], dfn[::x[x]], y - val[::x[x]]);
      |                                                                 ~~~~~^

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 4
AC × 73
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt, 02_random_49.txt, 02_random_50.txt, 02_random_51.txt, 02_random_52.txt, 02_random_53.txt, 02_random_54.txt, 02_random_55.txt, 02_random_56.txt, 02_random_57.txt, 02_random_58.txt, 02_random_59.txt, 02_random_60.txt, 02_random_61.txt, 02_random_62.txt, 02_random_63.txt, 02_random_64.txt, 02_random_65.txt, 02_random_66.txt, 02_random_67.txt, 02_random_68.txt, 02_random_69.txt, 02_random_70.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 22 ms 15228 KiB
00_sample_01.txt AC 15 ms 15164 KiB
00_sample_02.txt AC 20 ms 15068 KiB
00_sample_03.txt AC 14 ms 15276 KiB
01_handmade_03.txt AC 467 ms 55380 KiB
01_handmade_04.txt AC 455 ms 55252 KiB
01_handmade_05.txt AC 442 ms 54464 KiB
01_handmade_06.txt AC 249 ms 65272 KiB
01_handmade_07.txt AC 277 ms 58148 KiB
01_handmade_08.txt AC 199 ms 52604 KiB
01_handmade_09.txt AC 220 ms 53932 KiB
01_handmade_10.txt AC 25 ms 15140 KiB
02_random_10.txt AC 97 ms 30332 KiB
02_random_11.txt AC 96 ms 30480 KiB
02_random_12.txt AC 134 ms 45116 KiB
02_random_13.txt AC 282 ms 53568 KiB
02_random_14.txt AC 210 ms 53200 KiB
02_random_15.txt AC 292 ms 46148 KiB
02_random_16.txt AC 116 ms 20420 KiB
02_random_17.txt AC 180 ms 35608 KiB
02_random_18.txt AC 283 ms 34776 KiB
02_random_19.txt AC 171 ms 52164 KiB
02_random_20.txt AC 410 ms 38104 KiB
02_random_21.txt AC 206 ms 64124 KiB
02_random_22.txt AC 123 ms 34828 KiB
02_random_23.txt AC 84 ms 21072 KiB
02_random_24.txt AC 87 ms 26820 KiB
02_random_25.txt AC 148 ms 54888 KiB
02_random_26.txt AC 57 ms 20592 KiB
02_random_27.txt AC 176 ms 50056 KiB
02_random_28.txt AC 72 ms 38324 KiB
02_random_29.txt AC 94 ms 32304 KiB
02_random_30.txt AC 97 ms 46404 KiB
02_random_31.txt AC 222 ms 36884 KiB
02_random_32.txt AC 124 ms 34860 KiB
02_random_33.txt AC 402 ms 52068 KiB
02_random_34.txt AC 123 ms 37836 KiB
02_random_35.txt AC 299 ms 45152 KiB
02_random_36.txt AC 322 ms 46064 KiB
02_random_37.txt AC 241 ms 52124 KiB
02_random_38.txt AC 328 ms 47612 KiB
02_random_39.txt AC 190 ms 53336 KiB
02_random_40.txt AC 301 ms 36876 KiB
02_random_41.txt AC 158 ms 42172 KiB
02_random_42.txt AC 182 ms 57204 KiB
02_random_43.txt AC 153 ms 45424 KiB
02_random_44.txt AC 194 ms 59340 KiB
02_random_45.txt AC 185 ms 47432 KiB
02_random_46.txt AC 103 ms 35144 KiB
02_random_47.txt AC 166 ms 51688 KiB
02_random_48.txt AC 159 ms 48444 KiB
02_random_49.txt AC 102 ms 34888 KiB
02_random_50.txt AC 176 ms 47868 KiB
02_random_51.txt AC 354 ms 54212 KiB
02_random_52.txt AC 274 ms 54280 KiB
02_random_53.txt AC 408 ms 54176 KiB
02_random_54.txt AC 218 ms 54272 KiB
02_random_55.txt AC 464 ms 54076 KiB
02_random_56.txt AC 473 ms 55304 KiB
02_random_57.txt AC 351 ms 55112 KiB
02_random_58.txt AC 603 ms 55168 KiB
02_random_59.txt AC 222 ms 55176 KiB
02_random_60.txt AC 636 ms 55116 KiB
02_random_61.txt AC 259 ms 60784 KiB
02_random_62.txt AC 269 ms 59184 KiB
02_random_63.txt AC 286 ms 59416 KiB
02_random_64.txt AC 233 ms 60620 KiB
02_random_65.txt AC 285 ms 64852 KiB
02_random_66.txt AC 198 ms 54132 KiB
02_random_67.txt AC 196 ms 54204 KiB
02_random_68.txt AC 207 ms 54024 KiB
02_random_69.txt AC 168 ms 54236 KiB
02_random_70.txt AC 208 ms 54016 KiB