zoukankan      html  css  js  c++  java
  • 2020牛客国庆集训派对day7 J. 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 ≤ 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.
    

    示例1

    输入

    复制

    3 7
    R 2
    C 3
    R 2
    R 1
    C 2
    C 1
    R 3
    

    输出

    复制

    12
    10
    0
    5
    5
    4
    0
    

    大意是给定一个n阶方阵,其中a[x] [y]的值为x + y,每次查询的时候先输出查询的行 or 列的和,然后把这行/列的值全部置0,要求输出每次查询的结果。

    注意到每个单元的初始值很特殊。当我们删去一行r的时候,先假设之前的操作没有影响,那么答案就是一个等差数列求和,然后考虑之前删除列操作的累积效果,不妨用ncol表示之前删去的总的列数,col表示之前删去的列标的和,那么累积的影响就是ncol * r + col(不懂的话可以写一下这个矩阵模拟一下),用等差数列的和减去累积的影响输出即可,不要忘记更新ncol和col,同时注意如果之前已经删过这一行的话就不要再删了(vis数组判断记录即可)。

    删去一列的时候同理。

    #include <iostream>
    using namespace std;
    #define int long long
    int row = 0, col = 0, nrow = 0, ncol = 0, n, q;
    bool visc[1000005] = {0}, visr[1000005] = {0};
    signed main()
    {
    	//freopen("data.txt", "r", stdin);
    	cin >> n >> q;
    	for(int i = 1; i <= q; i++)
    	{
    		char c;
    		int num;
    		//scanf("%c%d", &c, &num);
    		cin >> c >> num;
    		int ans = 0, tmp = 0;
    		if(c == 'R')
    		{
    			if(visr[num]) 
    			{
    				cout << 0 << endl;
    				continue;
    			}
    			tmp += n * num + (1 + n) * n / 2;
    			tmp -= (ncol * num + col);
    			ans = tmp;
    			nrow++;
    			row += num;
    			visr[num] = 1;
    		}
    		else
    		{
    			if(visc[num])
    			{
    				cout << 0 << endl;
    				continue;
    			}
    			tmp += n * num + (1 + n) * n / 2;
    			tmp -= (nrow * num + row);
    			ans = tmp;
    			ncol++;
    			col += num;
    			visc[num] = 1;
    		}
    		cout << ans << endl;
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13777630.html
Copyright © 2011-2022 走看看