一、题意

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.
White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle [x1[i]...x2[i]][y1[i]...y2[i]].
White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

简单的说就是,给定一个大型矩形,矩形中每个格子对应一个单独的数字。之后K个命令给,要求大矩阵中某些子矩形的各个元素的赋予统一值,求在命令结束后还保持原有值的个数。其中矩阵的格子总数不超过1e6,但是不指定长宽。

二、解题思路

要求使用一种数据结构进行批量染色操作,且要求后面可以检测是否被"其他颜色污染"。

看了题解很容易想到,使用某种数据结构做批量增加的操作,之后检测增加后的值是否能够整除原来的元素。

考虑如果直接按照1、2、3、4进行赋值就会有:同样是染色2次,有3+3 = 6和2+2+2 = 6,无法有效判断整除。考虑加一个操作:增加染色次数的判定,但是同样也会有3+3 = 6 = 2+4的问题,因此对数组进行重新设计,则直觉告诉我们,1,2,4,7,......an,an+n这个数列可以完美解决这个问题——不存在第2种组合可以使用相同数目的数列元素相加得到数列的某个其他元素。

因此,使用一位数组开足够大的数组之后,动态的按照二维数组的方式进行寻址,即可完成上述操作。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll intmax_t
  5.  
  6. const int MAXN=;
  7.  
  8. ll mapp[MAXN];
  9. ll farm[MAXN];
  10. ll times[MAXN];
  11. ll color[MAXN];
  12.  
  13. int maxx_numebr = ;
  14. int add_number = ;
  15.  
  16. int m,n,k;
  17.  
  18. void insert_mex(ll *v,int a,int b,ll key)
  19. {
  20. a+=;
  21. b+=;
  22. while(a<n+)
  23. {
  24. int num = a*(m+);
  25. int pos = b;
  26. while(pos<m+)
  27. {
  28. v[num+pos] += key;
  29. pos+= pos&(-pos);
  30. }a+=a&(-a);
  31.  
  32. }
  33. }
  34. ll find_mex(ll *v,int a,int b)
  35. {
  36. a+=;
  37. b+=;
  38. ll cntt = ;
  39. while(a)
  40. {
  41. int num = a*(m+);
  42. int pos = b;
  43. while(pos)
  44. {
  45. cntt += v[num+pos];
  46. pos -= pos&(-pos);
  47. }
  48. a-=a&(-a);
  49. }
  50. return cntt;
  51. }
  52.  
  53. void insert(ll *v,int a,int b,int c,int d,ll key)
  54. {
  55. // cout<<"coor :"<<a<<" "<<b<<endl;
  56. // cout<<"coor :"<<a<<" "<<d<<endl;
  57. // cout<<"coor :"<<c<<" "<<b<<endl;
  58. // cout<<"coor :"<<c<<" "<<d<<endl;
  59.  
  60. insert_mex(v,a,b,key);
  61. insert_mex(v,c,d,key);
  62. insert_mex(v,a,d,-key);
  63. insert_mex(v,c,b,-key);
  64. }
  65.  
  66. ll find(ll *v,int a,int b)
  67. {
  68. ll cntt = ;
  69. cntt += find_mex(v,a,b);
  70. return cntt;
  71. }
  72.  
  73. void init()
  74. {
  75. memset(color,-,sizeof(color));
  76. for(int i=;i<n;++i)
  77. {
  78. int pos = i*m;
  79. for(int j=;j<m;++j)
  80. {
  81. // int ppos = pos +j;
  82. scanf("%d",&mapp[pos]);
  83. if(color[pos] == -)color[mapp[pos]] = (maxx_numebr += (add_number++));
  84. pos++;
  85. }
  86. }
  87. for(int i=;i<k;++i)
  88. {
  89. int a,b,c,d,kk;
  90. scanf("%d%d%d%d%d",&a,&b,&c,&d,&kk);
  91. if(color[kk] == -)color[kk] = (maxx_numebr += add_number++ );
  92. ll key = color[kk];
  93. a--;b--;
  94. insert(farm,a,b,c,d,key);
  95. insert(times,a,b,c,d,);
  96. }
  97. int cntt = n*m;
  98. int pos = ;
  99. for(int i=;i<n;++i)
  100. {
  101. for(int j=;j<m;++j)
  102. {
  103. // int pos = i*(m+23)+j;
  104. ll kk = color[mapp[pos]];
  105. int time_now = find(times,i,j);
  106. ll res = find(farm,i,j);
  107. // cout<<"check: "<<i<<" "<<j<<" times: "<<time_now<<" color "<<mapp[pos]<<endl;
  108. if(time_now == || res == time_now*kk)cntt--;
  109. pos++;
  110. }
  111. }
  112. cout<<cntt<<endl;
  113.  
  114. }
  115.  
  116. int main()
  117. {
  118. cin>>n>>m>>k;
  119. init();
  120.  
  121. return ;
  122. }

