Gym 100851AAdjustment Office 解题报告

来源:互联网 发布:中级经济师题库软件 编辑:程序博客网 时间:2024/06/07 18:42


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




0 0
原创粉丝点击