void quick_sort(int q[], int l, int r){ if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1]; while (i < j){ do i ++ ; while (q[i] < x); do j -- ; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l, j), quick_sort(q, j + 1, r); }
void merge_sort(int q[], int l, int r){ if (l >= r) return;
int mid = l + r >> 1; merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1; while (i <= mid && j <= r) if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ]; else tmp[k ++ ] = q[j ++ ];
while (i <= mid) tmp[k ++ ] = q[i ++ ]; while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j]; }
int bsearch_1(int l, int r){ while (l < r){ int mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } return l; }
int bsearch_2(int l, int r){ while (l < r){ int mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; } return l; }
double bsearch_3(double l, double r){ const double eps = 1e-6; while (r - l > eps){ double mid = l + r >> 1; if (check(mid)) r = mid; else l = mid; } return l; }
vector<int> add(vector<int> &A, vector<int> &B){ if (A.size() < B.size()) return add(B, A); vector<int> C; int t = 0; for (int i = 0; i < A.size() || t; i ++ ){ t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % 10); t /= 10; } return C; }
vector<int> sub(vector<int> &A, vector<int> &B){ vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ){ t = A[i] - t; if (i < B.size()) t -= B[i]; C.push_back((t + 10) % 10); if (t < 0) t = 1; else t = 0; } while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
vector<int> mul(vector<int> &A, int b){ vector<int> C; int t = 0; for (int i = 0; i < A.size() || t; i ++ ){ if (i < A.size()) t += A[i] * b; C.push_back(t % 10); t /= 10; } while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
vector<int> div(vector<int> &A, int b, int &r){ vector<int> C; r = 0; for (int i = A.size() - 1; i >= 0; i -- ){ r = r * 10 + A[i]; C.push_back(r / b); r %= b; } reverse(C.begin(), C.end()); while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); for (int i = 1; i <= n; i ++ ) s[i] = s[i - 1] + a[i];
#include <iostream> using namespace std;
const int N = 1010; int n, m, q; int s[N][N];
int main(){ scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) scanf("%d", &s[i][j]);
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
while (q -- ){ int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); printf("%d\n", s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]); } return 0; }
void insert(int l, int r, int c){ b[l] += c; b[r + 1] -= c; } for (int i = 1; i <= n; i ++ ) insert(i, i, a[i]);
#include<iostream> using namespace std;
const int N = 1e3 + 10; int a[N][N], b[N][N];
void insert(int x1, int y1, int x2, int y2, int c){ b[x1][y1] += c; b[x2 + 1][y1] -= c; b[x1][y2 + 1] -= c; b[x2 + 1][y2 + 1] += c; }
int main(){ int n, m, q; cin >> n >> m >> q; for(int i = 1; i<= n; i ++) for(int j = 1; j <= m; j ++) scanf("%d", &a[i][j]); for(int i = 1; i<= n; i ++) for(int j = 1; j <= m; j ++) insert(i, j, i, j, a[i][j]); while(q --){ int x1, y1, x2, y2, c; cin >> x1 >> y1 >> x2 >> y2 >> c; insert(x1, y1, x2, y2, c); }
for(int i = 1; i<= n; i ++){ for(int j = 1; j <= m; j ++){ b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]; cout << b[i][j] << " "; } cout << endl; } return 0; }
int lengthOfLongestSubstring(string s) { int n = s.size(); map<char, int> mp; int res = 0; for(int l = 0, r = 0; r < n; r ++){ mp[s[r]] ++; while(l < n && mp[s[r]] > 1) mp[s[l ++]] --; res = max(res ,r - l + 1); } return res; }
n >> 1 n & 1 n & (n - 1) n & -n
vector<int> alls; sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end());
int find(int x){ int l = 0, r = alls.size() - 1; while (l < r){ int mid = l + r >> 1; if (alls[mid] >= x) r = mid; else l = mid + 1; } return l + 1; xx }
#include <iostream> #include <vector> #include <algorithm>
using namespace std; const int N = 300010; int n, m; int a[N], s[N]; vector<int> alls; vector<pair<int, int>> add, query;
int find(int x){ int l = 0, r = alls.size() - 1; while (l < r){ int mid = l + r >> 1; if (alls[mid] >= x) r = mid; else l = mid + 1; } return l + 1; }
int main(){ cin >> n >> m;
for (int i = 0; i < n; i ++ ){ int x, c; cin >> x >> c; add.push_back({x, c}); alls.push_back(x); }
for (int i = 0; i < m; i ++ ){ int l, r; cin >> l >> r; query.push_back({l, r}); alls.push_back(l); alls.push_back(r); }
sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); for (auto item : add){ int x = find(item.first); a[x] += item.second; }
for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i];
for (auto item : query){ int l = find(item.first), r = find(item.second); cout << s[r] - s[l - 1] << endl; }
return 0; }
|