codeforces643D

在一个魔法森林中,阿狸寻找着回家的路。森林里充满了糖果,每棵树都有其独特的糖果稠密度。通过一系列操作,阿狸试图找到糖果稠密度最高和最低的树,以便更好地规划他的旅程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阿狸的基环内向树森林

Background

当阿狸醒来的时候,发现自己处在基环内向森林的深处,阿狸渴望离开这个乌烟瘴气的地方。“明天还有与桃子的约会呢”,阿狸一边走一边说,“可是,这个森林的出口在哪儿呢?”

阿狸走啊走,走啊走,就是找不到出口。不知所措的他,突然听到了一个苍老的声音:“这是一片有魔法的密林,这里的树的形态也会时不时的变化,晃晃你的小脑瓜,是不是感觉有水在流动呢?”

Description

阿狸所在的森林有 N 个节点,编号从 1 到 N。每个节点都连出去恰好一条有向边,设 i 号点连出去的点为 A[i]。同时,阿狸发现,A[i]≠i,而且 A[A[i]]≠i。

每个节点上都有一些糖果,第 i 个节点上的糖果数为 B[i]。阿狸定义一个节点的糖果稠密度为 C[i],C[i]求法如下:

假设与 i 距离不超过 1 的点有 D[i]个(包括 i 连出去的点、连向 i 的点以及 i 自己),分别是 P[1]、P[2]…P[D[i]]。

E[i]=B[i]D[i]E[i]=⌊B[i]D[i]⌋,那么C[i]=B[i]D[i]×E[i]+D[i]j=1E[P[j]]C[i]=B[i]−D[i]×E[i]+∑j=1D[i]E[P[j]]

现在阿狸想让你实现一个糖果稠密度分析仪,这个分析仪要支持三种操作:
➢ 1 i j 表示把 A[i]改为 j,保证 j≠i 且 A[j]≠i。
➢ 2 i 表示询问 C[i]的值,即点 i 的糖果稠密度。
➢ 3 表示询问所有节点中最小的 C[i]的值和最大的 C[i]的值。

Input

第一行两个正整数 N 和 Q,表示节点个数和操作个数。
第二行 N 个正整数,第 i 个数表示 B[i]。
第三行 N 个正整数,第 i 个数表示 A[i]。
接下来 Q 行,每行形如 1 i j 或 2 i 或 3 ,表示操作。

Output

有若干行,表示操作 2 和操作 3 的答案。

Sample Input

5 12
10 20 30 40 50
2 3 4 5 2
2 1
2 2
2 3
2 4
2 5
1 4 2
2 1
2 2
2 3
2 4
2 5
3

Sample Output

10
36
28
40
36
9
57
27
28
29
9 57

Data Limitation

对于测试点 1~2,保证 N,Q5×103N,Q≤5×103,1、2、3 操作出现次数均在 Q/3 左右。
对于测试点 3~6,保证 N,Q3×104N,Q≤3×104,1、2、3 操作出现次数均在 Q/3 左右。
对于测试点 7~8,保证没有 2 操作,1、3 操作出现次数均在 Q/2 左右。
对于测试点 9~10,保证没有 3 操作,1、2 操作出现次数均在 Q/2 左右。
对于测试点 11~12,保证任何时候 A[i]≤5,1、2、3 操作出现次数均在 Q/3 左右。
对于测试点 13~14,保证任何时候 A[i]≤100,1、2、3 操作出现次数均在 Q/3 左右。
对于测试点 15~16,保证 B[i]≤100,1、2、3 操作出现次数均在 Q/3 左右。
对于 100%的数据,保证 3N1051Q1051B[i]10121A[i]N3≤N≤105,1≤Q≤105,1≤B[i]≤1012,1≤A[i]≤N。

分析

分析那个糖果稠密度,发现可以拆分成“只与i及其周围点数量有关的式子”+“周围一圈的E”。直觉那个修改操作的变动量很少。

由于要修改A,所以想到把“周围一圈的E”拆分成“连向i的点的E”+“i连到的点的E”,然后大力维护即可。

我们可以把一个节点 i 的糖果稠密度 C[i]分成两部分,第一部分是 A[i]对 C[i]的贡献E[A[i]],第二部分是剩下的点对 i 的贡献 C[i]-E[A[i]],设 F[i]=C[i]-E[A[i]]。

对于一个节点 i,我们维护两个信息,一个是 E[i],另一个是所有连向 i 的点的 F 值所构成的集合(也可以用两个堆来维护),设这个集合为 Son[i]。

