【模拟】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.

 

题目大意:

先输入一个整数t,代表有t组测试样例,对于每组样例先输入两个整数m,n,m代表下面m行操作,n代表数组的长度,对于每一行操作,先输入一个字母,'U'代表修改,‘Q’代表查询,对于U操作,先输入一个字母,'E'代表向右,'W'代表向左,后面跟着四个正整数a,b,c,d,代表从数组里面的第a项开始,往左或往右修改d项,对于第一项将其加上b,第二项加上b+c,第三项加上b+2*c,等等,按照等差的形式修改这d项元素。而Q操作即查询经过修改后的元素是多少。

解题思路:

此题只需将所有的修改操作存在一个结构体中,当进行查询时,通过遍历这些操作,判断所需要查询的元素是否在其修改区间内,在就改变元素的值,否则继续。注意:此题需用scanf,printf输入输出,用cin,cout会超时,并且数据类型最好定义为long long,防止出错。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{
	char dir;
	int start;
	ll fir;
	ll d;
	int len;
}arr[500500];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int t;
    scanf("%d",&t);
    while(t--) {
    	char s1[10];
    	char s[10];
    	int cnt=0;
    	int m,n;
    	scanf("%d %d",&m,&n);
    	while(m--) {
    		scanf("%s",s1);
    		if(s1[0]=='U') {
    			scanf("%s",s);
    			int a,d;ll b,c;
    			scanf("%d %lld %lld %d",&a,&b,&c,&d);
    			cnt++;
    			arr[cnt].dir=s[0];
    			arr[cnt].start=a;
    			arr[cnt].fir=b;
    			arr[cnt].d=c;
    			arr[cnt].len=d;
    		}
    		else {
    			int id;
    			scanf("%d",&id);
    			ll ans=0;
    			for(int i=1;i<=cnt;i++) {
    				if(arr[i].dir=='E') {
    					if(arr[i].start<=id&&arr[i].start+arr[i].len-1>=id) {
    						ans=ans+(arr[i].fir+(id-arr[i].start)*arr[i].d);
    					}
    				}
    				else if(arr[i].dir=='W') {
    					if(arr[i].start>=id&&arr[i].start-arr[i].len+1<=id) {
    						ans=ans+(arr[i].fir+(arr[i].start-id)*arr[i].d);
    					}
    				}
    			}
    			printf("%lld\n",ans);
    		}
    	}
    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值