QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#387001 | #4002. Easy Fix | wdw | TL | 4ms | 48928kb | C++20 | 3.6kb | 2024-04-11 22:47:21 | 2024-04-11 22:47:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define int long long
const int N=1e5+5;
const double eps=1e-10;
const int mod=998244353;
#define endl '\n'
struct node{
int n=0,m=0;
int cnt=0;
static const int maxn=1e5+5;
int c[maxn];
int lowbit(int x){
return x&(-x);
}
void add(int i,int x){
while(i<=maxn){
c[i]+=x;
i+= lowbit(i);
}
}
int ask(int i){
int ans=0;
while(i){
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
struct dian{
int x,y,id;
inline bool operator<(const dian &a)const{
return x<a.x;
}
}v[maxn];
struct que{
int x,y,k,id;
inline bool operator<(const que &a)const{
return x<a.x;
}
}que[4*maxn];
void vadd(int x,int y){
v[++n].x=x;
v[n].y=y;
}
void vque(int x1,int y1,int x2,int y2,int id){
que[++cnt]={x1-1,y1-1,1,id};
que[++cnt]={x1-1,y2,-1,id};
que[++cnt]={x2,y1-1,-1,id};
que[++cnt]={x2,y2,1,id};
}
void get(int *a){
sort(v+1,v+1+n);
sort(que+1,que+1+cnt);
int l=1;
for(int i=1;i<=cnt;i++){
while(l<=n&&v[l].x<=que[i].x){
add(v[l].y,1);
l++;
}
a[que[i].id]+=que[i].k*ask(que[i].y);
}
return;
}
}t1,t2,t3,t4,t5,t6,t7,tt
;
const int MAXN=2e5+5;
int ans1[MAXN], ans2[MAXN], ans3[MAXN], ans4[MAXN], ans5[MAXN],ans6[MAXN], ans7[MAXN];
void solve(){
int n;
cin>>n;
vector<int>a(n+5),b(n+5),p(n+5);
int sum=0;
for(int i=1;i<=n;i++){
cin>>p[i];
a[i]=tt.ask(p[i]);
b[i]=p[i]-a[i]-1;
tt.add(p[i],1);
if(a[i]==b[i]){
t1.vadd(i,p[i]);
}else if(a[i]==b[i]+1){
t2.vadd(i,p[i]);
}else if(a[i]>=b[i]+2){
t3.vadd(i,p[i]);
}else if(a[i]+1==b[i]){
t4.vadd(i,p[i]);
}else if(a[i]+2<=b[i]){
t5.vadd(i,p[i]);
}
t6.vadd(i,p[i]);
t7.vadd(i,p[i]);
sum+=min(a[i],b[i]);
}
int m;
cin>>m;
vector<int>x1(m+5),x2(m+5);
for(int i=1;i<=m;i++){
int l,r;
cin>>l>>r;
if(l>r){
swap(l,r);
}
x1[i]=l,x2[i]=r;
if(l==r)continue;
int xl=min(p[l],p[r]),yr=max(p[l],p[r]);
l++,r--;
t1.vque(l,xl,r,yr,i);
t2.vque(l,xl,r,yr,i);
t3.vque(l,xl,r,yr,i);
t4.vque(l,xl,r,yr,i);
t5.vque(l,xl,r,yr,i);
t6.vque(l,1,r+1,p[l-1]-1,i);
t7.vque(l-1,1,r,p[r+1]-1,i);
}
t1.get(ans1);
t2.get(ans2);
t3.get(ans3);
t4.get(ans4);
t5.get(ans5);
t6.get(ans6);
t7.get(ans7);
for(int i=1;i<=m;i++){
if(x1[i]==x2[i]){
cout<<sum<<endl;
}else{
int ans=0;
if(p[x1[i]]<p[x2[i]]){
ans+=sum-ans1[i]+ans3[i]-ans4[i]-ans5[i];
}else{
ans+=sum-ans1[i]-ans2[i]-ans3[i]+ans5[i];
}
ans+=(min(a[x1[i]]+ans6[i],b[x1[i]]-ans6[i]))-min(a[x1[i]],b[x1[i]]);
ans+=(min(a[x2[i]]-ans7[i],b[x2[i]]+ans7[i]))-min(a[x2[i]],b[x2[i]]);
cout<<ans<<endl;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
//cout<<fixed<<setprecision(12);
int T=1;
// cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 46540kb
input:
7 1 6 2 7 5 4 3 7 1 7 2 6 3 5 4 4 1 1 2 1 3 7
output:
7 6 6 7 7 6 8
result:
ok 7 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 46692kb
input:
5 5 3 1 2 4 3 3 1 2 5 3 3
output:
3 0 0
result:
ok 3 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 48928kb
input:
10 10 1 6 7 9 8 4 3 5 2 10 6 7 5 2 4 1 10 4 6 2 1 9 2 9 4 5 6 8 9 7
output:
12 7 13 8 7 15 9 11 11 12
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 48696kb
input:
20 3 4 19 18 5 6 20 12 9 13 1 10 8 7 17 16 2 14 11 15 20 19 9 20 6 1 14 11 15 12 10 20 15 10 3 20 1 16 7 8 19 3 17 9 2 20 14 20 2 20 9 2 4 12 4 9 15 7 16 9 4
output:
46 48 43 50 44 42 48 45 42 45 39 41 50 46 48 44 50 42 42 48
result:
ok 20 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 48652kb
input:
100 70 54 10 72 81 84 56 15 27 19 43 100 49 44 52 33 63 40 95 17 58 2 51 39 22 18 82 1 16 99 32 29 24 94 9 98 5 37 47 14 42 73 41 31 79 64 12 6 53 26 68 67 89 13 90 4 21 93 46 74 75 88 66 57 23 7 25 48 92 62 30 8 50 61 38 87 71 34 97 28 80 11 60 91 3 35 86 96 36 20 59 65 83 45 76 77 78 69 85 55 100 ...
output:
1080 1083 1056 1056 1087 1058 1082 1085 1116 1088 1072 1084 1096 1094 1072 1092 1090 1102 1096 1084 1086 1057 1062 1102 1080 1086 1082 1082 1082 1088 1080 1086 1122 1104 1086 1084 1113 1088 1060 1097 1100 1084 1089 1089 1082 1115 1087 1082 1087 1092 1086 1080 1084 1085 1082 1107 1072 1092 1061 1069 ...
result:
ok 100 lines
Test #6:
score: 0
Accepted
time: 0ms
memory: 46868kb
input:
150 100 27 33 124 22 137 14 109 35 92 54 69 67 96 97 3 19 98 120 28 36 16 84 135 20 9 141 62 48 87 103 18 34 76 122 138 50 1 26 70 117 134 72 121 40 17 142 82 91 53 118 37 111 15 149 60 104 93 46 73 126 47 140 59 132 144 143 78 71 38 85 68 74 31 102 4 139 2 65 45 25 7 29 150 131 21 146 130 148 11 89...
output:
2844 2818 2837 2818 2813 2796 2774 2849 2862 2821 2832 2846 2816 2840 2844 2826 2824 2818 2749 2830 2818 2822 2799 2826 2801 2826 2823 2828 2760 2824 2851 2818 2790 2850 2797 2880 2777 2779 2825 2824 2825 2818 2844 2825 2818 2818 2825 2834 2858 2834 2833 2842 2795 2842 2856 2802 2825 2836 2833 2804 ...
result:
ok 109 lines
Test #7:
score: 0
Accepted
time: 3ms
memory: 48832kb
input:
1001 683 346 384 352 291 367 447 360 490 102 779 900 781 642 299 293 619 742 834 434 817 171 341 561 122 471 145 90 769 457 882 256 525 509 338 41 851 269 96 630 751 297 347 687 903 57 80 771 393 921 82 300 580 579 507 660 463 551 578 902 626 33 927 260 822 670 116 210 349 301 166 631 390 395 860 75...
output:
121741 122016 121953 121923 122061 121986 121953 121867 121695 122378 122035 121961 121937 122668 122276 122063 122168 122565 122176 122037 122001 122599 121741 122053 122082 121891 121699 122031 122071 122077 122049 122065 122207 121927 122039 122050 122087 121807 121984 122029 121982 122033 122033...
result:
ok 2002 lines
Test #8:
score: 0
Accepted
time: 0ms
memory: 48640kb
input:
100 60 58 61 71 6 87 9 82 10 77 51 72 36 97 92 21 68 32 81 20 3 29 52 93 88 94 16 48 50 67 11 55 84 57 38 65 25 41 66 59 19 56 100 49 86 85 34 31 80 2 28 33 15 7 69 26 45 91 75 23 95 22 35 4 8 17 54 79 74 27 53 64 39 12 1 24 13 47 98 5 96 43 42 30 37 90 63 99 73 18 14 40 46 78 70 76 62 83 44 89 200 ...
output:
1073 1086 1083 1095 1076 1079 1079 1094 1070 1095 1080 1084 1087 1080 1056 1116 1086 1074 1090 1096 1100 1076 1080 1070 1078 1090 1086 1092 1068 1084 1086 1084 1080 1079 1084 1082 1082 1060 1051 1098 1078 1097 1089 1078 1078 1087 1083 1066 1089 1075 1087 1089 1080 1095 1076 1081 1090 1080 1100 1096 ...
result:
ok 200 lines
Test #9:
score: 0
Accepted
time: 0ms
memory: 44508kb
input:
200 59 41 64 113 107 121 20 92 106 160 197 181 83 166 36 54 152 180 52 176 22 190 153 10 94 21 125 75 74 24 159 198 175 102 79 188 6 95 40 30 154 115 46 45 42 194 7 48 185 39 189 157 186 66 87 85 1 12 31 78 3 119 127 104 26 29 124 53 184 2 70 196 76 110 34 179 8 112 109 18 132 35 123 65 44 163 191 1...
output:
4825 4773 4757 4781 4777 4785 4782 4768 4794 4814 4869 4789 4859 4824 4767 4777 4806 4794 4789 4869 4781 4779 4803 4822 4766 4737 4773 4733 4795 4777 4795 4907 4783 4786 4756 4746 4743 4798 4784 4789 4835 4805 4767 4787 4803 4783 4773 4808 4763 4780 4793 4759 4783 4796 4840 4798 4814 4799 4777 4797 ...
result:
ok 100 lines
Test #10:
score: 0
Accepted
time: 4ms
memory: 48804kb
input:
1000 662 811 263 46 591 511 230 223 628 603 716 880 187 575 701 184 505 754 130 154 367 91 704 945 171 588 19 486 34 513 622 849 629 437 776 290 743 470 587 681 723 271 879 642 77 281 71 655 365 942 809 94 266 148 146 305 198 311 788 69 343 277 887 557 169 41 784 791 451 812 932 663 594 576 487 946 ...
output:
121683 121539 121718 121831 121737 121628 121167 121818 121731 121681 121693 121250 121364 121745 121395 121733 121183 121711 121758 121745 121781 121237 121552 121964 121711 121814 121807 121735 121385 121746 121793 121779 121740 121725 122351 121731 121722 121742 121552 121741 121785 121503 121852...
result:
ok 1000 lines
Test #11:
score: 0
Accepted
time: 0ms
memory: 42612kb
input:
1005 317 805 235 278 778 826 874 73 679 24 943 520 831 781 645 406 34 43 701 939 56 304 836 620 696 238 1003 957 949 795 295 941 692 484 4 27 561 85 399 80 174 856 199 382 502 144 40 748 218 698 613 250 806 49 55 606 258 297 162 254 982 355 835 102 325 59 689 403 270 167 202 777 246 448 120 300 678 ...
output:
122764 122590 122214 122778 122862 122501 122904 122745 122888 122630 122820 122856 122782 122592 122732 122792 122916 122819 123056 123002 122670 122456 122686 122756 122788 122698 122738 122658 122786 122954 122784 123040 122765 122972 122950 122788 123348 122684 122780 122891 122756 122864 122764...
result:
ok 1005 lines
Test #12:
score: 0
Accepted
time: 4ms
memory: 42740kb
input:
2000 1869 1841 1361 1803 1559 1740 1235 1362 805 1804 165 681 883 181 562 922 1576 1704 271 1260 811 399 475 1775 1519 83 1345 1666 713 798 578 443 666 867 1080 597 665 148 136 1840 846 1054 1566 1537 910 1712 1662 499 958 687 1124 1865 1284 1922 30 1194 1467 1839 1101 1426 1262 381 1812 975 473 182...
output:
503789 503648 504546 503861 504189 503890 503766 502595 503686 503684 504276 503657 503697 503032 503966 503624 503070 503669 503315 503798 503746 503648 502713 503862 503688 503966 502783 503628 503800 504532 504050 503406 503578 503464 503698 503566 503814 503222 503836 504364 503662 503722 503315...
result:
ok 2000 lines
Test #13:
score: 0
Accepted
time: 4ms
memory: 42612kb
input:
1500 1162 964 138 1372 386 1494 420 883 251 1371 663 1218 1110 1241 581 267 1169 901 531 389 416 487 428 94 792 1177 378 1175 1071 1439 817 1222 1174 1076 577 1236 827 97 345 1369 1238 1077 1366 59 184 855 897 1227 1411 1220 556 333 529 503 437 1297 539 1114 1082 284 672 186 979 185 618 493 1009 963...
output:
280833 280859 281123 280954 280912 281018 280572 281182 281128 280828 281016 281062 280788 281554 281002 281135 281096 281102 281043 280970 281022 281222 281015 280984 280944 281022 280985 281332 281244 281020 281119 280839 280892 280936 280942 281024 281048 280900 281086 281022 281016 281017 280661...
result:
ok 1500 lines
Test #14:
score: -100
Time Limit Exceeded
input:
50001 32799 28330 26898 17795 36551 11914 18140 12319 4684 45329 23187 30307 48712 42400 44352 17220 48100 17699 5773 9592 35944 31214 37208 1896 24298 20883 1764 46297 5828 23488 9300 35166 40644 1194 34625 5792 15396 24610 36373 46762 14317 40124 11778 33884 40392 17464 41852 29411 28407 42896 130...