求助站外题
  • 板块题目总版
  • 楼主lighthouse
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/12/12 11:11
  • 上次更新2023/11/3 22:24:15
查看原帖
求助站外题
218180
lighthouse楼主2021/12/12 11:11

https://vjudge.net/problem/POJ-3468

样例过了,交上去WA

CODE:

#include <iostream>

#include <stdio.h>

using namespace std;

inline long long read(){
	long long x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9'){
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9'){
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

int a[100010];

long long d1[100010], d2[100010];

long long n, m;

int lowbit(int x){ 
	return x & (-x);
}

void add(int x, int k){
	for(int i = x;i <= n;i += lowbit(i)){ 
		d1[i] += k;
		d2[i] += k * (x - 1);
	}
}

long long sum(int x){
	long long tot = 0;
	for(int i = x;i > 0;i -= lowbit(i)) 
	tot = tot + x * d1[i] - d2[i];
	return tot;
}

int main(){
	n = read();
	m = read();
	for(long long i = 1;i <= n;i++){ 
		a[i] = read();
		int p = a[i] - a[i - 1];
		add(i, p);
	}
	for(long long i = 1;i <= m;i++){ 
		char ch;
		int x, y, z;
		cin >> ch;
		if(ch == 'Q'){
			x = read();
			y = read();
			cout << sum(y) - sum(x - 1) << endl;
		}
		else{
			x = read();
			y = read();
			z = read();
			add(x, z); 
			add(y + 1, -z);
		}
	}
	return 0;
}

求助,急

2021/12/12 11:11
加载中...