http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1369
The problem you need to solve here is pretty simple. You are give a function f(A, n), where A is an array of integers and n is the number of elements in the array. f(A, n) is defined as follows:
long long f( int A[], int n ) { // n = size of A
long long sum = 0;
for( int i = 0; i < n; i++ )
for( int j = i + 1; j < n; j++ )
sum += A[i] - A[j];
return sum;
}
Given the array A and an integer n, and some queries of the form:
1) 0 x v (0 ≤ x < n, 0 ≤ v ≤ 106), meaning that you have to change the value of A[x] to v.
2) 1, meaning that you have to find f as described above.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers: n and q (1 ≤ n, q ≤ 105). The next line contains n space separated integers between 0 and 106 denoting the array A as described above.
Each of the next q lines contains one query as described above.
Output
For each case, print the case number in a single line first. Then for each query-type "1" print one single line containing the value of f(A, n).
Sample Input
1
3 5
1 2 3
1
0 0 3
1
0 2 1
1
Sample Output
Case 1:
-4
0
4
Note
Dataset is huge, use faster I/O methods.
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
struct node {
int x;ll l;
node(){}
node(int x,ll l):x(x),l(l) {}
bool operator < (const node a) const { return l>a.l; }
};
int tot,u[N],v[N],p[N],pre[N];
int n,m,q[N],x[N],y[N],c[N],w[N];
void add(int a,int b,int c) {
v[tot]=b;p[tot]=c;pre[tot]=u[a];u[a]=tot++;
}
ll dis[N];
priority_queue<node>Q;
ll getmin() {
node now;
ll ret=0;memset(q,0,sizeof(q));
for (int i=1;i<=m;i++) dis[i]=MAX;
for (int i=1;i<=m;i++)
if (w[i]==0) dis[i]=0,Q.push(node(i,0));
while (!Q.empty()) {
now=Q.top();Q.pop();
if (q[now.x]) continue ;q[now.x]=1;
for (int i=u[now.x];i!=-1;i=pre[i])
if (dis[v[i]]>now.l+p[i]) {
dis[v[i]]=now.l+p[i];Q.push(node(v[i],dis[v[i]]));
}
}
for (int i=1;i<=m;i++)
if (w[i]==2&&dis[i]==MAX) return -1;
else if (w[i]==2) ret+=dis[i];
return ret;
}
int main()
{
int i,t,ca;
scanf("%d", &t);
for (ca=1;ca<=t;ca++) {
scanf("%d%d", &n, &m);
for (i=1;i<=n;i++) scanf("%d", &x[i]);
for (i=1;i<=n;i++) scanf("%d", &y[i]);
for (i=1;i<=n;i++) scanf("%d", &c[i]);
for (i=1;i<=m;i++) scanf("%d", &w[i]);
tot=0;memset(u,-1,sizeof(u));
for (i=1;i<=n;i++) add(y[i],x[i],c[i]);
printf("Case #%d: %I64d\n", ca, getmin());
}
return 0;
}