Ground Defense 线段树维护差分数组,求前缀和(线段树维护区间和)

这个大佬的简单很多。

https://blog.csdn.net/Prince_NYing/article/details/89194576

我的做法太麻烦,就是自己吐槽一下

问题 G: Ground Defense

时间限制: 1 Sec  内存限制: 128 MB
提交: 300  解决: 54
[提交] [状态] [命题人:admin]

题目描述

You are a denizen of Linetopia, whose n major cities happen to be equally spaced along an east-west line. In fact, they are often numbered in order from 1 to n, where 1 is the westmost city and n is the eastmost city. 
Linetopia was a lovely place to live until forces from neighboring Trapez invaded. As part of Linetopia’s Shielding Lives and Protecting Citizens initiative, you have been called upon to process information about Trapezoid troop movements so we can determine which cities have been hardest hit and know where to send reinforcements.
Linetopia intelligence has discovered that the Trapezoid forces are attacking in the following pattern. They are sending massive aircraft to drop troops on Linetopia cities. Each aircraft starts at some city i, dropping s soldiers. The aircraft then proceeds to fly either east or west. Each time it flies over another city, it drops a more soldiers than it dropped on the previous city it passed. After performing d drops, the aircraft returns to Trapez to resupply.
You will be receiving intel updates that inform you of the specs of each Trapezoid aircraft passing over Linetopia. You want to answer queries that ask how many Trapezoid troops have been dropped on a particular city. Are you up to the task?

 

输入

The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains two integers: m (1 ≤ m ≤ 10,000), the number of updates and queries and n (1 ≤ n ≤ 500,000), the number of cities in Linetopia.
The next m lines of input are either updates or queries. Update lines begin with a capital U, then contain either a capital E (east) or W (west) to indicate direction, and then contain four integers i (1 ≤ i ≤ n), s (1 ≤ s ≤ 10,000), a (0 ≤ a ≤ 10,000), and d (1 ≤ d ≤ n). These integers signify the starting city, the starting number of soldiers, the increase in soldiers per city, and the number of drops, respectively. You can assume d never results in an aircraft flying to the west of city 1 or to the east of city n.
Query lines begin with a capital Q, and then contain a single integer i (1 ≤ i ≤ n) indicating the city being queried.

 

输出

For each query in the input, output a single line containing the number of Trapezoid troops dropped in that city.

 

样例输入

复制样例数据

1
8 3
U E 1 5 2 3
Q 1
Q 2
Q 3
U W 3 10 10 2
Q 1
Q 2
Q 3

样例输出

5
7
9
5
27
19

提示

Two aircrafts fly over Linetopia. The first starts at city 1 and heads east. It drops 5 soldiers on city 1, 7
soldiers on city 2, and 9 soldiers on city 3. The second starts at city 3 and flies west. It drops 10 soldiers on
city 3 and 20 soldiers on city 2.

 

思路:读懂题意后 发现对区间操作  立即推  可能是线段树 ,从起点向后是 递增的等差数列 每个城市人数不一样,立即推,不能区间操作,,突然发现, 差分数组可以存储公差,在这个区间内 都是 加 d ,立即推 线段树维护差分数组。(我想到这里就停下了。)(剩下的超哥和 xyx 他们队伍的想法)

但是查询怎么办?   题意是单点查询,突然想到   差分数组的前缀和就是该点的 实际人数,

那么,线段树维护区间和,再求一个  1 到 q 的区间和 就是 q点的实际人数了。  

#include
using namespace std;
const int C= 500001;
long long ad[4*C+10];
long long sum[4*C+10];
long long  sum_q;

void maintain(int o,int l,int r){

    sum[o]=0;
    if(lR) return ;
    if(L<=l && r<=R)
    {
        ad[o]+=a;
        maintain(o,l,r);
        return ;
    }
    int m=(l+r)/2;
    if(L<=m)
    updateE(o*2,l,m,L,R,a);
    if(m

 

你可能感兴趣的:(#,区间信息的维护与查询)