Ground Defense

题目描述

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.

 

题意:

给你两种命令,U是从起点开始增加一个值每经过一个点就多增加另一个值,在告诉你长度,可以把他看成等差数列,用一个结构体存一下,Q是查询城市的坐标,查询的时候,我们就遍历等差数列的结构体数组,符合要求的就加上,然后输出结果。

AC

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#include<cmath>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sca4(x,y,z,a) scanf("%lld%lld%lld%lld",&x,&y,&z,&a)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>

typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;
// const double pi = acos(-1);

using namespace std;
struct node
{
  ll a;
  int flag;
  ll d;
  ll pos;
  ll cnt;
}p[maxn];
char op[5];
int main()
{
    int t;
    sca(t);
    while(t--)
    {
      int ans = 0;
      int n,m;
      sca2(m,n);
      rep(j,0,m)
      {
        scanf("%s",op);
        if(op[0] == 'U')
        {
          scanf("%s",op);
          if(op[0] == 'E')
          {
            sca4(p[ans].pos,p[ans].a,p[ans].d,p[ans].cnt);
            p[ans].cnt--;
            p[ans].flag = 1;
            ans++;
          }
          else
          {
            sca4(p[ans].pos,p[ans].a,p[ans].d,p[ans].cnt);
            p[ans].cnt --;
            p[ans].flag = 0;
            ans++;
          }
        }
        else
        {
          ll pos;
          ll sum = 0;
          scl(pos);
          rep(i,0,ans)
          {
            if(p[i].flag && pos >= p[i].pos && pos <= p[i].pos + p[i].cnt)
            {
              sum += p[i].a + p[i].d *(pos - p[i].pos);
            }
            else if(!p[i].flag && pos<=p[i].pos && pos>= p[i].pos - p[i].cnt)
            {
              sum += p[i].a + p[i].d*(p[i].pos - pos);
            }
          }
          prl(sum);
        }
      }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值