Gym100851A Adjustment Office + analog thinking

URL: https://codeforces.com/gym/100851

Meaning of the questions:

You give a $ n * n $ grid graph, the value of the lattice $ (x, y) $ is $ x + y $, there are two modes of operation:

$ R $ $ c $: $ output of $ R & lt columns and then the list of all the numbers to zero.

$ C $ $ c $: $ C $ output of the first row and then all of this line number zero.

A total of $ q $ operations.

$ N \ leq 1e6, q \ leq 1e5 $.

answer:

The problem we consider the first column of each row of each pretreatment of the trellis diagram and the one by one violence seeking $ O (n ^ 2) $ timeout will be considered to obtain the first row and first column, the next line is clearly and to this line and add $ n $. $ O (n) $ delivery can launch results. Then consider a single operation. If this column is empty, output $ 0 $ to, if not, to see how many rows and is already $ 0 $, the actual and this column is: the original calculated and $ - $ (and $ 0 $ the number of rows number of rows and columns of the $ + $ * $ $ column number and line is $ 0 $), and then put this column and referred to as $ 0 $. Then recorded. The same operation two.

Note: All values ​​calculated are required to participate in $ long $ $ long $, otherwise it will overflow.

AC Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
vector<ll>rd, cd;
ll rdsum, cdsum;
ll rsum[N], csum[N];
int main()
{
	freopen("adjustment.in", "r", stdin);
	freopen("adjustment.out", "w", stdout);
	ll n, q;//这里不用ll应该也可,但是不值得冒这个险
	scanf("%lld%lld", &n, &q);
	rsum[1] = (2ll + n + 1ll) * n / 2;
	for (int i = 2; i <= n; ++i)
		rsum[i] = rsum[i - 1] + n;
	for (int i = 1; i <= n; ++i)
		csum[i] = rsum[i];
	char op[2];
	ll p = 0;//p不用ll会溢出
	for (int i = 1; i <= q; ++i)
	{
		scanf("%s%lld", op, &p);
		if (op[0] == 'R')
		{
			if (!rsum[p])
				printf("0\n");
			else
			{
				rdsum += p;
				printf("%lld\n", rsum[p] - (ll)cd.size() * p - cdsum);
				rsum[p] = 0;
				rd.push_back(p);
			}
		}
		else if (op[0] == 'C')
		{
			if (!csum[p])
				printf("0\n");
			else
			{
				cdsum += p;
				printf("%lld\n", csum[p] - (ll)rd.size() * p - rdsum);
				csum[p] = 0;
				cd.push_back(p);
			}
		}
	}
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/Aya-Uchida/p/12588727.html