• CF701 B. Cells Not Under Attack


    题目传送门:https://codeforces.com/problemset/problem/701/B

    题目大意:
    给定一个(n imes n)的棋盘,共有(m)次操作,每次操作会在棋盘的((x,y))处放置一个城堡(国际象棋),问每次操作后不会被攻击的格子总数


    每次放置城堡只会影响到一整行和一整列,故我们用两个数组记录行和列的放置情况

    每次加入新的城堡的时候进行判断即可

    /*program from Wolfycz*/
    #include<map>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Fi first
    #define Se second
    #define ll_inf 1e18
    #define MK make_pair
    #define sqr(x) ((x)*(x))
    #define pii pair<int,int>
    #define int_inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline T frd(T x){
    	int f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    template<typename T>inline T read(T x){
    	int f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=1e5;
    bool VisR[N+10],VisC[N+10];
    //R(Row)表示行的占用情况,C(Column)表示列的占用情况
    int main(){
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	int n=read(0),m=read(0);
    	int totR=n,totC=n; ll All=1ll*n*n;
        //totR,totC表示剩余未放置城堡的行数和列数
    	for (int i=1;i<=m;i++){
    		int x=read(0),y=read(0);
    		if (VisR[x]&&VisC[y]){//行和列都已放置了城堡
    			printf("%lld
    ",All);
    			continue;
    		}
    		if (!VisC[y])	All-=totR;
    		if (!VisR[x])	All-=totC;
    		if (!(VisR[x]^VisC[y]))	All++;	//(x,y)算重,得减去一次
    		totR-=!VisR[x],totC-=!VisC[y];
    		VisR[x]=VisC[y]=1;
    		printf("%lld%c",All,i==m?'
    ':' ');
    	}
    	return 0;
    }
    
    作者:Wolfycz
    本文版权归作者和博客园共有,欢迎转载,但必须在文章开头注明原文出处,否则保留追究法律责任的权利
  • 相关阅读:
    PHP 构造方法 __construct()(转)
    PHP笔记
    php的print_r第二个参数是true有啥用啊
    如何给类或方法规范地注释
    详解spl_autoload_register()  函数(转)
    PHP中文网上的分页代码
    echo 0000
    [csu/coj 1619] 递归
    [csu/coj 1083]贪心
    [csu/coj 1078]多个序列的最长公共子序列
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/14960521.html
Copyright © 2020-2023  润新知