QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#65493#5094. 小 H 分糖果Zesty_Fox0 36ms307680kbC++145.3kb2022-12-01 12:48:432022-12-01 12:49:43

Judging History

你现在查看的是测评时间为 2022-12-01 12:49:43 的历史记录

  • [2023-10-04 02:48:16]
  • 管理员手动重测本题所有提交记录
  • 测评结果:0
  • 用时:4467ms
  • 内存:328428kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-01 12:49:43]
  • 评测
  • 测评结果:0
  • 用时:36ms
  • 内存:307680kb
  • [2022-12-01 12:48:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using i64=long long;
using i128=__int128_t;
using u32=unsigned int;
using u64=unsigned long long;
using db=double;
using pii=pair<int,int>;
using vi=vector<int>;

template<typename T>
inline T read(){
    T x=0,f=0;char ch=getchar();
    while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
    while(isdigit(ch)) x=x*10+(ch-'0'),ch=getchar();
    return f?-x:x;
}
template<typename T>
inline void write(T x){
    if(x<0) {putchar('-'),x=-x;}
    static int bit[50];
    int tp=0;
    do bit[++tp]=x%10,x/=10; while(x);
    while(tp) putchar(bit[tp--]+'0');
}

#define rdi read<int>
#define rdi64 read<i64>
#define fi first
#define se second
#define pb push_back

const int N=1e5+10,MX=1e9;

int n,q,a[N];
vi e[N];
i128 sum2;

inline int lg2(int x) {return !x?-1:__lg(x);}

int f[N][18],dep[N];
int dfn[N],low[N],ti,seq[N];
void dfs(int x,int fa){
    dfn[x]=++ti,dep[x]=dep[fa]+1,seq[ti]=x;
    f[x][0]=fa;
    for(int i=1;i<=lg2(dep[x]);i++) f[x][i]=f[f[x][i-1]][i-1];
    for(auto y:e[x]){
        if(y==fa) continue;
        dfs(y,x);
    }
    low[x]=ti;
}

int lca(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    for(int i=lg2(dep[x]-dep[y]);i>=0;i--)
        if(dep[f[x][i]]>=dep[y]) x=f[x][i];
    if(x==y) return x;
    for(int i=lg2(dep[x]);i>=0;i--)
        if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
    return f[x][0];
}

struct Data{
    int s0;i64 s1;i128 s2;
    Data() {s0=s1=s2=0;}
    Data(int s0,i64 s1,i128 s2):s0(s0),s1(s1),s2(s2){}
    Data(int v){
        if(v>=0) s0=1,s1=v,s2=(i64)v*v;
        else s0=-1,s1=v,s2=(i64)-v*v;
    }
    Data operator + (const Data &rhs) const{
        return {s0+rhs.s0,s1+rhs.s1,s2+rhs.s2};
    }
    Data operator - (const Data &rhs) const{
        return {s0-rhs.s0,s1-rhs.s1,s2-rhs.s2};
    }
    Data operator * (int b) const{
        return {s0*b,s1*b,s2*b};
    }
};

struct Op{
    int op,x,y;
    i64 z;
}p[N];
Op md[N];
int cnt;

struct SGT{
#define lson (t[nw].ls)
#define rson (t[nw].rs)
#define mid ((l+r)>>1)
    struct Node{int ls,rs;Data d;}t[N*64];
    int tot;
    void add(int &nw,int pr,int l,int r,int x,int v){
        t[nw=++tot]=t[pr],t[nw].d=t[nw].d+v;
        if(l==r) return;
        if(x<=mid) add(lson,t[pr].ls,l,mid,x,v);
        else add(rson,t[pr].rs,mid+1,r,x,v);
    }
    i128 query(int rt1,int rt2,int rt3,int rt4,int l,int r,
            i64 lim,int x,int y,int lc,
            Data d={0,0,0},i128 sum=0){
        auto qinfo = [&](int typ){
            auto insub = [&](int x,int y){
                return dfn[x]<=dfn[y]&&dfn[y]<=low[x];
            };
            auto on_path = [&](int u){
                return insub(lc,u)&&(insub(u,x)||insub(u,y));
            };

            Data ret;int l1,r1;
            if(typ==1) ret=t[rt1].d+t[rt2].d-t[rt3].d-t[rt4].d,l1=l,r1=r;
            else if(typ==2) ret=t[t[rt1].ls].d+t[t[rt2].ls].d-t[t[rt3].ls].d-t[t[rt4].ls].d,l1=l,r1=mid;
            else if(typ==3) ret=t[t[rt1].rs].d+t[t[rt2].rs].d-t[t[rt3].rs].d-t[t[rt4].rs].d,l1=mid+1,r1=r;
            for(int i=1;i<=cnt;i++)
                if(on_path(p[i].x)){
                    if(l1<=p[i].y&&p[i].y<=r1) ret=ret-p[i].y;
                    if(l1<=p[i].z&&p[i].z<=r1) ret=ret+p[i].z;
                }
            return ret;
        };
        if(l==r){
            d=d+qinfo(1);
            i64 rest=lim-(d.s1-(i64)l*d.s0);
            i128 res=(i128)l*l*d.s0+sum;
            if(l) res-=(i128)(2*l-1)*rest;
            return res+(sum2-d.s2-sum);
        }
        Data tmp=qinfo(3),nxt=tmp+d;
        if(nxt.s1-(i64)mid*nxt.s0<=lim) return query(t[rt1].ls,t[rt2].ls,t[rt3].ls,t[rt4].ls,l,mid,lim,x,y,lc,nxt,sum);
        else return query(t[rt1].rs,t[rt2].rs,t[rt3].rs,t[rt4].rs,mid+1,r,lim,x,y,lc,d,sum+qinfo(2).s2);
    }
    void clear() {tot=0;}
#undef lson
#undef rson
#undef mid
}t;

int rt[N];
void rebuild(){
    t.clear(),cnt=0;
    for(int i=1;i<=n;i++){
        int x=seq[i];
        t.add(rt[x],rt[f[x][0]],0,MX,a[x],a[x]);
    }
}

void init(){
    n=rdi(),q=rdi();
    for(int i=1;i<n;i++){
        int x=rdi(),y=rdi();
        e[x].pb(y),e[y].pb(x);
    }
    for(int i=1;i<=n;i++) a[i]=rdi();

    dfs(1,0);
    for(int i=1;i<=n;i++)
        sum2+=(i64)a[i]*a[i];

    static int a1[N];
    copy(a+1,a+n+1,a1+1);
    for(int i=1;i<=q;i++){
        p[i].op=rdi();
        if(p[i].op==1){
            p[i].x=rdi();
            p[i].y=a[p[i].x],p[i].z=rdi();
            a[p[i].x]=p[i].z;
        }
        else{
            p[i].x=rdi(),p[i].y=rdi(),p[i].z=rdi64();
        }
    }
    copy(a1+1,a1+n+1,a+1);
}

i128 query(int x,int y,i64 k){
    int lc=lca(x,y);
    return t.query(rt[x],rt[y],rt[lc],rt[f[lc][0]],0,MX,k,x,y,lc);
}

void solve(){
    init();
    int bsiz=sqrt(n)*4;
    for(int l=1,r;l<=q;l=r+1){
        r=min(q,l+bsiz-1);
        rebuild();
        for(int i=l;i<=r;i++){
            if(p[i].op==1){
                sum2=sum2-(i64)p[i].y*p[i].y+(i64)p[i].z*p[i].z;
                a[p[i].x]=p[i].z,md[++cnt]=p[i];
            }
            else write(query(p[i].x,p[i].y,p[i].z)),putchar('\n');
        }
    }
}

int main(){
#ifdef LOCAL
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    solve();
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 36ms
memory: 307680kb

input:

866 841
1 864
4 153
9 559
10 410
11 336
12 417
14 666
18 241
21 184
22 849
23 40
25 783
26 189
28 329
29 216
31 864
34 581
40 131
42 625
45 744
47 723
50 633
51 447
52 454
53 88
55 619
60 259
62 680
67 126
72 371
73 742
80 196
81 536
82 647
85 254
87 172
88 489
93 708
94 227
95 340
96 7
7 91
97 594
...

output:

285125508
285374449
285871392
285072359
284419704
284843737
284692039
284936099
285944374
285174668
285019779
284651455
287282253
287175619
284878507
285369672
284880507
285404741
284913527
286053317
288622563
286960150
287231109
288326074
286937403
287883097
288535226
288195055
288676358
288632989
...

result:

wrong answer 23rd lines differ - expected: '287194443', found: '287231109'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

input:

87080 98363
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

27217464773998101198216
27222683135365131711066
27215685950441383375941
27221607244120669838311
27219047117137492446677
27222635053035794978138
27218848172360084265818
27217641965048032442370
27217075857038185043354
27219505943263517662069
27219987830714690994915
27216425553487126261338
272156845500...

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%