A Simple Problem with Integers

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
   
Case Time Limit: 2000MS

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 a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" 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.

Source

/**
题意:
   给出一列数,提供两个操作:1.输出区间[l, r]所有书的和; 2.将区间[l, r]所有数加上C。


   与上一道相同,也是使用Lazy标记。但是,这道题还要实现多次查询。
   在查询时,也需要像成段时那样将lazy标记一直向下更新,并且,需要将维护的和同步更新.
**/
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long  LL;
typedef unsigned long long ull;

int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};//up down left right
bool inmap(int x,int y,int n,int m){if(x<1||x>n||y<1||y>m)return false;return true;}
int hashmap(int x,int y,int m){return (x-1)*m+y;}

#define eps 1e-8
#define inf 0x7fffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define Mem(a,x) memset(a,x,sizeof(a))
#define maxn 100005

LL col[maxn<<2];//延迟标记
LL sum[maxn<<2];//需要维护的值

void PushUp(int rt)//向上更新需要维护的值
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void PushDown(int rt,int len)//向下传递延迟标记
{
    if(col[rt])
    {
        col[rt<<1]  +=col[rt];//在这里延迟标记成为了可以累加的和.左子树累加
        col[rt<<1|1]+=col[rt];//右子树累加
        sum[rt<<1]  +=(len-(len>>1))*col[rt];//更新左子树sum
        sum[rt<<1|1]+=(len>>1)*col[rt];//更新右子树sum
        col[rt]=0;//删除标记
    }
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%lld",&sum[rt]);//利用递归,可以按照顺序进行赋值
        col[rt]=0;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int L,int R,int key,int l,int r,int rt)
{
    int len=(r-l+1);
    if(L<=l&&r<=R)
    {
        col[rt]+=key;//标记累加
        sum[rt]+=len*key;//更细当前的sum
        return ;
    }
    PushDown(rt,len);//标记传递
    int m=(l+r)>>1;
    if(L<=m)
        update(L,R,key,lson);
    if(R>m)
        update(L,R,key,rson);
    PushUp(rt);//更新sum
}

LL query(int L,int R,int l,int r,int rt)
{
    int len=(r-l+1);
    if(L<=l&&r<=R)
    {
        return sum[rt];//查到了sum,返回
    }
    PushDown(rt,len);//标记传递
    int m=(l+r)>>1;
    LL ans=0;
    if(L<=m)//与左子树的区间有交集
        ans+=query(L,R,lson);
    if(R>m)//与右子树的区间有交集
        ans+=query(L,R,rson);
    PushUp(rt);//因为标记已经传递,传递的过程中素描也进行了更新,因此需要更新rt的sum
    return ans;
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,n,1);//建树
    for(int i=0;i<m;i++)
    {
        char s[3];
        scanf("%s",s);
        if(s[0]=='Q')//查询
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%lld\n",query(l,r,1,n,1));
        }
        else
        {
            int l,r,key;
            scanf("%d%d%d",&l,&r,&key);
            update(l,r,key,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、付费专栏及课程。

余额充值