PROBLEM LINK:
Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1
Author: Aryan Agarwala, Daanish Mahajan
Tester: Istvan Nagy
Editorialist: Vichitr Gandas
DIFFICULTY:
EASY
PREREQUISITES:
Fenwick Trees (Binary Indexed Trees), Segment Trees, Range Updates
PROBLEM:
Given an array A consisting of size N. Process Q queries of the following types:
1 L R X - Add (X+i−L)^2 to A_i, for all i in [L,R]
2 i - Output A_i for i∈[1,N]
QUICK EXPLANATION:
The given queries can be easily processed with binary indexed tress or segment trees.
EXPLANATION:
Processing type 1 queries
Let’s look at the first query:
1 L R X - Add (X+i−L)^2 to A_i, for all i in [L,R]
Here we are adding (X+i−L)^2. Let y = X-L. Now lets expand this term:
(X+i−L)^2 = (X-L+i)^2
= (y+i)^2
=y^2 + 2*y*i + i^2
Now lets say we performed two queries of type 1 with y=y_1 and y=y_2 respectively. Let’s see what value we added to A_i after the two queries. We add following to A_i.
(y_1^2 + 2*y_1*i + i^2) + (y_2^2 + 2*y_2*i + i^2)
=(y_1^2 + y_2^2) + (2*y_1 + 2*y_2)*i + (1+1)*i^2
Similarly after we perform k queries of type 1, A_i will be increased by:
(y_1^2 + y_2^2+...+y_k^2) + (2*y_1 + 2*y_2+...+2*y_k)*i + (1+1+...+1 (k \space times))*i^2
If we can maintain following sums \sum{y^2}, \sum{2*y} and \sum{1} for each i then we can easily answer what would be the value of A_i after performing these queries. These values can be maintained by using three binary indexed trees or segment trees. Let the data structures B_1 maintain the sum \sum{y^2}, B_2 maintain the sum \sum{2*y} and B_3 maintain the sum \sum{1}.
Now we can break the query of type 1 in following steps:
- Add y^2 to the range [L, R] in B_1.
- Add 2*y to the range [L,R] in B_2.
- Add 1 to the range [L,R] in B_3.
These are simple update queries which can be performed in O(logn) time in both segment trees and binary indexed trees.
Answering type 2 queries
As we saw in the previous section, after k queries of type 1, A_i will be increased by:
(y_1^2 + y_2^2+...+y_k^2) + (2*y_1 + 2*y_2+...+2*y_k)*i + (1+1+...+1 (k \space times))*i^2
=\sum{y^2} + i* \sum{2*y} + i^2 * \sum{1}
Let the point query q_1(i) return the value at index i in B_1, q_2(i) return the value at index i in B_2 and q_3(i) return the value at index i in B_3. These are simple get queries for the value at index i which can be easily processed in O(logn) time in both segment trees and binary indexed trees.
Now the value added at index i would be:
q_1(i) + i*q_2(i) + i^2*q_3(i)
So answer to the query 2 \space i would be A_i + q_1(i) + i*q_2(i) + i^2*q_3(i).
Algorithm
- Keep 3 fenwick arrays or segment trees B_1, B_2, B_3.
- For queries of type 1
- Now let y = X - L.
- For B_1 do a range update: add y ^ 2 for all i from L to R.
- For B_2 do a range update: add 2 * y for all i from L to R.
- For B_3 do a range update: add 1 for all i from L to R.
- For queries of type 2
- Let point query in i^{th} DS on index j be represented by q_i(j)
- Answer to the query is A_i + q_1(i) + i * q_2(i) + i^2 * q_3(i).
TIME COMPLEXITY:
O(QlogN) for processing Q queries as each query takes O(logn) time.
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
#define initrand mt19937 mt_rand(time(0));
#define rand mt_rand()
#define MOD 1000000007
#define INF 1000000000
#define int long long
#define mid(l, u) ((l+u)/2)
#define rchild(i) (i*2 + 2)
#define lchild(i) (i*2 + 1)
#define lz lazup(l, u, i);
//#define mp(x, y) make_pair(x, y)
using namespace std;
const int NLIM = 1e5;
const int QLIM = 1e5;
const int XLIM = 1e6;
const int N = 1e5 + 5;
const string INFILE = "2.in";
const string OUTFILE = "2.out";
//what do we need?
//negative stuff in segment tree
//make l greater than x
//1 30 50 5
//2 30
//big stuff in segment tree
//1 1 1e5 1e6
//1 1 1e5 1e6
initrand;
int n;
ofstream fout(INFILE);
ofstream fout1(OUTFILE);
int seg[4*N][3];
int lazy[4*N][3];
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);
assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}
assert(l<=x && x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(int l,int r,char endd){
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
return readString(l,r,'\n');
}
string readStringSp(int l,int r){
return readString(l,r,' ');
}
void lazup(int l, int u, int i, int k){
if(lazy[i][k] == 0) return;
seg[i][k] += lazy[i][k];
if(l!=u) {
lazy[lchild(i)][k] += lazy[i][k];
lazy[rchild(i)][k] += lazy[i][k];
}
lazy[i][k] = 0;
}
int update(int l, int u, int i, int ll, int uu, int x, int k) {
lazup(l, u, i, k);
if (l >= ll && u <= uu) {
lazy[i][k] += x;
lazup(l, u, i, k);
return seg[i][k];
}
if (l > uu || u < ll) return seg[i][k];
return seg[i][k] = max(update(l, mid(l, u), lchild(i), ll, uu, x, k),
update(mid(l, u) + 1, u, rchild(i), ll, uu, x, k));
}
int query(int l, int u, int i, int ll, int uu, int k){
lazup(l, u, i, k);
if(l>=ll && u<=uu){
return seg[i][k];
}
if(l>uu || u<ll) return -1e18;
return max(query(l, mid(l, u), lchild(i), ll, uu, k), query(mid(l, u)+1, u, rchild(i), ll, uu, k));
}
void q1(int l, int r, int x){
update(1, n, 0, l, r, x*x + l*l - 2*x*l, 0);
update(1, n, 0, l, r, 2*x - 2*l, 1);
update(1, n, 0, l, r, 1, 2);
}
int q2(int i){
int tp = query(1, n, 0,i, i, 0) + query(1, n, 0,i, i, 1)*i + query(1, n, 0,i, i, 2)*i*i;//16, 24, 9
cout<<tp<<endl;
}
int getr(int l, int r){
int temp = rand;
temp %= (r - l + 1);
return (l + temp);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q;
n = readIntSp(1, 1e5);
q = readIntLn(1, 1e5);
for(int i = 1;i<n;i++){
int ai;
ai = readIntSp(-1e6, 1e6);
update(1, n, 0, i, i, ai, 0);
}
int an;
an = readIntLn(-1e6, 1e6);
update(1, n, 0, n, n, an, 0);
while(q--){
int type;
type = readIntSp(1, 2);
if(type==1){
int l, r, x;
l = readIntSp(1, n);
r = readIntSp(l, n);
x = readIntLn(-1e6, 1e6);
q1(l, r, x);
}
else{
int i;
i = readIntLn(1, n);
q2(i);
}
}
}
Tester's Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>
#ifdef HOME
#include <windows.h>
#endif
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
using namespace std;
template<typename T>
struct FenwickTree
{
vector<T> data;
void init(size_t n)
{
data.resize(n);
}
void init(const vector<T>& v)
{
data.resize(v.size());
forn(i, v.size())
inc(i, v[i]);
}
void fastInit(const vector<T>& v)
{
data = v;
forn(i, v.size())
{
size_t np = i | (i + 1);
if (np < v.size())
data[np] += data[i];
}
}
void inc(size_t pos, T val)
{
for (; pos < data.size(); pos |= pos + 1)
{
data[pos] += val;
}
}
void incRange(size_t posL, size_t posR, T val)
{
inc(posL, val);
inc(posR+1, -val);
}
T sum(size_t pos) const
{
T ret = 0;
for (++pos; pos; pos &= pos - 1)
ret += data[pos - 1];
return ret;
}
};
long long readInt(long long l, long long r, char endd) {
long long x = 0;
int cnt = 0;
int fi = -1;
bool is_neg = false;
while (true) {
char g = getchar();
if (g == '-') {
assert(fi == -1);
is_neg = true;
continue;
}
if ('0' <= g && g <= '9') {
x *= 10;
x += g - '0';
if (cnt == 0) {
fi = g - '0';
}
cnt++;
assert(fi != 0 || cnt == 1);
assert(fi != 0 || is_neg == false);
assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
}
else if (g == endd) {
assert(cnt > 0);
if (is_neg) {
x = -x;
}
assert(l <= x && x <= r);
return x;
}
else {
//assert(false);
}
}
}
string readString(int l, int r, char endd) {
string ret = "";
int cnt = 0;
while (true) {
char g = getchar();
assert(g != -1);
if (g == endd) {
break;
}
cnt++;
ret += g;
}
assert(l <= cnt && cnt <= r);
return ret;
}
long long readIntSp(long long l, long long r) {
return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
return readString(l, r, ' ');
}
int main(int argc, char** argv)
{
#ifdef HOME
if(IsDebuggerPresent())
{
freopen("../in.txt", "rb", stdin);
freopen("../out.txt", "wb", stdout);
}
#endif
uint32_t N = readIntSp(1, 100'000);
uint32_t Q = readIntLn(1, 100'000);
vector<int64_t> A(N);
int ctr = 0;
for (auto& ai : A)
{
++ctr;
if (ctr == N)
ai = readIntLn(-1'000'000, 1'000'000'000);
else
ai = readIntSp(-1'000'000, 1'000'000'000);
}
FenwickTree<int64_t> FenwickConstant, FenwickLinear, FenwickQuadr;
FenwickConstant.init(N);
FenwickLinear.init(N);
FenwickQuadr.init(N);
forn(q, Q)
{
int qtype = readIntSp(1, 2);
if (qtype == 1)
{
size_t L = readIntSp(1, N);
size_t R = readIntSp(1, N);
int64_t X = readIntLn(-1'000'000'000, 1'000'000'000);
X -= L;
--L;
--R;
FenwickConstant.incRange(L, R, X * X);
FenwickLinear.incRange(L, R, 2 * X);
FenwickQuadr.incRange(L, R, 1);
}
else
{
size_t P = readIntLn(1, N);
--P;
int64_t res = A[P] + FenwickConstant.sum(P) +
(P + 1) * FenwickLinear.sum(P) +
(P + 1) * (P + 1) * FenwickQuadr.sum(P);
printf("%lld\n",res);
}
}
return 0;
}
Editorialist's Solution
/*
* @author: vichitr
* @date: 26th June 2021
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
// Template taken from cp-algorithms
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n + 1;
bit.assign(n + 1, 0ll);
}
void add(int idx, int val) {
for (++idx; idx < n; idx += idx & -idx)
bit[idx] += val;
}
void range_add(int l, int r, int val) {
add(l, val);
add(r + 1, -val);
}
int get(int idx) { //
int ret = 0;
for (++idx; idx > 0ll; idx -= idx & -idx)
ret += bit[idx];
return ret;
}
};
void solve() {
int n, q; cin >> n >> q;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
FenwickTree B1(n), B2(n), B3(n);
while (q--) {
int type;; cin >> type;
if (type == 1) {
int l, r, x; cin >> l >> r >> x;
int y = x - l;
l--, r--;
B1.range_add(l, r, y * y);
B2.range_add(l, r, y * 2);
B3.range_add(l, r, 1ll);
}
else {
int i; cin >> i;
int ans = a[i - 1] + B1.get(i - 1) + i * B2.get(i - 1) + i * i * B3.get(i - 1);
cout << ans << '\n';
}
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
VIDEO EDITORIAL:
If you have other approaches or solutions, let’s discuss in comments.