样例没有过求调
查看原帖
样例没有过求调
701202
high_sky楼主2024/12/2 12:53

下面是代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#define N 500005
#define lowbit(x) ((x) & (-(x)))
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
using namespace std;
int n,m,a[N];
int gcd(int a,int b) {
	a = abs(a),b = abs(b);
	return b ? gcd(b,a % b) : a;
}
int arr[N],tr[N << 2];
void update(int x,int val) {
	for (;x <= n;x += lowbit(x)) arr[x] += val;
}
int query(int x) {
	int res = 0;
	for (;x;x -= lowbit(x)) res += arr[x];
	return res;
}
void pushup(int x) {
	tr[x] = gcd(tr[ls(x)],tr[rs(x)]);
}
void update2(int x,int l,int r,int pos,int val) {
	if (l == r) {
		tr[x] += val;
		return;
	}
	int mid = l + r >> 1;
	if (pos <= mid) update2(ls(x),l,mid,pos,val);
	else update2(rs(x),mid + 1,r,pos,val);
	pushup(x);
}
int query2(int x,int l,int r,int L,int R) {
	if (l > R || r < L) return 0;
	if (L <= l && r <= R) return tr[x];
	int mid = l + r >> 1;
	return gcd(query2(ls(x),l,mid,L,R),query2(rs(x),mid + 1,r,L,R));
}
signed main(){
	cin >> n >> m;
	for (int i = 1;i <= n;i ++) cin >> a[i],update2(1,1,n,i,a[i] - a[i - 1]),update(i,a[i] - a[i - 1]);
	for (int l,r,d;m --;) {
		char x;
		cin >> x >> l >> r;
		if (x == 'C') {
			cin >> d;
			update(l,d),update(r + 1,-d);
			update2(1,1,n,l,d),update2(1,1,n,r + 1,-d);
		}
		else cout << gcd(query(l),query2(1,1,n,l + 1,r)) << endl;
	} 
	return 0;
} 

输出的好像是:1 1 4

2024/12/2 12:53
加载中...