이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 100007
#define MOD 1000000007
tuple<int, int, int> hw[MAX];
int p[MAX];
int sumsz[MAX];
int storeSqrSum = 0;
int compute(int x){
return ( (x * (x+1))/2 ) %MOD;
}
void activate(int a){
sumsz[a] = get<1>(hw[a]);
storeSqrSum += compute(get<1>(hw[a]));
storeSqrSum %= MOD;
}
int root(int a){
if (a == p[a]) return a;
return p[a] = root(p[a]);
}
void merge(int a, int b){
a = root(a); b = root(b);
if (a == b) return;
p[a] = b;
storeSqrSum -= compute(sumsz[a]);
storeSqrSum += MOD; storeSqrSum %= MOD;
storeSqrSum -= compute(sumsz[b]);
storeSqrSum += MOD; storeSqrSum %= MOD;
sumsz[b] += sumsz[a]; //sumsz[b] %= MOD; //????????????????????
storeSqrSum += compute(sumsz[b]);
storeSqrSum %= MOD;
}
int rainbow(int start, int end){
if ( (end-start)%2 ){
//nice, no mid element
int res = start+end;
return (res * ((end-start+1)/2))%MOD;
}
else{
int res = start+end;
return ((res * ((end-start+1)/2))%MOD + (start+end)/2)%MOD;
}
}
main(){
int n; cin >> n;
set<int> heights;
for (int x = 0; x < n; x++) cin >> get<0>(hw[x]), heights.insert(get<0>(hw[x]));
for (int x = 0; x < n; x++) cin >> get<1>(hw[x]), get<2>(hw[x]) = x;
sort(hw, hw+n);
reverse(hw, hw+n);
for (int x = 0; x < n; x++) p[x] = x, sumsz[x] = 0;
int ptr = 0;
int ans = 0;
heights.insert(0);
for (auto it = heights.rbegin(); it != heights.rend(); it++){
if (*it == 0) break;
while (ptr != n && get<0>(hw[ptr]) == *it){
int idx = get<2>(hw[ptr]);
activate( idx );
if (idx != 0 && sumsz[idx-1] != 0) merge(idx, idx-1);
if (idx != n-1 && sumsz[idx+1] != 0) merge(idx, idx+1);
ptr++;
}
int curh = *it, nexth = (*next(it));
ans += (storeSqrSum * rainbow(nexth+1, curh))%MOD;
ans %= MOD;
//cerr << curh << ' ' << nexth << ' ' << storeSqrSum << ' ' << (storeSqrSum * rainbow(nexth+1, curh))%MOD << '\n';
}
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
fancyfence.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |