The Trip On Abandoned Railway(线段树+树状数组)

2024-02-01 14:58

本文主要是介绍The Trip On Abandoned Railway(线段树+树状数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:https://ac.nowcoder.com/acm/problem/13891
来源:牛客网

题目描述
There are many ghosts at the abandoned station on unknown railway.

We mark the abandoned stations as 1,2…n according to the order. There are ai ghosts at the ith station.Yakumo Yukari often opens a black hole and makes a train appearing at a certain station. For example, the train appears at the x station, and k ghosts get off at the station. Then there would be k+d ghosts at the x+1 station to get off,k+2×d at x+2 station and so on…There would be k+y∗d ghosts at the x+y station to get off (0≤y,x+y≤n). In others words, the numbers getting off at x,x+1,x+2…n station form a tolerance of d arithmetic progression.(you can consider ghosts getting off at the same time.)Onozuka Komachi would comes a certain station to take away the ghosts.(The number of ghosts at the station would become 0)You have the records of trains appearing and Komachi coming. You should tell Komachi how much ghosts at a certain station when she come to there.
输入描述:
The first line contains an positive integer T(1≤T≤10), represents there are T test cases.
For each test case:
The first line contains three positive integers n,m,d(1≤n≤105,1≤m≤105,1≤d≤1000) - the number of station,the number of records,and the tolerance of the arithmetic progress.

The second line contains n integers a1,a2…an(1≤ai≤1000). Then m lines followed. Each line contains a records and there are two types. 1 x y,indicating train appearing at x station and y ghosts geting off. 2 x y,indicating Komachi coming to the x station. (1≤x≤n,0≤y≤1000)
输出描述:
For each second records(2 x), output an integer in one line, representing the number of ghosts at the station.Since the ans may be too large, out put tme ans mod 109+7.
示例1
输入
复制
2
6 6 1
1 2 3 3 2 1
1 1 1
2 1
2 2
2 3
2 4
2 5
5 3 2
1 2 3 4 5
1 3 0
2 4
2 4
输出
复制
2
4
6
7
7
6
0
说明
There lists the number of ghosts changing at these station.
case1:
1 2 3 3 2 1
2 4 6 7 7 7
0 4 6 7 7 7
0 0 6 7 7 7
0 0 0 7 7 7
0 0 0 0 7 7
0 0 0 0 0 7
case2:
1 2 3 4 5
1 2 3 6 9
1 2 3 0 9
1 2 3 0 9
题意:题目大意:给你一个长度为n的数列和一个公差d,然后m个操作,操作分为两种,第一种操作有一个x和y,代表从x开始的每个数按照等差数列开始加,x这个位置加上y,x+1这个位置加上y+d,x+2这个位置加上y+2*d,依次递推;第二种操作有一个x,代表把这个位置的数模1e9+7后输出,并且这个位置变成零。
思路:挺巧妙的一种思路。
对于1操作来说,x~n都会加上y,我们可以用树状数组或者线段树对x ~n加上y。对于加上的d,跟位置有关系。那么我们在另一棵线段树上对[x+1,n]都加上1,然后在求的时候,求[1,x]上1的个数,就相当于加上了多少个d。这个题目有个坑点,就是只对答案取模就可以,不用对其余的取模。
线段树+树状数组

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;const int maxx=1e5+100;
struct node{int l;int r;ll sum;ll lazy1;
}p[maxx<<2];
ll a[maxx];
ll c[maxx];
int n,m;ll d;
/*-----------树状数组------------*/
inline int lowbit(int x){return x & -x;}
inline void add(int cur,ll v)
{while(cur<maxx){c[cur]=(c[cur]+v)%mod;cur+=lowbit(cur);}
}
inline ll Query(int cur)
{ll ans=0;while(cur>0){ans=(ans+c[cur])%mod;cur-=lowbit(cur);}return ans;
}
/*------------线段树-------------*/
inline void pushup(int cur)
{p[cur].sum=(p[cur<<1].sum+p[cur<<1|1].sum);
}
inline void pushdown(int cur)
{if(p[cur].lazy1){p[cur<<1].lazy1=(p[cur<<1].lazy1+p[cur].lazy1);p[cur<<1|1].lazy1=(p[cur<<1|1].lazy1+p[cur].lazy1);p[cur<<1].sum=(p[cur<<1].sum+(p[cur<<1].r-p[cur<<1].l+1)*1ll*p[cur].lazy1);p[cur<<1|1].sum=(p[cur<<1|1].sum+(p[cur<<1|1].r-p[cur<<1|1].l+1)*1ll*p[cur].lazy1);p[cur].lazy1=0;}
}
inline void build(int l,int r,int cur)
{p[cur].l=l;p[cur].r=r;p[cur].lazy1=p[cur].sum=0;if(l==r) return ;int mid=l+r>>1;build(l,mid,cur<<1);build(mid+1,r,cur<<1|1);
}
inline void update(int l,int r,ll v,int cur)
{int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r){p[cur].lazy1=(p[cur].lazy1+v);p[cur].sum=(p[cur].sum+(R-L+1)*v);return ;}int mid=L+R>>1;pushdown(cur);if(r<=mid) update(l,r,v,cur<<1);else if(l>mid) update(l,r,v,cur<<1|1);else update(l,mid,v,cur<<1),update(mid+1,r,v,cur<<1|1);pushup(cur);
}
inline ll query(int l,int r,int cur)
{int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r) return p[cur].sum;pushdown(cur);int mid=L+R>>1;ll ans;if(r<=mid) return query(l,r,cur<<1);else if(l>mid) return query(l,r,cur<<1|1);else return (query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1));return ans;
}
int main()
{int t,op,x;ll y;scanf("%d",&t);while(t--){memset(c,0,sizeof(c));scanf("%d%d%lld",&n,&m,&d);for(int i=1;i<=n;i++) scanf("%lld",&a[i]);build(1,n,1);while(m--){scanf("%d",&op);if(op==1){scanf("%d%lld",&x,&y);add(x,y);update(x+1,n,1,1);}else{scanf("%d",&x);ll ans=(Query(x)+query(1,x,1)*d*1ll+a[x]);printf("%lld\n",ans%mod);a[x]-=ans;}}}return 0;
}

努力加油a啊,(o)/~

这篇关于The Trip On Abandoned Railway(线段树+树状数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/667604

相关文章

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.