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

相关文章

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl