您的位置:首页 > 其它

Gym 100851A Adjustment Office 解题报告

2016-09-16 18:59 579 查看
Problem A. Adjustment Office
ACM ICPC 2015–2016, Northeastern European Regional Contest.

Input file: adjustment.in
Output file: adjustment.out

Garrison and Anderson are working in a company named “Adjustment Office”. In competing companies workers change the reality, in this company they try to predict the future.
They are given a big square board n ×n. Initially in each cell (x, y) of this board the value of x + y is written (1≤ x, y ≤ n). They know that in the future there will be two types of queries on the board:
• “R r” — sum up all values in row r,print the result and set all values in row r to zero;
• “C c” — sum up all values in column c, print the result and set all values in column c to zero.
They have predicted what queries and results there will be. They need to ensure that they have correctly predicted the results. Help them by computing the results of the queries.

Input
The first line of the input contains two integers n and q (1 ≤ n ≤ 106 , 1 ≤ q ≤ 105 ) — the size of the square and the number of queries.
Each of the next q lines contains the description of the query. Each query is either “R r” (1 ≤ r ≤ n) or “C c” (1 ≤c ≤ n).

Output
The output file shall contain q lines.The i-th line shall contain one integer — the result of the i-th query.

Sample input and output

adjustment.in

3 7
R 2
C 3
R 2
R 1
C 2
C 1
R 3

adjustment.out  
12
10
0
5
5
4
0
 

区域赛的签到题,但做出来还是很令人愉快。
每次操作输出对应的格子中数值的和。

如果这个操作已经进行过,依题意和一定是零。如果没有,可先假设该操作不受之前操作的干扰,使用加法结合律与等差数列求和公式求出一个和值,而后减去由于之前操作已经数值清零的格子的原值的和值。被清零的格子原值的和,可用加法结合律分成两部分。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#define maxn 1000005
typedef long long LL;
using namespace std;

LL rsum=0,csum=0;
LL rnum=0,cnum=0;
LL r[maxn],c[maxn];

int main()
{
LL n,q;
freopen("adjustment.in", "r", stdin);
freopen("adjustment.out", "w", stdout);
//重定向标准输入输出
scanf("%lld%lld",&n,&q);
memset(r, 0, sizeof(r));
memset(c, 0, sizeof(c));
getchar();
for(LL i=1;i<=q;i++)
{
char w=getchar();
LL o;
scanf("%lld",&o);
getchar();
LL ans=0;
if(w=='R'&&!r[o])
{
ans=n*o+n*(n+1)/2-o*cnum-csum;
//减去由于之前的操作,已经清零的格子的值
rnum++;
rsum+=o;
r[o]=1;
//标记已对此行进行过R操作
}
else if (w=='C'&&!c[o])
{
ans=n*o+n*(n+1)/2-o*rnum-rsum;
//减去由于之前的操作,已经清零的格子的值
cnum++;
csum+=o;
c[o]=1;
//标记已对此列进行过C操作
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息