#include<stdio.h>//uncle-lu
#include<iostream>
using namespace std;
//我们对修改的部分进行差分,每次操作只会用两次操作,降低了一维的时间复杂度。
int line[10010];
int change[10010];//修改的部分的差分
int n, q, p;
int main()
{
//输入n组数据
cout << "请输入n" << endl;
scanf_s("%d", &n);
for (int i = 1; i <= n; ++i)
scanf_s("%d", &line[i]);
//输入q组数据
cout << "请输入q" << endl;
scanf_s("%d", &q);
for (int i = 1; i <= q; ++i)
{
int a, b, val;
scanf_s("%d %d %d", &a, &b, &val);
for (int j = a; j <= b; j++) //用change数组记录修改的过程
change[j] += val;
}
for (int i = 1; i <= n; ++i)
line[i]+=change[i];//line数组加上change数组得到修改过的数组
cout << "请输入p:" << endl; //查询p组数据
scanf_s("%d", &p);
for (int i = 1; i <= p; ++i)
{
int temp;
scanf_s("%d", &temp);
printf("%d\n", line[temp]);
}
system("pause");
return 0;
}
C++解决 区间问题
于 2022-07-07 19:05:46 首次发布