QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19182 | #1877. Matryoshka Dolls | BrotherCall | WA | 35ms | 12668kb | C++14 | 3.1kb | 2022-01-28 13:17:41 | 2022-05-06 04:23:33 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#define LL (long long)
using namespace std;
const int N = 5e5 + 100;
int n , m , t;
int a[N];
int fans[N];
struct edge{
int x , y;
int seat;
}G[N];
set<pair<int , int> >s;
set<pair<int , int> >::iterator it;
set<pair<int , int> >::iterator it2;
set<pair<int , int> >::iterator it3;
inline int read(){
int a=0,b=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')
b=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
a=(a<<3)+(a<<1)+c-'0';
c=getchar();
}
return a*b;
}
bool compare(edge s1 , edge s2) {
if(s1.x / t == s2.x / t) {
if((s1.x / t) & 1) return s1.y < s2.y;
else return s1.y > s2.y;
}
return s1.x / t < s2.x / t;
}
int main() {
n = read() , m = read();
t = sqrt(n);
for(int i=1;i<=n;i++)
a[i] = read();
for(int i=1;i<=m;i++) {
G[i].x = read();
G[i].y = read();
G[i].seat = i;
}
sort(G + 1 , G + 1 + m , compare);
int l = 1 , r = 0;
long long ans = 0;
for(int i=1;i<=m;i++) {
while(r < G[i].y) {
r ++;
s.insert(make_pair(a[r] , r));
it = s.lower_bound(make_pair(a[r] , 0));
it3 = it2 = it;
it ++;
it2 --;
if(it != s.end() && it3 != s.begin()) ans -= LL(abs(it->second - it2->second));
if(it != s.end()) ans += LL(abs(it->second - it3->second));
if(it3 != s.begin()) ans += LL(abs(it3->second - it2->second));
}
while(l > G[i].x) {
l --;
s.insert(make_pair(a[l] , l));
it = s.lower_bound(make_pair(a[l] , 0));
it3 = it2 = it;
it ++;
it2 --;
if(it != s.end() && it3 != s.begin()) ans -= LL(abs(it->second - it2->second));
if(it != s.end()) ans += LL(abs(it->second - it3->second));
if(it3 != s.begin()) ans += LL(abs(it3->second - it2->second));
}
while(r > G[i].y) {
it = s.lower_bound(make_pair(a[r] , 0));
it3 = it2 = it;
it ++;
it2 --;
if(it != s.end() && it3 != s.begin()) ans += LL(abs(it->second - it2->second));
if(it != s.end()) ans -= LL(abs(it->second - it3->second));
if(it3 != s.begin()) ans -= LL(abs(it3->second - it2->second));
s.erase(it3);
r --;
}
while(l < G[i].x) {
it = s.lower_bound(make_pair(a[l] , 0));
it3 = it2 = it;
it ++;
it2 --;
if(it != s.end() && it3 != s.begin()) ans += LL(abs(it->second - it2->second));
if(it != s.end()) ans -= LL(abs(it->second - it3->second));
if(it3 != s.begin()) ans -= LL(abs(it3->second - it2->second));
s.erase(it3);
l ++;
}
fans[G[i].seat] = ans;
}
for(int i=1;i<=m;i++)
printf("%lld\n" , fans[i]);
fclose(stdin);
fclose(stdout);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 7816kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 3 1 0
result:
ok 5 number(s): "7 5 3 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7824kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 35ms
memory: 12668kb
input:
100000 1 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...
output:
704982704
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'