Advertisement
nicuvlad76

Untitled

Nov 26th, 2020
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define BUF 16384
  3.  
  4. using namespace std;
  5.  
  6. class InParser {
  7. private:
  8.     FILE *fin;
  9.     char *buff;
  10.     int sp;
  11.  
  12.     char read_ch() {
  13.         ++sp;
  14.         if (sp == BUF) {
  15.             sp = 0;
  16.             fread(buff, 1, BUF, fin);
  17.         }
  18.         return buff[sp];
  19.     }
  20.  
  21. public:
  22.     InParser(const char* nume) {
  23.         fin = fopen(nume, "r");
  24.         buff = new char[BUF]();
  25.         sp = BUF-1;
  26.     }
  27.  
  28.     InParser& operator >> (int &n) {
  29.         char c;
  30.         while (!isdigit(c = read_ch()) && c != '-');
  31.         int sgn = 1;
  32.         if (c == '-') {
  33.             n = 0;
  34.             sgn = -1;
  35.         } else {
  36.             n = c - '0';
  37.         }
  38.         while (isdigit(c = read_ch())) {
  39.             n = 10 * n + c - '0';
  40.         }
  41.         n *= sgn;
  42.         return *this;
  43.     }
  44.  
  45.     InParser& operator >> (long long &n) {
  46.         char c;
  47.         n = 0;
  48.         while (!isdigit(c = read_ch()) && c != '-');
  49.         long long sgn = 1;
  50.         if (c == '-') {
  51.             n = 0;
  52.             sgn = -1;
  53.         } else {
  54.             n = c - '0';
  55.         }
  56.         while (isdigit(c = read_ch())) {
  57.             n = 10 * n + c - '0';
  58.         }
  59.         n *= sgn;
  60.         return *this;
  61.     }
  62. };
  63.  
  64. class OutParser {
  65. private:
  66.     FILE *fout;
  67.     char *buff;
  68.     int sp;
  69.  
  70.     void write_ch(char ch) {
  71.         if (sp == 50000) {
  72.             fwrite(buff, 1, 50000, fout);
  73.             sp = 0;
  74.             buff[sp++] = ch;
  75.         } else {
  76.             buff[sp++] = ch;
  77.         }
  78.     }
  79.  
  80.  
  81. public:
  82.     OutParser(const char* name) {
  83.         fout = fopen(name, "w");
  84.         buff = new char[50000]();
  85.         sp = 0;
  86.     }
  87.     ~OutParser() {
  88.         fwrite(buff, 1, sp, fout);
  89.         fclose(fout);
  90.     }
  91.  
  92.     OutParser& operator << (int vu32) {
  93.         if (vu32 <= 9) {
  94.             write_ch(vu32 + '0');
  95.         } else {
  96.             (*this) << (vu32 / 10);
  97.             write_ch(vu32 % 10 + '0');
  98.         }
  99.         return *this;
  100.     }
  101.  
  102.     OutParser& operator << (long long vu64) {
  103.         if (vu64 <= 9) {
  104.             write_ch(vu64 + '0');
  105.         } else {
  106.             (*this) << (vu64 / 10);
  107.             write_ch(vu64 % 10 + '0');
  108.         }
  109.         return *this;
  110.     }
  111.  
  112.     OutParser& operator << (char ch) {
  113.         write_ch(ch);
  114.         return *this;
  115.     }
  116.     OutParser& operator << (const char *ch) {
  117.         while (*ch) {
  118.             write_ch(*ch);
  119.             ++ch;
  120.         }
  121.         return *this;
  122.     }
  123. };
  124.  
  125. InParser fin ("aib2d.in");
  126. OutParser fout ("aib2d.out");
  127.  
  128. int n, Q, op, x, aib[1001][1001];
  129.  
  130. inline int Suma (int x, int y) {
  131.     int S = 0;
  132.     for (int i = x; i > 0; i -= (i & (-i)))
  133.     for (int j = y; j > 0; j -= (j & (-j)))
  134.     S += aib[i][j];
  135.     return S;
  136. }
  137.  
  138. inline void Update (int x, int y, int val) {
  139.     for (int i = x; i <= n; i += (i & (-i)))
  140.     for (int j = y; j <= n; j += (j & (-j)))
  141.     aib[i][j] += val;
  142. }
  143.  
  144. inline void Query (int x1, int y1, int x2, int y2) {
  145.     fout << Suma (x2, y2) - Suma (x2, y1 - 1) - Suma (x1 - 1, y2) + Suma (x1 - 1, y1 - 1) << '\n';
  146. }
  147.  
  148. int main() {
  149.     fin >> n;
  150.     for (int i = 1; i <= n; i ++)
  151.     for (int j = 1; j <= n; j ++) {
  152.       fin >> x;
  153.       Update (i, j, x);
  154.     }
  155.     fin >> Q;
  156.     while (Q --) {
  157.         fin >> op;
  158.         if (op == 1) {
  159.             int x, y, val;
  160.             fin >> x >> y >> val;
  161.             Update (x, y, val);
  162.         } else {
  163.             int x1, y1, x2, y2;
  164.             fin >> x1 >> y1 >> x2 >> y2;
  165.             Query (x1, y1, x2, y2);
  166.         }
  167.     }
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement