QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282424 | #7072. Photograph | VladM | RE | 0ms | 0kb | C++14 | 2.1kb | 2023-12-12 00:11:53 | 2023-12-12 00:11:53 |
answer
#include <bits/stdc++.h>
using namespace std;
#define DIM 100007
long long d, h[DIM], p[2 * DIM], H[2 * DIM], ans, sum, it1, it2;
int n, q, it;
set<int> s;
int main()
{
scanf("%lld %lld", &n, &q);
for(int i = 1; i <= n; i++) scanf("%lld", &h[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &p[i]);
for(int i = 1; i <= n; i++) H[i] = h[p[i]];
for(int i = n + 1; i <= 2 * n; i++) H[i] = H[i - n];
for(int i = n + 1; i <= 2 * n; i++) p[i] = p[i - n];
it = 1;
for(int tst = 1; tst <= q + 1; tst++){
if(tst != 1){
scanf("%lld", &d);
d += ans;
it = (it - 1 + d + n) % n + 1;
}
sum = 0;
ans = 0;
s.clear();
for(int i = it; i <= it + n - 1; i++){
if(s.empty()){
s.insert(p[i]);
continue;
}
if(s.size() == 1){
it1 = *s.begin();
sum += (H[i] - h[it1]) * (H[i] - h[it1]);
ans += sum;
s.insert(p[i]);
continue;
}
it1 = *s.begin();
if(p[i] < it1){
//cout << it1 << ' ' << H[i] << endl;
sum += (H[i] - h[it1]) * (H[i] - h[it1]);
ans += sum;
s.insert(p[i]);
continue;
}
it1 = *(--s.end());
if(p[i] > it1){
//cout << it1 << ' ' << H[i] << endl;
sum += (H[i] - h[it1]) * (H[i] - h[it1]);
ans += sum;
s.insert(p[i]);
continue;
}
auto LB = s.lower_bound(p[i]);
it1 = *(LB);
it2 = *(--LB);
it1 = h[it1];
it2 = h[it2];
//cout << it1 << ' ' << H[i] << ' ' << it2 << endl;
sum -= (it1 - it2) * (it1 - it2);
sum += (H[i] - it1) * (H[i] - it1);
sum += (H[i] - it2) * (H[i] - it2);
ans += sum;
s.insert(p[i]);
}
printf("%lld\n", ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 4 1 2 3 4 5 1 2 3 4 5 6 6 8 10