Adjustment Office 暴力模擬

丶di發表於2020-10-07

在牛客上做的gym某一水題
Adjustment Office

題目描述

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.

輸入描述

The first line of the input contains two integers n and q ( 1 ≤ n ≤ 1 0 6 , 1 ≤ q ≤ 1 0 5 1 ≤ n ≤ 10^6, 1 ≤ q ≤ 10^5 1n106,1q105) — 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).

輸出描述

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

題意

有一個n*n的矩陣,任意格子(x,y)的大小為x+y。
現可以查詢q次,查詢某行或某列的和,輸出後再把該行或該列全部置零。

題解

因為 1 ≤ n ≤ 1 0 6 1≤n≤10^6 1n106,所以可以設一個 m a t [ 2 ] [ 1 0 6 + 1 ] mat[2][10^6+1] mat[2][106+1]的陣列,分別表示行列是否被查詢置零過,維護第一行和第一列的和,答案是再加上當前行或列相對第一行或列的大小*當前還剩下多少行或列未被查詢置零。

#pragma GCC optimize(2)
#include<bits/stdc++.h> 
using namespace std;
#define ll long long
#define endl "\n"
const int MAX=1e6+7;
ll mat[2][MAX],sum_row,sum_col,nr,nc;
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
	ll n,q;
	cin>>n>>q;
		memset(mat,sizeof(mat),0);
		nr=nc=n;
		sum_row=sum_col=n*(n+1)/2; //第一行或列的和
		char s[5];ll d;	
		while(q--){
			cin>>s>>d;
			if(s[0]=='R'){
				if(mat[0][d])cout<<0;
				else {
					mat[0][d]=1;
					cout<<d*nr+sum_row; 
					nc--;  //當前還有nc列不是空白
					sum_col-=d; //當前第一列的和
				}
			}
			else if(s[0]=='C'){
				if(mat[1][d])cout<<0;
				else {
					mat[1][d]=1;
					cout<<d*nc+sum_col;
					nr--; //當前還有nr行不是空白
					sum_row-=d; //當前第一行的和
				}
			}
			cout<<endl; 
		}
   return 0;
}