Submission #484782

# Submission time Handle Problem Language Result Execution time Memory
484782 2021-11-05T13:10:02 Z fuad27 Addk (eJOI21_addk) C++17
64 / 100
387 ms 5816 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_SEGMENT_TREE_SIZE = 100000 + 10;
template<class T> class segment_tree {
    int n = MAX_SEGMENT_TREE_SIZE;
    T t[2*MAX_SEGMENT_TREE_SIZE + 2] = {0};
    public:
        T intializer = 0;
        std::function<T(T,T)> fun;
        void build(T arr[], int sz) {
            for(int i = 0;i<n;i++) {
                t[i+n] = arr[i];
            }
            for(int i = n-1;i>0;i--) {
                t[i] = fun(t[i*2], t[i*2 + 1]);
            }
        }
        void modify(int p, T value) {
            for(t[p+=n]=value;p>1;p>>=1) {
                t[p>>1] = fun(t[p], t[p^1]);
            }
        }
        T query(int l, int r) {
	    swap(l, r);
	    r++,l++;
            T ans = intializer;
            for(l+=n,r+=n;l<r;l>>=1,r>>=1) {
                if(l&1)ans=fun(ans, t[l++]);
                if(r&1)ans=fun(ans, t[--r]);
            }
            return ans;
        }
};
 
long long op(long long a, long long b) {
    return a+b;
}
int32_t main () {
	int n, k;
	cin >> n >> k;
	segment_tree<int> s1;
	s1.fun = op;
	segment_tree<int> s2;
	s2.fun = op;
	int arr[n+1]/*, prefix1[n+1] = {0}, prefix2[n+1] = {0}*/;
	arr[0] = 0;
	for(int i = 1;i<=n;i++) {
		cin >> arr[i];
		//prefix2[i] = prefix2[i-1] + arr[i]*i;
		//prefix1[i] = prefix1[i-1] + arr[i];
	}
	s1.build(arr, n+1);
	for(int i = 1;i<=n;i++) {
		arr[i]*=i;
	}

	s2.build(arr, n+1);
	for(int i = 1;i<=n;i++) {
		arr[i]/=i;
	}


	int q=1;
	cin >> q;
	int asdf[k];
	while(q--) {
		int asdf1;
		cin >> asdf1;
		if(asdf1 == 1) {
			for(int i = 0;i<k;i++) {
				cin >> asdf[i];
			}
			reverse(asdf, asdf+k);
			long long prev = arr[asdf[k-1]];
			for(int i = 0;i<k;i++) {
				swap(prev, arr[asdf[i]]);
			}
			for(int i = 0;i<k;i++) {
				s1.modify(asdf[i], arr[asdf[i]]);
				s2.modify(asdf[i], arr[asdf[i]]*asdf[i]);
			}
		}
		else {
			int ans = 0;
			int l, r, m;
			cin >> l >> r >> m;
			m = min(r-l-m + 2, m);
			int a = s2.query(l+m-1, l-1) - s1.query(l+m-1, l-1)*(l-1);
			//int a1 = (prefix2[l+m-1] - prefix2[l-1])-(prefix1[l+m-1] - prefix1[l-1])*(l-1);
			//int b1 = (prefix1[r-m] - prefix1[l+m-1])*m;
			int b = s1.query(r-m, l+m-1)*m;
			//int c1 = (prefix1[r] - prefix1[r-m])*(r+1) - (prefix2[r] - prefix2[r-m]);
			int c = s1.query(r, r-m)*(r+1) - s2.query(r, r-m);
		        cout<<a+b+c<<endl;
		}
	}
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:85:8: warning: unused variable 'ans' [-Wunused-variable]
   85 |    int ans = 0;
      |        ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 3404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 61 ms 3712 KB Output is correct
2 Correct 100 ms 4224 KB Output is correct
3 Correct 125 ms 4420 KB Output is correct
4 Correct 220 ms 5184 KB Output is correct
5 Correct 311 ms 5816 KB Output is correct
6 Correct 286 ms 5732 KB Output is correct
7 Correct 304 ms 5700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 4180 KB Output is correct
2 Correct 303 ms 5208 KB Output is correct
3 Correct 387 ms 4908 KB Output is correct
4 Correct 364 ms 5600 KB Output is correct