Advertisement
Emiliatan

c216

Sep 16th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. /* c216           */
  2. /* AC (0.1s, 5MB) */
  3. #pragma warning( disable : 4996 )
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <cstdint>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <tuple>
  10. #define ios_jazz ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  11.  
  12. using namespace std;
  13.  
  14. using int16 = short;
  15. using uint16 = unsigned short;
  16. using uint = unsigned int;
  17. using int64 = long long;
  18. using uint64 = unsigned long long;
  19. using pii = pair<int, int>;
  20.  
  21. /* main code */
  22. using tiiii = tuple<int, int, int, int>;
  23.  
  24. enum {Num, L, R, K};
  25.  
  26. struct obj
  27. {
  28.     int64 value;
  29.     int pos;
  30.     obj() = default;
  31.     obj(int v, int p) : value(v), pos(p) {};
  32.     bool operator< (const obj& B) const
  33.     {
  34.         return value < B.value;
  35.     }
  36. };
  37.  
  38. const int MAXN = 1e5;
  39. const int MAXM = 5e5;
  40.  
  41. tiiii query[MAXM];
  42. int n, m, queryCnt = 0;
  43. obj tH[MAXN + 1];
  44. int64 ans[MAXM], S[MAXN + 1]{};
  45.  
  46. /* BIT */
  47. auto lowbit = [](const int &x) noexcept { return x & -x; };
  48. int BIT[MAXN + 1];
  49. int ask(int) noexcept;
  50. void add1(int) noexcept;
  51. /* BIT */
  52.  
  53. int main()
  54. {
  55.     scanf("%d %d", &n, &m);
  56.  
  57.     for (int i = 1; i <= n; ++i)
  58.     {
  59.         scanf("%d", &tH[i].value);
  60.         S[i] = S[i - 1] + tH[i].value;
  61.         tH[i].pos = i;
  62.     }
  63.  
  64.     for (int i = 0, op, l, r; i < m; ++i)
  65.     {
  66.         scanf("%d", &op);
  67.         if (op == 1)
  68.         {
  69.             scanf("%d", &l);
  70.             *S = (*S + l) % MAXN;
  71.         }
  72.         else
  73.         {
  74.             scanf("%d %d", &l, &r);
  75.             get<Num>(query[queryCnt]) = queryCnt;
  76.             get<L>(query[queryCnt]) = l;
  77.             get<R>(query[queryCnt]) = r;
  78.             get<K>(query[queryCnt]) = MAXN - *S;
  79.             ++queryCnt;
  80.         }
  81.     }
  82.     *S = 0;
  83.  
  84.     sort(tH + 1, tH + 1 + n);
  85.     sort(query, query + queryCnt, [](const tiiii& lhs, const tiiii& rhs) {
  86.         return get<K>(lhs) < get<K>(rhs);
  87.         });
  88.  
  89.     for (int i = 0, x = 1; i < queryCnt; ++i)
  90.     {
  91.         int p = upper_bound(tH + 1, tH + 1 + n, obj{ get<K>(query[i]), 1 }) - tH;
  92.  
  93.         int qr = get<R>(query[i]), ql = get<L>(query[i]), k = MAXN - get<K>(query[i]);
  94.  
  95.         while (x < p)
  96.             add1(tH[x++].pos);
  97.  
  98.         ans[get<Num>(query[i])] = (S[qr] - S[ql - 1] + k * (int64)(qr - ql + 1)) - MAXN * (int64)(qr - ql + 1 - (ask(qr) - ask(ql - 1)));
  99.     }
  100.  
  101.     for (int i = 0; i < queryCnt; ++i)
  102.         printf("%lld\n", ans[i]);
  103.  
  104.     return 0;
  105. }
  106.  
  107. int ask(int pos) noexcept
  108. {
  109.     int res = 0;
  110.  
  111.     for (; pos; pos -= lowbit(pos))
  112.         res += BIT[pos];
  113.  
  114.     return res;
  115. }
  116.  
  117. void add1(int pos) noexcept
  118. {
  119.     for (; pos <= MAXN; pos += lowbit(pos))
  120.         ++BIT[pos];
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement