1924B - Space Harbour - CodeForces Solution


data structures implementation math sortings *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
set <int> hb;
ull c1[300010], c2[300010], c3[300010], val[300010];
int n, m, q;
void add(ull *c, int k, ull d)
{
   for(; k <= n; k += k & -k)
      c[k] += d;
}
ull ask(ull *c, int k)
{
   ull ans = 0;
   for(; k; k &= k-1)
   {
      ans += c[k];
   }
   return ans;
}
void chg0(int k, ull x)
{
   add(c1, k, x); add(c2, k, k*x); add(c3, k, k*x*k);
}
void chg1(int l, int r, ull v)
{
   chg0(l, v); if(r < n) chg0(r+1, -v);
}
void chg2(int l, int r, ull v)
{
   chg1(l+1, r, -v);
   chg1(l+1,l+1,1ll*v*(r-l));
}
ull query(int k)
{
   return (ask(c3,k)-(2*k+3)*ask(c2,k)+ask(c1,k)*(k+1)*(k+2))/2;
}
int id[300010];
int main()
{
   ios::sync_with_stdio(0);
   cin >> n >> m >> q;
   for(int i = 1; i <= m; ++i) cin >> id[i], hb.insert(id[i]);
   for(int i = 1; i <= m; ++i) cin >> val[id[i]];
   chg2(1, n, val[1]); sort(id+1, id+m+1);
   for(int i = 2; i < m; ++i)
   {
      chg2(id[i-1], n, -val[id[i-1]]);
      chg2(id[i-1], id[i], val[id[i-1]]);
      chg2(id[i], n, val[id[i]]);
   }
   while(q--)
   {
      int op, x, v; cin >> op >> x >> v;
      if(op == 1)
      {
         int L = *--hb.lower_bound(x), R = *hb.lower_bound(x);
         chg2(L, R, -val[L]);
         chg2(L, x, val[L]);
         chg2(x, R, val[x]=v); hb.insert(x);
      } else cout << (query(v)-query(x-1)) << '\n';
   }
   return 0;
}


Comments

Submit
0 Comments
More Questions

230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns