-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEstimatingProgress.java
More file actions
184 lines (147 loc) · 4.43 KB
/
EstimatingProgress.java
File metadata and controls
184 lines (147 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeSet;
/**
* Created by arpit on 28/12/16.
*/
public class EstimatingProgress {
final static int MAX=50000;
static TreeSet<Integer>set=new TreeSet<>();
static Integer a[]=new Integer[MAX];
static Query q[]=new Query[MAX];
static long queryAns[]=new long[MAX];
static int counts[]=new int[1000010];
static int blockSize;
static long ans;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,m;
String []s;
n=Integer.parseInt(br.readLine());
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i]=Integer.parseInt(s[i]);
}
//input no. of queries
m=Integer.parseInt(br.readLine());
for (int i = 0; i < m; i++) {
s=br.readLine().split("\\s");
q[i]=new Query(Integer.parseInt(s[0])-1,Integer.parseInt(s[1])-1,i);
}
blockSize= (int) Math.sqrt(n);
solve(n, m);
}
private static void solve(int n, int m) {
Arrays.sort(q,0,m);
/* for (int i = 0; i < m; i++) {
System.out.print(q[i] + " ");
}
System.out.println();*/
/* int currL=q[0].l,currR=q[0].r;
for (int i =currL; i <=currR ; i++) {
add(i);
}
queryAns[q[0].idx]=ans;
currR++;*/
int currL=0,currR=0;
for (int i = 0; i <m ; i++)
{
int l=q[i].l,r=q[i].r;
while (currL<l){
remove(currL);
currL++;
}
while (currL>l){
add(currL-1);
currL--;
}
while (currR<=r){
add(currR);
currR++;
}
while (currR>(r+1)){
remove(currR-1);
currR--;
}
queryAns[q[i].idx]=ans;
}
for (int i = 0; i < m; i++) {
System.out.println(queryAns[i]);
}
}
private static void remove(int pos) {
counts[a[pos]]--;
if (counts[a[pos]]==0){
set.remove(a[pos]);
Integer prev=set.lower(a[pos]);
Integer next=set.higher(a[pos]);
if (prev!=null){
ans-=(long)(a[pos]-prev)*(a[pos]-prev);
}
if (next!=null){
ans-=(long)(next-a[pos])*(next-a[pos]);
}
if (next!=null && prev!=null)
ans+=(long)(next-prev)*(next-prev);
}
}
private static void add(int pos) {
//System.out.println(pos+" "+xenny[pos]);
if (counts[a[pos]]>0) {
counts[a[pos]]++;
return;
}
counts[a[pos]]++;
set.add(a[pos]);
Integer prev=set.lower(a[pos]);
Integer next=set.higher(a[pos]);
if (prev!=null){
ans+=(long)(a[pos]-prev)*(a[pos]-prev);
}
if (next!=null){
ans+=(long)(next-a[pos])*(next-a[pos]);
}
if (next!=null && prev!=null)
ans-=(long)(next-prev)*(next-prev);
/*if (set.add(xenny[pos]))
{
Integer prev=set.lower(xenny[pos]);
Integer next=set.higher(xenny[pos]);
if (prev!=null){
ans+=(long)(xenny[pos]-prev)*(xenny[pos]-prev);
}
if (next!=null){
ans+=(long)(next-xenny[pos])*(next-xenny[pos]);
}
if (next!=null && prev!=null)
ans-=(long)(next-prev)*(next-prev);
}*/
}
static class Query implements Comparable<Query>{
Integer l,r,idx;
public Query(Integer l, Integer r, Integer idx) {
this.l = l;
this.r = r;
this.idx = idx;
}
@Override
public int compareTo(Query query) {
Integer a=this.l/blockSize;
Integer b=query.l/blockSize;
if (a!=b)
return a.compareTo(b);
return this.r.compareTo(query.r);
}
@Override
public String toString() {
return "Query{" +
"l=" + l +
", r=" + r +
", idx=" + idx +
'}';
}
}
}