Adjustment Office 暴力模擬
在牛客上做的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 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).
輸出描述
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 1≤n≤106,所以可以設一個 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;
}
相關文章
- 天才模擬器程式設計師死於網路暴力中程式設計師
- 模擬
- 重灌 Homestead 虛擬機器 暴力解決難題虛擬機
- 2024.11.20 NOIP模擬 - 模擬賽記錄
- 10.6 模擬賽(NOIP 模擬賽 #9)
- 有限元模擬 有限體積模擬
- 模擬賽
- 模擬題
- ACP模擬
- git 模擬Git
- Altair SimSolid 工程模擬軟體 衡祖模擬AISolid
- Gpssworld模擬(二):並排排隊系統模擬
- 「模擬賽」暑期集訓CSP提高模擬15(8.7)
- 「模擬賽」暑期集訓CSP提高模擬5(7.22)
- 「模擬賽」暑期集訓CSP提高模擬6(7.23)
- 「模擬賽」暑期集訓CSP提高模擬10(7.28)
- 「模擬賽」暑期集訓CSP提高模擬3(7.20)
- 11.26 模擬賽
- noip模擬21
- 20241016 模擬賽
- noip模擬15
- 11.11模擬賽
- noip模擬14
- 模擬賽 2
- 1114模擬賽
- 11.27 模擬賽
- 11.22 模擬賽
- 11.21 模擬賽
- noip模擬17
- noip模擬19
- 11.23 模擬賽
- 1123模擬賽
- 1101模擬賽
- noip模擬3
- noip模擬4
- 11.3 模擬賽
- 2024.11.2 模擬賽
- 10.30 模擬賽