Submission #1358551

#TimeUsernameProblemLanguageResultExecution timeMemory
1358551huseyncafarliBubble Sort Machine (JOI25_bubble)C++20
5 / 100
2095 ms15932 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll

const int MAXN = 1e6 + 5;
const int inf = (int)2e9 + 5;
const int infll = (int)4e18 + 5;
const int mod = (int)1e9 + 7;

struct BIT {
  int n;
  vector<int> tree;
  BIT(int n) {
    this->n = n;
    tree.assign(n + 5, 0);
  }
  void update(int p, int x) {
    for(; p <= n; p += p & -p) tree[p] += x;
  }
  int query(int p) {
    int res = 0;
    for(; p > 0; p -= p & -p) res += tree[p];
    return res;
  }
  int range_query(int l, int r) {
    return query(r) - query(l - 1);
  }
};

void solve(){
  int n;
  cin >> n;
  vector<int> a(n + 1);
  BIT b(n + 5);
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b.update(i, a[i]);
  }
  int q;
  cin >> q;
  while(q--) {
    int t;
    cin >> t;
    if(t == 1) {
      for(int j = 1; j < n; j++) {
        if(a[j + 1] < a[j]) {
          swap(a[j], a[j+1]);
          b.update(j, a[j] - a[j + 1]);
          b.update(j + 1, a[j+1] - a[j]);
        }
      }
    } else {
      int l, r;
      cin >> l >> r;
      cout << b.range_query(l, r) << endl;;
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  //cin >> t;
  while(t--)
    solve();
  return 0;
}

#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...