无聊的数列 线段树差分
题目背景
无聊的YYB总喜欢搞出一些正常人无法搞出的东西。有一天,无聊的YYB想出了一道无聊的题:无聊的数列。。。(K峰:这题不是傻X题吗)
题目描述
维护一个数列{a[i]},支持两种操作:
1、1 L R K D:给出一个长度等于R-L+1的等差数列,首项为K,公差为D,并将它对应加到a[L]~a[R]的每一个数上。即:令a[L]=a[L]+K,a[L+1]=a[L+1]+K+D,
a[L+2]=a[L+2]+K+2D……a[R]=a[R]+K+(R-L)D。
2、2 P:询问序列的第P个数的值a[P]。
输入输出格式
输入格式:
第一行两个整数数n,m,表示数列长度和操作个数。
第二行n个整数,第i个数表示a[i](i=1,2,3…,n)。
接下来的m行,表示m个操作,有两种形式:
1 L R K D
2 P 字母意义见描述(L≤R)。
输出格式:
对于每个询问,输出答案,每个答案占一行。
输入输出样例
输入样例#1: 复制
5 2 1 2 3 4 5 1 2 4 1 2 2 3
输出样例#1: 复制
6
说明
数据规模:
0≤n,m≤100000
|a[i]|,|K|,|D|≤200
Hint:
有没有巧妙的做法?
单点查询,
考虑差分:
对于区间[ L,R ]
在 L处 + k ,( L,R ] 区间+d,R+1处 -k-(R-L)*d;
可见就是区间修改和区间查询的问题;
那么用线段树就行了;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }
/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
struct node {
int sum;
int l, r;
int lazy;
}tree[maxn];
int n, m;
int a[maxn];
void pushup(int rt) {
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
void pushdown(int rt) {
if (tree[rt].lazy) {
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt << 1].sum += (tree[rt << 1].r - tree[rt << 1].l + 1)*tree[rt].lazy;
tree[rt << 1 | 1].sum += (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1)*tree[rt].lazy;
tree[rt].lazy = 0;
}
}
void build(int l, int r, int rt) {
tree[rt].l = l; tree[rt].r = r; tree[rt].lazy = 0;
if (l == r) {
tree[rt].sum = 0;
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1);
pushup(rt);
}
void upd(int l, int r, int rt, int val) {
if (l <= tree[rt].l&&tree[rt].r <= r) {
tree[rt].sum += (tree[rt].r - tree[rt].l + 1)*val;
tree[rt].lazy += val;
return;
}
pushdown(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if (l <= mid)upd(l, r, rt << 1, val);
if (r > mid)upd(l, r, rt << 1 | 1, val);
pushup(rt);
}
int query(int l, int r, int rt) {
if (l <= tree[rt].l&&tree[rt].r <= r) {
return tree[rt].sum;
}
pushdown(rt);
int mid = (tree[rt].r + tree[rt].l) >> 1;
ll ans = 0;
if (l <= mid)ans += query(l, r, rt << 1);
if (mid < r)ans += query(l, r, rt << 1 | 1);
return ans;
}
int main() {
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
for (int i = 1; i <= n; i++)rdint(a[i]);
build(1, n, 1);
while (m--) {
int op; rdint(op);
if (op == 1) {
int l, r, k, d;
rdint(l); rdint(r); rdint(k); rdint(d);
upd(l, l, 1, k);
if(r>l)
upd(l + 1, r, 1, d);
if(r!=n)upd(r + 1, r + 1, 1, -k - (r - l)*d);
}
else {
int x; rdint(x);
cout << a[x] + query(1, x, 1) << endl;
}
}
return 0;
}