[WC2013]糖果公园

题面

UOJ

Sol

树上带修改莫队

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

template <class Int>
IL void Input(RG Int &x){
    RG int z = 1; RG char c = getchar(); x = 0;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= z;
}

const int maxn(1e5 + 5);

int n, m, q, first[maxn], cnt, w[maxn], f[maxn], c[maxn];
int dfn[maxn], st[20][maxn << 1], lg[maxn << 1], deep[maxn], idx, fa[maxn];
int sta[maxn], bl[maxn], blo, num, sum[maxn * 10], vis[maxn], tot1, tot2;
ll ans[maxn], tmpans;

struct Edge{
    int to, next;
} edge[maxn << 1];

struct Query{
    int l, r, id, t;

    IL int operator <(RG Query B) const{
        if(bl[l] != bl[B.l]) return bl[l] < bl[B.l];
        if(bl[r] != bl[B.r]) return bl[r] < bl[B.r];
        return t < B.t;
    }
} qry[maxn];

struct Change{
    int x, y;
} mdy[maxn];

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Dfs(RG int u){
    dfn[u] = ++idx, st[0][idx] = u;
    RG int l = sta[0];
    for(RG int e = first[u]; e != -1; e = edge[e].next){
        RG int v = edge[e].to;
        if(dfn[v]) continue;
        deep[v] = deep[u] + 1, fa[v] = u;
        Dfs(v);
        st[0][++idx] = u;
        if(sta[0] - l < blo) continue;
        for(++num; sta[0] != l; --sta[0]) bl[sta[sta[0]]] = num;
    }
    sta[++sta[0]] = u;
}

IL void Chk(RG int &x, RG int u, RG int v){
    x = deep[u] < deep[v] ? u : v;
}

IL int LCA(RG int u, RG int v){
    u = dfn[u], v = dfn[v];
    if(u > v) swap(u, v);
    RG int log2 = lg[v - u + 1], t;
    Chk(t, st[log2][u], st[log2][v - (1 << log2) + 1]);
    return t;
}

IL void Update(RG int x){
    if(vis[x]) tmpans -= 1LL * f[c[x]] * w[sum[c[x]]], --sum[c[x]];
    else ++sum[c[x]], tmpans += 1LL * f[c[x]] * w[sum[c[x]]];
    vis[x] ^= 1;
}

IL void Adjust(RG int x, RG int &y){
    if(vis[x]) Update(x), swap(c[x], y), Update(x);
    else swap(c[x], y);
}

IL void Modify(RG int u, RG int v){
    while(u != v){
        if(deep[u] > deep[v]) swap(u, v);
        Update(v), v = fa[v];
    }
}

IL void Init_Graph(){
    Input(n), Input(m), Input(q);
    blo = pow(n, 0.2 / 0.3);
    for(RG int i = 1; i <= m; ++i) Input(f[i]);
    for(RG int i = 1; i <= n; ++i) Input(w[i]), first[i] = -1;
    for(RG int i = 1, u, v; i < n; ++i) Input(u), Input(v), Add(u, v), Add(v, u);
    for(RG int i = 1; i <= n; ++i) Input(c[i]);
    Dfs(1);
    for(++num; sta[0]; --sta[0]) bl[sta[sta[0]]] = num;
    for(RG int i = 2; i <= idx; ++i) lg[i] = lg[i >> 1] + 1;
    for(RG int j = 1; j <= lg[idx]; ++j)
        for(RG int i = 1; i + (1 << j) - 1 <= idx; ++i)
            Chk(st[j][i], st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}

IL void Init_Task(){
    for(RG int i = 1, op, x, y; i <= q; ++i){
        Input(op), Input(x), Input(y);
        if(op){
            if(dfn[x] > dfn[y]) swap(x, y);
            qry[++tot1] = (Query){x, y, tot1, tot2};
        }
        else mdy[++tot2] = (Change){x, y};
    }
    sort(qry + 1, qry + tot1 + 1);
}

int main(RG int argc, RG char* argv[]){
    Init_Graph(), Init_Task();
    RG int j = 0, lca = LCA(qry[1].l, qry[1].r);
    while(j < qry[1].t) ++j, Adjust(mdy[j].x, mdy[j].y);
    Modify(qry[1].l, qry[1].r);
    Update(lca), ans[qry[1].id] = tmpans, Update(lca);
    for(RG int i = 2; i <= tot1; ++i){
        while(j < qry[i].t) ++j, Adjust(mdy[j].x, mdy[j].y);
        while(j > qry[i].t) Adjust(mdy[j].x, mdy[j].y), --j;
        lca = LCA(qry[i].l, qry[i].r);
        Modify(qry[i - 1].l, qry[i].l), Modify(qry[i - 1].r, qry[i].r);;
        Update(lca), ans[qry[i].id] = tmpans, Update(lca);
    }
    for(RG int i = 1; i <= tot1; ++i) printf("%lld\n", ans[i]);
    return 0;
}

转载于:https://www.cnblogs.com/cjoieryl/p/8729174.html

调一下代码(WC2013糖果公园,1<=n,m,q<=1e5),RE了: #include<bits/stdc++.h> using namespace std; long long n, m, Q, block, top, v[200010], w[200010], cn[200010]; vector<long long> vec[200010]; long long ans, cnt1, cnt2, pt, pl, pr; long long stk[200010], t1[200010], t2[200010]; long long dep[200010], q[200010], fa[26][200010]; long long sum[200010], cnt[200010], st[200010], pp[200010]; struct node{ long long l, r, pp; long long time, qtime; friend bool operator<(node p, node q){ return (p.l / block != q.l / block) ? (p.l < q.l) : ((p.r / block != q.r / block ? (p.r < q.r) : (p.time < q.time))); } }b1[200010]; struct node2{ long long l, r, time; }b2[200010]; void dfs(long long x, long long fa){ stk[++ top] = x; t1[x] = top; for(auto i : vec[x]){ if(i == fa) continue; dfs(i, x); } stk[++ top] = x; t2[x] = top; } void bfs(long long rt){ memset(dep, 0x3f, sizeof(dep)); dep[0] = 0, dep[rt] = 1; long long hh = 0, tt = 0; q[0] = rt; while(hh <= tt){ long long t = q[hh ++]; for(auto i : vec[t]){ if(dep[i] > dep[t] + 1){ dep[i] = dep[t] + 1; q[++ tt] = i; fa[i][0] = t; for(long long k = 1; k <= 25; ++ k){ fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } } } } long long lca(long long x, long long y){ if(dep[x] < dep[y]) swap(x, y); for(long long i = 25; i >= 0; -- i){ if(dep[fa[x][i]] >= dep[y]){ x = fa[x][i]; } } if(x == y) return x; for(long long i = 25; i >= 0; -- i){ if(fa[x][i] != fa[y][i]){ x = fa[x][i], y = fa[y][i]; } } return fa[x][0]; } void add(long long x){ st[x] ^= 1; if(st[x]){ cnt[cn[x]] ++; ans += v[cn[x]] * w[cnt[cn[x]]]; } else{ ans -= v[cn[x]] * w[cnt[cn[x]]]; cnt[cn[x]] --; } } int main(){ scanf("%lld %lld %lld", &n, &m, &Q); for(long long i = 1; i <= m; ++ i){ scanf("%lld", &v[i]); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &w[i]); } for(long long i = 1; i < n; ++ i){ long long x, y; scanf("%lld %lld", &x, &y); vec[x].push_back(y); vec[y].push_back(x); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &cn[i]); } dfs(1, -1); bfs(1); for(long long i = 1; i <= Q; ++ i){ long long op, l, r; scanf("%lld %lld %lld", &op, &l, &r); if(op == 0){ b2[++ cnt2].l = l; b2[cnt2].r = r; } else{ if(t1[l] > t1[r]) swap(l, r); long long p = lca(l, r); if(p == l){ b1[++ cnt1].l = t1[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = 0; } else{ b1[++ cnt1].l = t2[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = p; } b1[cnt1].time = cnt1; b1[cnt1].qtime = cnt2; } } block = max(1ll, (long long)cbrt(top * top)); sort(b1 + 1, b1 + cnt1 + 1); pt = 0, pl = 1, pr = 0; for(long long i = 1; i <= cnt1; ++ i){ while(pr < b1[i].r) ++ pr, add(stk[pr]); while(pr > b1[i].r) add(stk[pr]), -- pr; while(pl > b1[i].l) -- pl, add(stk[pl]); while(pl < b1[i].l) add(stk[pl]), ++ pl; while(pt < b1[i].qtime){ ++ pt; if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } } while(pt > b1[i].qtime){ if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } -- pt; } if(b1[i].pp){ add(b1[i].pp); } pp[b1[i].time] = ans; if(b1[i].pp){ add(b1[i].pp); } } for(long long i = 1; i <= cnt1; ++ i){ printf("%lld\n", pp[i]); } return 0; }
03-08
# P4074 [WC2013] 糖果公园 ## 题目描述 Candyland 有一座糖果公园公园里不仅有美丽的风景、好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩。 糖果公园的结构十分奇特,它由 $n$ 个游览点构成,每个游览点都有一个糖果发放处,我们可以依次将游览点编号为 $1$ 至 $n$。有 $n - 1$ 条双向道路连接着这些游览点,并且整个糖果公园都是连通的,即从任何一个游览点出发都可以通过这些道路到达公园里的所有其它游览点。 糖果公园所发放的糖果种类非常丰富,总共有 $m$ 种,它们的编号依次为 $1$ 至 $m$。每一个糖果发放处都只发放某种特定的糖果,我们用 $C_i$ 来表示 $i$ 号游览点的糖果。 来到公园里游玩的游客都不喜欢走回头路,他们总是从某个特定的游览点出发前往另一个特定的游览点,并游览途中的景点,这条路线一定是唯一的。他们经过每个游览点,都可以品尝到一颗对应种类的糖果。 大家对不同类型糖果的喜爱程度都不尽相同。 根据游客们的反馈打分,我们得到了糖果的美味指数, 第 $i$ 种糖果的美味指数为 $V_i$。另外,如果一位游客反复地品尝同一种类的糖果,他肯定会觉得有一些腻。根据量化统计,我们得到了游客第 $i$ 次品尝某类糖果的新奇指数 $W_i$。如果一位游客第 $i$ 次品尝第 $j$ 种糖果,那么他的愉悦指数 $H$ 将会增加对应的美味指数与新奇指数的乘积,即 $V_j \times W_i$。这位游客游览公园的愉悦指数最终将是这些乘积的和。 当然,公园中每个糖果发放点所发放的糖果种类不一定是一成不变的。有时,一些糖果点所发放的糖果种类可能会更改(也只会是 $m$ 种中的一种),这样的目的是能够让游客们总是感受到惊喜。 糖果公园的工作人员小 A 接到了一个任务,那就是根据公园最近的数据统计出每位游客游玩公园的愉悦指数。但数学不好的小 A 一看到密密麻麻的数字就觉得头晕,作为小 A 最好的朋友,你决定帮他一把。 ## 输入格式 从文件 `park.in` 中读入数据。 第一行包含三个正整数 $n, m, q$, 分别表示游览点个数、 糖果种类数和操作次数。 第二行包含 $m$ 个正整数 $V_1, V_2, \ldots, V_m$。 第三行包含 $n$ 个正整数 $W_1, W_2, \ldots, W_n$。 第四行到第 $n + 2$ 行,每行包含两个正整数 $A_i, B_i$,表示这两个游览点之间有路径可以直接到达。 第 $n + 3$ 行包含 $n$ 个正整数 $C_1, C_2, \ldots, C_n$。 接下来 $q$ 行, 每行包含三个整数 $Type, x, y$,表示一次操作: - 若 $Type$ 为 $0$,则 $1 \leq x \leq n$, $1 \leq y \leq m$,表示将编号为 $x$ 的游览点发放的糖果类型改为 $y$; - 若 $Type$ 为 $1$,则 $1 \leq x, y \leq n$,表示对出发点为 $x$,终止点为 $y$ 的路线询问愉悦指数。 ## 输出格式 输出到文件 `park.out` 中。 按照输入的先后顺序,对于每个 $Type$ 为 $1$ 的操作输出一行,用一个正整数表示答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 3 5 1 9 2 7 6 5 1 2 3 3 1 3 4 1 2 3 2 1 1 2 1 4 2 0 2 1 1 1 2 1 4 2 ``` ### 输出 #1 ``` 84 131 27 84 ``` ## 说明/提示 【样例解释】 我们分别用 ![](https://cdn.luogu.com.cn/upload/image_hosting/isw3ib3u.png) 代表 $C_i$ 为 $1$、 $2$、 $3$ 的节点,在修改之前: ![](https://cdn.luogu.com.cn/upload/image_hosting/ttkzii1u.png) 在将 $C_2$ 修改为 $1$ 之后: ![](https://cdn.luogu.com.cn/upload/image_hosting/izro364w.png) 【数据规模与约定】 对于所有的数据: $1 \leq V_i, W_i \leq 10^6$,$1 \leq A_i, B_i \leq n$, $1 \leq C_i \leq m$, $W_1, W_2, \ldots, W_n$ 是非递增序列,即对任意 $1 < i \leq n$, 满足 $W_i \le W_{i-1}$。 其它的限制条件如下表所示: ![QQ20180113072014.png](https://cdn.luogu.com.cn/upload/image_hosting/g6884nx1.png) #include <bits/stdc++.h> #define ll long long using namespace std; const ll N=2e6; struct node{ ll l,r,f,t,ans; }a[N+5]; struct node1{ ll x,y; }b[N+5]; ll n,m,q; ll c[N+5],v[N+5],w[N+5]; ll cnta,cntb; ll lenb,blk[N+5]; ll fst[N+5],lst[N+5],dfn[N+5],dfnn; ll xl[N+5],xll; ll sum[N+5]; vector<ll> e[N+5]; ll lp=1,rp,s,now; ll cntc[N+5],jl[N+5]; ll st[N+5][21],dep[N+5]; inline ll read() { ll x=0,f=1; char c=getchar(); while (c<'0' || c>'9') { if (c=='-') f=-1; c=getchar(); } while (c>='0' && c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } bool cmp(node l1,node l2){ if(blk[l1.l]==blk[l2.l]){ if(blk[l1.r]==blk[l2.r]){ return l1.t<l2.t; } return blk[l1.r]<blk[l2.r]; } return blk[l1.l]<blk[l2.l]; } bool cmpf(node l1,node l2){ return l1.f<l2.f; } ll get(ll col,ll x){ return sum[x]*v[col]; } void dfs(ll x,ll fa){ fst[x]=++dfnn; dfn[dfnn]=x; dep[x]=dep[fa]+1; st[x][0]=fa; xl[x]=++xll; for(ll y:e[x]){ if(y==fa) continue; dfs(y,x); } lst[x]=++dfnn; dfn[dfnn]=x; return; } void add(ll p){ ll x=dfn[p],col=c[x]; jl[x]++; if(jl[x]==2){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void del(ll p){ ll x=dfn[p],col=c[x]; jl[x]--; if(jl[x]==1){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void addd(ll col){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void dell(ll col){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void solve(ll l,ll r,ll t,ll &ans){ while(l<lp){ lp--; add(lp); } while(lp<l){ del(lp); lp++; } while(rp<r){ rp++; add(rp); } while(r<rp){ del(rp); rp--; } while(now<t){ now++; // cout<<l<<"oooooo"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); } while(now>t){ // cout<<l<<"rrrrrrrrr"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); now--; } // cout<<lp<<"ooooooooooooooo"<<rp<<endl; ans=s; return; } ll lca(ll x,ll y){ if(dep[x]<dep[y]) swap(x,y); for(ll i=20;i>=0;i--){ ll xx=st[x][i]; if(dep[xx]>=dep[y]) x=xx; } if(x==y) return x; for(ll i=20;i>=0;i--){ ll xx=st[x][i],yy=st[y][i]; if(xx!=yy) x=xx,y=yy; } return st[x][0]; } int main(){ // freopen("P4074_4.in","r",stdin); n=read(),m=read(),q=read(); lenb=pow(n*2.0,2.0/3.0); ll sx=1; for(ll i=1;i<=2*n;i++){ blk[i]=sx; if(i%lenb==0) sx++; } for(ll i=1;i<=m;i++) v[i]=read(); for(ll i=1;i<=n;i++){ w[i]=read(); sum[i]=sum[i-1]+w[i]; } for(ll i=1;i<n;i++){ ll x,y; x=read(),y=read(); e[x].push_back(y); e[y].push_back(x); } for(ll i=1;i<=n;i++) c[i]=read(); for(ll i=1;i<=q;i++){ ll op; op=read(); if(op==0){ cntb++; b[cntb].x=read(),b[cntb].y=read(); } else{ cnta++; a[cnta].l=read(),a[cnta].r=read(); a[cnta].t=cntb,a[cnta].f=cnta; } } dfs(1,0); for(ll i=1;i<=20;i++){ for(ll j=1;j<=n;j++){ st[j][i]=st[st[j][i-1]][i-1]; // cout<<j<<" "<<i<<" "<<st[4][0]<<endl; } } // for(ll i=1;i<=n;i++){ // cout<<fst[i]<<"ooo"<<lst[i]<<endl; // } // return 0; sort(a+1,a+cnta+1,cmp); for(ll i=1;i<=cnta;i++){ ll x=a[i].l,y=a[i].r,lcaa=lca(x,y),xx,yy,ff=0; if(lcaa==x){ ff=1; xx=fst[x],yy=fst[y]; } else if(lcaa==y){ ff=1; xx=fst[y],yy=fst[x]; } else{ if(xl[x]>xl[y]) swap(x,y); xx=lst[x],yy=fst[y]; } // cout<<xx<<" "<<yy<<" "<<ff<<endl; solve(xx,yy,a[i].t,a[i].ans); if(!ff){ ll col=c[lcaa]; // cout<<lcaa<<endl; a[i].ans+=w[cntc[col]+1]*v[col]; } // printf("%lld\n",i); } sort(a+1,a+cnta+1,cmpf); for(ll i=1;i<=cnta;i++) printf("%lld\n",a[i].ans); return 0; } TLE,求调
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值