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
区域赛的签到题,但做出来还是很令人愉快。
每次操作输出对应的格子中数值的和。
如果这个操作已经进行过,依题意和一定是零。如果没有,可先假设该操作不受之前操作的干扰,使用加法结合律与等差数列求和公式求出一个和值,而后减去由于之前操作已经数值清零的格子的原值的和值。被清零的格子原值的和,可用加法结合律分成两部分。
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; }
相关文章推荐
- Gym 100694 解题报告
- Gym 100851E Easy Problemset 解题报告
- POJ 1009不算解题报告的解题报告
- POJ 1046(颜色映射 简单数学) 解题报告
- hdoj2001解题报告
- HDU1864解题报告-dp
- [leetcode] 61. Rotate List 解题报告
- 1034. Head of a Gang (30)解题报告
- leetcode解题报告18. 4Sum
- 人民大学云计算编程的网上评估平台--解题报告 1001-1003
- zoj 3164 Cookie choice 解题报告
- 解题报告 之 UVA11054 Wine trading in Gergovia
- 20161022 NOIP模拟赛 解题报告
- 【LeetCode】27.Remove Element(Easy)解题报告
- 贪心思想题的解题报告
- HDU 1.2.2解题报告
- [leetcode] 109. Convert Sorted List to Binary Search Tree 解题报告
- 20161022 NOIP模拟赛 T1 解题报告
- 第九届蓝桥杯选拔赛C语言组 解题报告索引
- hdu2006解题报告