fork download
  1. //#pragma GCC optimize("Ofast,unroll-loops")
  2. //#pragma GCC target("avx2,tune=native")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define file "o"
  7. #define ff(i, a, b) for (auto i=(a); i<=(b); ++i)
  8. #define ffr(i, b, a) for (auto i=(b); i>=(a); --i)
  9. #define nl "\n"
  10. #define ss " "
  11. #define pb emplace_back
  12. #define fi first
  13. #define se second
  14. #define sz(s) (int)s.size()
  15. #define all(s) (s).begin(), (s).end()
  16. #define ms(a,x) memset(a, x, sizeof (a))
  17. #define cn continue
  18. #define re exit(0)
  19.  
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef long double ld;
  23. typedef vector<int> vi;
  24. typedef vector<ll> vll;
  25. typedef pair<int,int> pii;
  26. typedef pair<ll,ll> pll;
  27. typedef vector<pii> vpii;
  28. typedef vector<pll> vpll;
  29.  
  30. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  31. ll ran(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }
  32.  
  33. inline void rf() {
  34. ios_base::sync_with_stdio(false);
  35. cin.tie(nullptr); cout.tie(nullptr);
  36. if (fopen(file".inp","r")) {
  37. freopen(file".inp","r",stdin);
  38. freopen(file".out","w",stdout);
  39. }
  40. }
  41.  
  42. const int mod=1e9+19972207;
  43. const int maxn=3e4+15;
  44. const ll inf=1e16;
  45.  
  46. template<typename T> inline void add(T &x, const T &y)
  47. {
  48. x+=y;
  49. if(x>=mod) x-=mod;
  50. if(x<0) x+=mod;
  51. }
  52.  
  53. template<typename T> inline bool maxi(T &a, T b)
  54. {
  55. if(a>=b) return 0;
  56. a=b; return 1;
  57. }
  58.  
  59. template<typename T> inline bool mini(T &a, T b)
  60. {
  61. if(a<=b) return 0;
  62. a=b; return 1;
  63. }
  64.  
  65. struct Upd {
  66. int xa, ya, xb, yb, t;
  67. ll w;
  68. };
  69.  
  70. int main() {
  71. rf();
  72.  
  73. int K, P;
  74. if(!(cin >> K >> P)) return 0;
  75.  
  76. vector<Upd> ups(P);
  77. int maxT = 0;
  78.  
  79. vector<vector<ll>> D(K+3, vector<ll>(K+3, 0));
  80.  
  81. ff(i,0,P-1){
  82. int xa, ya, xb, yb, t; ll w;
  83. cin >> xa >> ya >> xb >> yb >> t >> w;
  84. if(xa>xb) swap(xa,xb);
  85. if(ya>yb) swap(ya,yb);
  86. ups[i] = {xa,ya,xb,yb,t,w};
  87. maxT = max(maxT, t);
  88.  
  89. D[xa][ya] += w;
  90. D[xb+1][ya] -= w;
  91. D[xa][yb+1] -= w;
  92. D[xb+1][yb+1] += w;
  93. }
  94.  
  95. vector<vector<ll>> S(K+2, vector<ll>(K+2,0));
  96. ff(x,1,K){
  97. ll run=0;
  98. ff(y,1,K){
  99. run += D[x][y];
  100. S[x][y] = S[x-1][y] + run;
  101. }
  102. }
  103.  
  104. vector<vector<int>> byT(maxT+1);
  105. ff(i,0,P-1) byT[ups[i].t].push_back(i);
  106.  
  107. const int B = 32;
  108. int BX = (K + B - 1) / B, BY = (K + B - 1) / B;
  109.  
  110. struct BlockInfo {
  111. int cand = 0;
  112. ll bal = 0;
  113. vector<int> border;
  114. };
  115. vector<vector<BlockInfo>> blk(BX, vector<BlockInfo>(BY));
  116.  
  117. auto blockBounds = [&](int bx, int by) {
  118. int xl = bx*B + 1, xr = min(K, (bx+1)*B);
  119. int yl = by*B + 1, yr = min(K, (by+1)*B);
  120. return array<int,4>{xl,yl,xr,yr};
  121. };
  122.  
  123. auto fullyCover = [&](const Upd& u, int bx, int by)->bool{
  124. auto b = blockBounds(bx,by);
  125. return (u.xa <= b[0] && b[2] <= u.xb && u.ya <= b[1] && b[3] <= u.yb);
  126. };
  127.  
  128. auto overlap = [&](const Upd& u, int bx, int by)->bool{
  129. auto b = blockBounds(bx,by);
  130. if (u.xa > b[2] || u.xb < b[0] || u.ya > b[3] || u.yb < b[1]) return false;
  131. return true;
  132. };
  133.  
  134. for(int i=0;i<P;++i){
  135. const auto &u = ups[i];
  136. int bx1 = (u.xa-1)/B, bx2 = (u.xb-1)/B;
  137. int by1 = (u.ya-1)/B, by2 = (u.yb-1)/B;
  138.  
  139. for(int bx=bx1; bx<=bx2; ++bx){
  140. for(int by=by1; by<=by2; ++by){
  141. if(fullyCover(u,bx,by)){
  142. auto &bi = blk[bx][by];
  143. if(bi.cand==u.t){ bi.bal += u.w; }
  144. else{
  145. if(bi.bal >= u.w) bi.bal -= u.w;
  146. else { bi.cand = u.t; bi.bal = u.w - bi.bal; }
  147. }
  148. }else if(overlap(u,bx,by)){
  149. blk[bx][by].border.push_back(i);
  150. }
  151. }
  152. }
  153. }
  154.  
  155. vector<vector<int>> ans(K+1, vector<int>(K+1, 0));
  156.  
  157. for(int bx=0; bx<BX; ++bx){
  158. for(int by=0; by<BY; ++by){
  159. auto b = blockBounds(bx,by);
  160. int xl=b[0], yl=b[1], xr=b[2], yr=b[3];
  161. int nx = xr-xl+1, ny = yr-yl+1;
  162.  
  163. vector<int> candList;
  164. {
  165. unordered_set<int> seen;
  166. if(blk[bx][by].cand) { seen.insert(blk[bx][by].cand); candList.push_back(blk[bx][by].cand); }
  167. for(int id: blk[bx][by].border){
  168. int t = ups[id].t;
  169. if(!seen.count(t)){ seen.insert(t); candList.push_back(t); }
  170. }
  171. }
  172.  
  173. if(candList.empty()){
  174. ff(x,xl,xr) ff(y,yl,yr) ans[x][y]=0;
  175. continue;
  176. }
  177.  
  178. ff(x,xl,xr) ff(y,yl,yr) ans[x][y]=0;
  179.  
  180. for(int t : candList){
  181. vector<vector<ll>> Dloc(nx+2, vector<ll>(ny+2, 0));
  182. ll baseAll = 0;
  183.  
  184. for(int id : byT[t]){
  185. const auto &u = ups[id];
  186. if(u.xa>xr || u.xb<xl || u.ya>yr || u.yb<yl) continue;
  187.  
  188. if(u.xa<=xl && xr<=u.xb && u.ya<=yl && yr<=u.yb){
  189. baseAll += u.w;
  190. }else{
  191. int xa=max(u.xa,xl), xb=min(u.xb,xr);
  192. int ya=max(u.ya,yl), yb=min(u.yb,yr);
  193. int x1=xa-xl+1, x2=xb-xl+1;
  194. int y1=ya-yl+1, y2=yb-yl+1;
  195. Dloc[x1][y1] += u.w;
  196. Dloc[x2+1][y1] -= u.w;
  197. Dloc[x1][y2+1] -= u.w;
  198. Dloc[x2+1][y2+1] += u.w;
  199. }
  200. }
  201.  
  202. for(int i=1;i<=nx;++i){
  203. for(int j=1;j<=ny;++j){
  204. Dloc[i][j] += Dloc[i-1][j] + Dloc[i][j-1] - Dloc[i-1][j-1];
  205. }
  206. }
  207. bool anySet = false;
  208. for(int i=1;i<=nx;++i){
  209. for(int j=1;j<=ny;++j){
  210. int x = xl + i - 1;
  211. int y = yl + j - 1;
  212. if(ans[x][y]) continue;
  213. ll Ec = baseAll + Dloc[i][j];
  214. if( Ec*2 > S[x][y] ){
  215. ans[x][y] = t;
  216. anySet = true;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223.  
  224. ff(x,1,K){
  225. ff(y,1,K){
  226. if(y>1) cout << ' ';
  227. cout << ans[x][y];
  228. }
  229. cout << '\n';
  230. }
  231. return 0;
  232. }
Success #stdin #stdout 0.01s 5324KB
stdin
5 3
1 3 5 5 1 3
1 1 3 5 2 3
2 2 4 4 2 5
stdout
2 2 0 0 0
2 2 2 2 0
2 2 2 2 0
0 2 2 2 1
0 0 1 1 1