E - A Simple Problem with Integers

E - A Simple Problem with Integers
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

线段树功能:update:成段增减 query:区间求和
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL long long
const int maxn = 111111;
LL add[maxn<<2];
LL sum[maxn<<2];
void PushUp(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
    if (add[rt])
    {
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];
        sum[rt<<1] += add[rt] * (m - (m >> 1));
        sum[rt<<1|1] += add[rt] * (m >> 1);
        add[rt] = 0;
    }
}
void build(int l,int r,int rt)
{
    add[rt] = 0;
    if (l == r)
    {
        scanf("%lld",&sum[rt]);
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        add[rt] += c;
        sum[rt] += (LL)c * (r - l + 1);
        return ;
    }
    PushDown(rt , r - l + 1);
    int m = (l + r) >> 1;
    if (L <= m) update(L , R , c , lson);
    if (m < R) update(L , R , c , rson);
    PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    PushDown(rt , r - l + 1);
    int m = (l + r) >> 1;
    LL ret = 0;
    if (L <= m) ret += query(L , R , lson);
    if (m < R) ret += query(L , R , rson);
    return ret;
}
int main()
{
    int N , Q;
    scanf("%d%d",&N,&Q);
    build(1 , N , 1);
    while (Q --)
    {
        char op[2];
        int a , b , c;
        scanf("%s",op);
        if (op[0] == 'Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(a , b , 1 , N , 1));
        }
        else
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a , b , c , 1 , N , 1);
        }
    }
    return 0;
}


<think>好的,我现在需要帮用户翻译一个关于图论的题目。首先,用户之前让我翻译过汉明距离的问题,现在又来一个更复杂的关于森林的最小删边问题。用户可能是正在准备编程竞赛或者学习算法,需要准确理解题目要求。 首先,我要仔细阅读用户提供的英文内容,确保理解每一个术语和条件。题目要求的是将给定的无向图变成森林所需要删除的最小边数。森林的定义是不含任何环的图,也就是所有连通分量都是树的结构。树的性质是边数等于顶点数减一,所以每个连通分量的边数超过顶点数减一时,就需要删除多余的边。 接下来,我需要将问题准确翻译成中文,同时注意保持技术术语的正确性。例如,"simple undirected graph"要翻译为“简单无向图”,"forest"对应“森林”。还要注意数学符号和格式,比如变量用斜体,公式使用LaTeX,并且符合之前用户指定的系统级指令中的格式要求,比如行内公式用$...$,独立公式用$$...$$。 然后,题目中的约束条件也需要准确处理。比如顶点数N的范围是1到2e5,边数M的范围是0到min(N(N-1)/2, 2e5),并且每条边的两个顶点u_i和v_i满足1≤u_i<v_i≤N。这些约束条件需要清晰列出,方便解题时考虑算法的时间复杂度和数据结构的选择。 另外,用户可能希望翻译后的内容结构清晰,包含问题描述、关键术语解释、示例和实现建议,就像之前处理汉明距离的问题一样。因此,我需要组织内容,分点说明,可能还需要给出解题思路,比如使用并查集(Union-Find)来检测环,从而计算需要删除的边数。 需要注意用户提到的输入约束中的格式问题,比如原英文中的数学表达式可能有排版错误,例如“0≤M≤min( 2 N(N−1) ,2×10 5 )”应该正确翻译为“0 ≤ M ≤ min( N(N-1)/2 , 2×10^5 )”。这里要确保所有的数学公式都正确无误,使用合适的LaTeX语法。 最后,确保整个翻译符合用户之前提供的格式要求,比如使用中文回答,正确应用行内和独立公式的格式,结构清晰,分点明确。可能还需要添加示例和实现建议,帮助用户更好地理解如何解决这个问题。</think>**题目描述** 给定一个简单无向图,包含$N$个顶点(编号为$1$到$N$)和$M$条边。其中第$i$条边连接顶点$u_i$和$v_i$。 求需要删除的**最小边数**,使得剩余的图成为一个**森林**。 --- ### 关键术语说明 1. **森林** 定义为不含任何**环**的简单无向图。其等价于由若干棵树(连通无环子图)组成的集合。 - 树的性质:含$k$个顶点的树有且仅有$k-1$条边。 2. **简单无向图** - 无自环(边连接相同顶点) - 无重复边(任意两顶点间最多一条边) --- ### 输入约束 - $N$:顶点数,满足$1 \leq N \leq 2 \times 10^5$ - $M$:边数,满足$0 \leq M \leq \min\left(\frac{N(N-1)}{2}, 2 \times 10^5\right)$ - 边定义:$1 \leq u_i < v_i \leq N$ - 输入保证图是简单图,且所有值为整数。 --- ### 解题思路 1. **森林的边数限制** 若图最终成为森林,其总边数为$N - C$,其中$C$为连通分量个数。因此需删除的边数为: $$ \text{最小删除边数} = M - (N - C) $$ 2. **连通分量检测** 使用**并查集(Union-Find)**算法统计连通分量数$C$,并记录合并失败次数(即检测到的环数)。 - 每检测到一次合并失败(形成环),需删除至少一条边。 --- ### 示例 **输入** $N=4$, $M=5$,边集合: $$ \{(1,2), (1,3), (2,3), (2,4), (3,4)\} $$ **输出** $2$(原图含2个环,需删除2条边) --- ### 实现代码(Python) ```python def min_edges_to_forest(): import sys input = sys.stdin.read data = input().split() N = int(data[0]) M = int(data[1]) parent = list(range(N+1)) def find(u): while parent[u] != u: parent[u] = parent[parent[u]] u = parent[u] return u cycles = 0 ptr = 2 for _ in range(M): u = int(data[ptr]) v = int(data[ptr+1]) ptr += 2 pu, pv = find(u), find(v) if pu == pv: cycles += 1 else: parent[pv] = pu # 计算连通分量数C C = sum(1 for i in range(1, N+1) if parent[i] == i) return M - (N - C) # 等价于 cycles + (C_initial - C) ``` --- ### 时间复杂度 - **并查集操作**:近似$O(M \cdot \alpha(N))$,其中$\alpha$为阿克曼函数的反函数,可视为常数。 - 总复杂度:$O(N + M)$,适用于大规模输入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值