fork(1) download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "icebear"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 2e5 + 5;
  38. int n, q, a[N], c[N], pos[N];
  39. ii b[N];
  40. ll ans = 0;
  41.  
  42. struct BIT {
  43. ll ft[N];
  44. void update(int x, int val) {
  45. for(; x <= n; x += x & -x)
  46. ft[x] += val;
  47. }
  48.  
  49. ll getSum(int x) {
  50. ll ans = 0;
  51. for(; x; x -= x & -x) ans += ft[x];
  52. return ans;
  53. }
  54.  
  55. ll get(int l, int r) {
  56. return getSum(r) - getSum(l - 1);
  57. }
  58. } sumC, passEdge;
  59.  
  60. void init(void) {
  61. cin >> n;
  62. FOR(i, 1, n) cin >> a[i], b[i] = mp(a[i], i);
  63. FOR(i, 1, n - 1) cin >> c[i], sumC.update(i, c[i]);
  64. }
  65.  
  66. void process(void) {
  67. sort(b + 1, b + n + 1);
  68. FOR(i, 1, n) pos[b[i].se] = i;
  69.  
  70. auto upd = [&](int i, int sign) {
  71. if (i > pos[i]) {
  72. ans += sumC.get(pos[i], i - 1) * sign;
  73. passEdge.update(pos[i], +1 * sign);
  74. passEdge.update(i, -1 * sign);
  75. }
  76. if (i < pos[i]) {
  77. ans += sumC.get(i, pos[i] - 1) * sign;
  78. passEdge.update(i, +1 * sign);
  79. passEdge.update(pos[i], -1 * sign);
  80. }
  81. };
  82.  
  83. FOR(i, 1, n) upd(i, +1);
  84. cout << ans << '\n';
  85. cin >> q;
  86. while(q--) {
  87. int type, x, y;
  88. cin >> type >> x >> y;
  89. if (type == 1) {
  90. upd(x, -1);
  91. upd(y, -1);
  92.  
  93. swap(a[x], a[y]);
  94. swap(pos[x], pos[y]);
  95. upd(x, +1);
  96. upd(y, +1);
  97. } else {
  98. ans += (y - c[x]) * passEdge.getSum(x);
  99. sumC.update(x, y - c[x]);
  100. c[x] = y;
  101. }
  102. cout << ans << '\n';
  103. }
  104. }
  105.  
  106. int main() {
  107. ios_base::sync_with_stdio(0);
  108. cin.tie(0); cout.tie(0);
  109. if (fopen(task".inp", "r")) {
  110. freopen(task".inp", "r", stdin);
  111. freopen(task".out", "w", stdout);
  112. }
  113. int tc = 1;
  114. // cin >> tc;
  115. while(tc--) {
  116. init();
  117. process();
  118. }
  119. return 0;
  120. }
  121.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
0