牛客暑假多校第二场J-farm的更多相关文章

  1. 牛客暑假多校第二场 F trade

    题意: 白兔有n个仓库,每个仓库有啊ai个货物,在每个仓库白兔可以装上任意数量的货物,也可以卸下任意数量的货物,现在有k个圆形信号阻隔器,然后有m个顾客下个一个订单,每个顾客的收货量有一个上限, 在每 ...

  2. 牛客暑假多校第二场 K carpet

    题意:给你一个n*m的矩阵 ,每个位置都有一个字符并且都有一个值,现在需要找到一个p*q的子矩阵, 原来的矩阵可以由现在这个矩阵无限复制然后截取其中的一部分得到,并且要求 子矩阵里最大的值 * (p+ ...

  3. 牛客暑假多校第一场 J Different Integers

    题意:给你一个数组, q次询问, 每次询问都会有1个[l, r] 求 区间[1,l] 和 [r, n] 中 数字的种类是多少. 解法1, 莫队暴力: 代码: #include<bits/stdc ...

  4. 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)

    题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...

  5. 2019牛客暑期多校第二场题解FH

    F.Partition problem 传送门 题意:有2n个人,分两组,每组n个,要求sum(vij)最大值. 题解:n并不大我们可以枚举每个人是在1组还是2组爆搜. 代码: #include &l ...

  6. 牛客暑假多校第一场J-Different Integers

    一.题目描述: 链接:https://www.nowcoder.com/acm/contest/139/JGiven a sequence of integers a1, a2, ..., an an ...

  7. 2021牛客暑期多校训练营3 J 思维

    传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com) 题目 Goodeat finds an undirected complete graph ...

  8. 2019牛客暑假多校赛(第二场) F和H(单调栈)

    F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...

  9. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

随机推荐

  1. APK加固之静态脱壳机编写入门

    目录: 0x00APK加固简介与静态脱壳机的编写思路 1.大家都知道Android中的程序反编译比较简单,辛苦开发出一个APK轻易被人反编译了,所以现在就有很多APK加固的第三方平台,比如爱加密和梆梆 ...

  2. iphone 微信下浏览器中数字去除下划线

    在开发iphone应用程序的时候,safari下手机号码默认是有下划线的,通过下面的方法就可以去掉: <meta name="format-detection" conten ...

  3. Struts2_Global_Results_全局结果集

    struts.xml文件配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  4. JavaScript 数组排序(从大到小,从小到大)

    1.数组的从大到小排序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  5. 吴超hadoop7天视频教程全集

    吴超hadoop7天视频教程全集 一.初级班全套视频 1.linux使用(3节) 2.伪分布模式安装hadoop(2节) 3.HDFS的体系结构和操作(2节) 4.HDFS的java操作方式(4节) ...

  6. 4 - 函数&装饰器 and 迭代器&生成器

    函数是什么 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的.程序里函数的定义是: 定义:将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 ...

  7. April 12 2017 Week 15 Wednesday

    Genius often betrays itself into great errors. 天才常被天才误. Genius can help us get greater achievements, ...

  8. Selenium入门8 js调用

    execute_script 可以执行js和jquery脚本 示例如下:修改百度首页的按钮字体颜色,按钮隐藏,按钮显示 #coding=utf-8 # 调用js jquery from seleniu ...

  9. vue中多个元素或多个组件之间的动画效果

    多个元素的过渡 <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-acitve,.v-leave-active{ opacity: ...

  10. 在vue中使用animate库

    <style> @keyframes bounce-in { 0% { transform: scale(0); } 50% { transform: scale(1.5) } 100% ...