Gym 100851A Adjustment Office 解题报告


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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值