对于全局我们维护一个集合 S,S 的构成如下:我们把每个节点 i 的 min(Son[i])+E[i]和 max(Son[i])+E[i]两个值加到集合 S 中。

显然,操作 2 的答案就是 E[A[i]]+F[i],而操作 3 的答案就是 min(S)和 max(S)。

考虑操作 1 怎么维护,把 A[i]的值改成了 j,这个操作会影响的节点是 i、j、A[i]、A[j]、A[A[i]]、A[A[j]]、A[A[A[i]]],其中 i 的 A 发生了改变, A[i]和 j 的 D、E、F 和 Son 发生了改变,于是 A[A[i]]和 A[j]的 F 和 Son 也随之改变,于是 A[A[A[i]]]和 A[A[j]]的 Son 也改变了。所以分别对这七个节点维护即可,顺便再维护一下 S,常数超级大。

总复杂度是 O(NlogN)

以上为题解

 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=100005;
int i,j,k,n,m,q,ch,o,x,y;
int num[N],f[N];
ll t[N],val[N],dev[N],add[N];
multiset <ll> Ans,Son[N];
set <ll>::iterator it;
void rev(int x)
{
    if (Son[x].empty()) return;
    it=Son[x].begin();
    Ans.insert(*it+add[x]);
    it=Son[x].end();it--;
    Ans.insert(*it+add[x]);
}
void del(int x)
{
    if (Son[x].empty()) return;
    it=Son[x].begin();
    Ans.erase(Ans.find(*it+add[x]));
    it=Son[x].end();it--;
    Ans.erase(Ans.find(*it+add[x]));
}
void Rev(int x)
{
    Son[f[x]].insert(val[x]+dev[x]);
}
void Del(int x)
{
    Son[f[x]].erase(Son[f[x]].find(val[x]+dev[x]));
}
int main()
{
    freopen("forest.in","r",stdin);
    freopen("forest.out","w",stdout); 
    R(n);R(q);
    for (i=1;i<=n;i++) R(t[i]);
    for (i=1;i<=n;i++)
    {
        R(f[i]);
        num[f[i]]++;
    }
    for (i=1;i<=n;i++)
    {
        add[i]=t[i]/(num[i]+2);
        val[i]=t[i]-(num[i]+1)*add[i];
        dev[f[i]]+=add[i];
    }
    for (i=1;i<=n;i++) Rev(i);
    for (i=1;i<=n;i++) rev(i);
    for (i=1;i<=q;i++)
    {
        R(o);
        if (o==1)
        {
            R(x);R(y);
            if (f[x]==y) continue;
            del(f[x]);del(f[f[x]]);del(f[f[f[x]]]);
            Del(x);Del(f[x]);Del(f[f[x]]);
            dev[f[x]]-=add[x];num[f[x]]--;
            dev[f[f[x]]]-=add[f[x]];
            add[f[x]]=t[f[x]]/(num[f[x]]+2);
            val[f[x]]=t[f[x]]-(num[f[x]]+1)*add[f[x]];
            dev[f[f[x]]]+=add[f[x]];
            Rev(f[x]);Rev(f[f[x]]);
            rev(f[x]);rev(f[f[x]]);rev(f[f[f[x]]]);
            f[x]=y;
            del(f[x]);del(f[f[x]]);del(f[f[f[x]]]);
            Del(f[x]);Del(f[f[x]]);
            dev[f[x]]+=add[x];num[f[x]]++;
            dev[f[f[x]]]-=add[f[x]];
            add[f[x]]=t[f[x]]/(num[f[x]]+2);
            val[f[x]]=t[f[x]]-(num[f[x]]+1)*add[f[x]];
            dev[f[f[x]]]+=add[f[x]];
            Rev(x);Rev(f[x]);Rev(f[f[x]]);
            rev(f[x]);rev(f[f[x]]);rev(f[f[f[x]]]);
            continue;
        }
        if (o==2)
        {
            R(x);
            Wl(dev[x]+val[x]+add[f[x]]);
            continue;
        }
        it=Ans.begin();
        W(*it);
        it=Ans.end();it--;
        Wl(*it);
    }
}
/*
input
5 12
10 20 30 40 50
2 3 4 5 2
2 1
2 2
2 3
2 4
2 5
1 4 2
2 1
2 2
2 3
2 4
2 5
3
output
10
36
28
40
36
9
57
27
28
29
9 57
*/
View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/11144601.html

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值