Advertisement
Guest User

Untitled

a guest
Feb 11th, 2022
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #pragma GCC target("avx2")
  2.  
  3. #include <x86intrin.h>
  4. #include "bits/stdc++.h"
  5. using namespace std;
  6.  
  7. namespace IO {
  8.     const int BUFSIZE = 1<<14;
  9.     char buf[BUFSIZE + 1], *inp = buf;
  10.  
  11.     bool reacheof;
  12.     char get_char() {
  13.         if (!*inp && !reacheof) {
  14.             memset(buf, 0, sizeof buf);
  15.             int tmp = fread(buf, 1, BUFSIZE, stdin);
  16.             if (tmp != BUFSIZE) reacheof = true;
  17.             inp = buf;
  18.         }
  19.         return *inp++;
  20.     }
  21.     template<typename T>
  22.     T get() {
  23.         int neg = 0;
  24.         T res = 0;
  25.         char c = get_char();
  26.         while (!std::isdigit(c) && c != '-' && c != '+') c = get_char();
  27.         if (c == '+') { neg = 0; }
  28.         else if (c == '-') { neg = 1; }
  29.         else res = c - '0';
  30.  
  31.         c = get_char();
  32.         while (std::isdigit(c)) {
  33.             res = res * 10 + (c - '0');
  34.             c = get_char();
  35.         }
  36.         return neg ? -res : res;
  37.     }
  38. };
  39.  
  40. const int MN = 100111;
  41. const int INF = 1000111000;
  42.  
  43. int a[MN];
  44. int res[8], inf[8];
  45.  
  46. int main() {
  47.     int n = IO::get<int>();
  48.     for (int i = 1; i <= n; i++) {
  49.         a[i] = IO::get<int>();
  50.     }
  51.     int q = IO::get<int>();
  52.  
  53.     for (int i = 0; i < 8; i++) {
  54.         inf[i] = -INF;
  55.     }
  56.  
  57.     while (q--) {
  58.         int typ = IO::get<int>();
  59.         int x = IO::get<int>();
  60.         int y = IO::get<int>();
  61.         if (typ == 1) a[x] = y;
  62.         else {
  63.             // load (-INF, -INF, ..., -INF) into 256-bit register max_val
  64.             __m256i max_val = _mm256_loadu_si256((__m256i*) (inf));
  65.  
  66.             int l = x;
  67.             // Process each group of 8 numbers
  68.             for (; l+7 <= y; l += 8) {
  69.                 // load a[l] a[l+1] a[l+2] ... a[l+7] to 256-bit register
  70.                 __m256i vals = _mm256_loadu_si256((__m256i*) &a[l]);
  71.  
  72.                 // update max_val
  73.                 max_val = _mm256_max_epi32(max_val, vals);
  74.             }
  75.  
  76.             // save 256-bit register max_val to an array res
  77.             _mm256_storeu_si256((__m256i*) &res[0], max_val);
  78.            
  79.             // r = final result.
  80.             int r = -INF;
  81.             for (int i = 0; i < 8; i++) {
  82.                 r = (r < res[i]) ? res[i] : r;
  83.             }
  84.  
  85.             // add extra elements at the end
  86.             for (int i = l; i <= y; i++) {
  87.                 r = (r < a[i]) ? a[i] : r;
  88.             }
  89.             cout << r << '\n';
  90.         }
  91.     }
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement