Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

 
 
 
 
General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
33233031 Practice:
sdssudhu
86D - 36 C++14 (GCC 6-32) Accepted 4648 ms 14772 KB 2017-12-14 10:49:32 2017-12-14 10:49:32
→ Source
//Solution of dishant_18 used by sdssudhu for verification

#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
 
#define dbg(x) cerr << #x << " is " << x << " "
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&y)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
 
const ll mod = 1000000007;
const ll inf = (ll)1e17;
const ld eps = 1e-12;
const ll N = 10000005;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
double tick(){static clock_t oldt;clock_t newt=clock();double diff=1.0*(newt-oldt)/CLOCKS_PER_SEC;oldt = newt;return diff;}
ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}


//Declaration

int n,q;
int block_size;											// Sqrt(n)
ll current_answer=0ll;										//Stores current answer of queries
ll cnt[(int)1e6+5]={0};									//Frequency array
int a[(int)3e5+5]={0};									//Input array 
ll answers[(int)2e5+5]={0};								// Stores answers in the way we want to output them
pair< pair<int,int>,int > queries[(int)2e5+5]; 			// first.first -> L   first.second -> R  second -> query no.(for output purpose)

//Comparison function to sort queries for precomputation

bool comp(pair< pair<int,int>,int >& p,pair< pair<int,int>,int >& q)
{
	int block_p=p.first.first/block_size;
	int block_q=q.first.first/block_size;
	if(block_p!=block_q)
	{
		return block_p<block_q;
	}
	else
		return p.first.second<q.first.second;
}
 
//Functions for computing required operation in O(1) or O(logn) (Here it is O(1)) 
inline void add(int val)
{
	current_answer-=(cnt[val]*cnt[val])*val;
	cnt[val]++;
	current_answer+=(cnt[val]*cnt[val])*val;
}

inline void remove(int val)
{
	current_answer-=(cnt[val]*cnt[val])*val;
	cnt[val]--;
	current_answer+=(cnt[val]*cnt[val])*val;	
}
  
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>q;
    f(i,n)
    	cin>>a[i];
    block_size=static_cast<int> (sqrt(n));
    //cin>>q;
	f(i,q)
	{
		cin>>queries[i].first.first>>queries[i].first.second;
		queries[i].first.first--;
		queries[i].first.second--;
		queries[i].second=i;	
	}	
	sort(queries,queries+q,comp);
	int mo_left=0,mo_right=-1;
	f(i,q)
	{
		int l=queries[i].first.first;
		int r=queries[i].first.second;
		while(mo_left<l)
		{
			remove(a[mo_left]);
			mo_left++;
		}
		while(mo_left>l)
		{
			mo_left--;
			add(a[mo_left]);
		}
		while(mo_right<r)
		{
			mo_right++;
			add(a[mo_right]);
		}
		while(mo_right>r)
		{
			remove(a[mo_right]);
			mo_right--;
		}
		answers[queries[i].second]=current_answer;
	}
	f(i,q)
		cout<<answers[i]<<"\n";
    return 0;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details