QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#80588 | #5173. 染色 | LWLAymh | 0 | 777ms | 85700kb | C++14 | 4.0kb | 2023-02-24 12:38:08 | 2023-02-24 12:38:11 |
Judging History
answer
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define ll long long
#define qwq 2000007
inline int read(){
char c=getchar();
int x=0;int flag=0;
for(;c<'0'||c>'9';c=getchar())flag|=(c=='-');
for(;c<='9'&&c>='0';c=getchar())x=(x<<1)+(x<<3)+c-'0';
if(flag)return -x;
return x;
}
int n,q;
int a[qwq];
int flag1[qwq];
struct Line{
int l,r;
}b[qwq],c[qwq];
int cntb,cntc;
bool cmpLine(Line x,Line y){
if(x.r!=y.r)return x.r<y.r;
return x.l>y.l;
}
int L[qwq];
inline void add_line(int l,int r){
// printf("add[%d,%d]\n",l,r);
++cntb;
b[cntb].l=l;b[cntb].r=r;
return ;
}
struct ASK{
int l,r;
int num;
}ask[qwq];
bool cmpASK(ASK x,ASK y){
return x.l<y.l;
}
int ans[qwq];
int T[qwq];
#define pii std::pair<int,int>
#define mp std::make_pair
std::priority_queue<pii>p_que;
struct tree{
int son[2];
int dis;
int val;
int tag;
int ans;
}tr[qwq];
inline void add_v(int x,int v){
tr[x].tag+=v;
tr[x].ans+=v;
return ;
}
inline void pushdown(int x){
if(tr[x].tag){
if(tr[x].son[0])add_v(tr[x].son[0],tr[x].tag);
if(tr[x].son[1])add_v(tr[x].son[1],tr[x].tag);
tr[x].tag=0;
}
return ;
}
int merge(int x,int y){
if(!x)return y;
if(!y)return x;
pushdown(x);
pushdown(y);
if(tr[x].val<tr[y].val)std::swap(x,y);
if(tr[x].val==tr[y].val&&y>x)std::swap(x,y);
tr[y].son[1]=merge(x,tr[y].son[1]);
if(tr[tr[y].son[1]].dis>tr[tr[y].son[0]].dis)std::swap(tr[y].son[0],tr[y].son[1]);
tr[y].dis=tr[tr[y].son[1]].dis+1;
return y;
}
int flagdel[qwq];
inline int delet(int x){
flagdel[x]=1;
pushdown(x);
return merge(tr[x].son[0],tr[x].son[1]);
}
void dfs_pushdown(int x){
pushdown(x);
if(tr[x].son[0]){
dfs_pushdown(tr[x].son[0]);
}
if(tr[x].son[1]){
dfs_pushdown(tr[x].son[1]);
}
return ;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
int lasl=1,lasr=1;
for(int i=2;i<=n;++i){
if(a[i]==a[lasr])++lasr;
else {
add_line(lasl,lasr);
lasl=i;lasr=i;
}
}
add_line(lasl,lasr);
for(int i=1;i<=cntb;++i){
L[i]=b[i].l;
if(b[i].l==b[i].r){
flag1[b[i].l]=1;
}
}
for(int i=1;i<=n;++i)flag1[i]+=flag1[i-1];
for(int i=1;i<=cntb;++i){
if(T[a[b[i].l]]){
int l=T[a[b[i].l]];
int r=i;
if(flag1[b[r].l-1]-flag1[b[l].r]==b[r].l-1-b[l].r){
++cntc;
c[cntc].l=l,c[cntc].r=r;
}
// printf("[%d,%d]\n",l,r);
}
T[a[b[i].l]]=i;
}
std::sort(c+1,c+cntc+1,cmpLine);
int num=0;
for(int i=1;i<=q;++i){
++num;
int x=read(),y=read();
if(x>y)std::swap(x,y);
if(x==y){
--q;--i;
continue;
}
int l=std::upper_bound(L+1,L+cntb+1,x)-L-1;
int r=std::upper_bound(L+1,L+cntb+1,y)-L-1;
// printf("[%d,%d]\n",l,r);
ans[num]=r-l+y-x;
ask[i].l=l;ask[i].r=r;ask[i].num=num;
int R=l,cnt=0;
// for(int j=1;j<=cntc;++j){
// if(c[j].r>r)continue;
// if(c[j].l>=R){
// R=c[j].r;++cnt;
// }
// }
// printf("%d\n",ans[i]-cnt);
}
std::sort(ask+1,ask+q+1,cmpASK);
// for(int i=1;i<=q;++i){
// printf("ask[%d,%d]:%d\n",ask[i].l,ask[i].r,ask[i].num);
// }
int lasask=1,lasc=1;
for(int r=1;r<=cntb;++r){
// printf("nowwork%d\n",r);
for(;lasask<=q&&ask[lasask].l==r;++lasask){
p_que.push(mp(-ask[lasask].l,lasask));
tr[lasask].val=ask[lasask].r;
}
// printf("qwq\n");
for(;lasc<=cntc&&c[lasc].r==r;++lasc){
int nowx=0;
while(!p_que.empty()){
int x=p_que.top().second,R=-p_que.top().first;
if(ask[x].r<r){
int x=delet(x);
p_que.pop();
if(x!=0)p_que.push(mp(-ask[x].r,x));
continue;
}
if(R>c[lasc].l){
break;
}
// printf("nowget:%d\n",x);
nowx=merge(nowx,x);
p_que.pop();
}
if(nowx!=0){
add_v(nowx,1);
p_que.push(mp(-c[lasc].r,nowx));
}
}
}
// printf("initOK\n");
while(!p_que.empty()){
int x=p_que.top().second;
dfs_pushdown(x);
p_que.pop();
}
for(int i=1;i<=q;++i){
ans[ask[i].num]-=tr[i].ans;
// printf("%d:%d\n",ask[i].num,tr[i].ans);
}
for(int i=1;i<=num;++i){
printf("%d\n",ans[i]);
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 19508kb
input:
10000 100 84 85 52 2 78 53 20 21 23 76 37 44 18 5 37 8 81 65 46 58 69 1 69 37 53 46 37 35 35 89 1 77 35 6 46 59 89 46 25 55 50 38 61 67 44 23 29 24 46 4 42 15 34 77 20 34 83 79 12 50 69 26 38 14 9 66 80 72 22 26 9 68 35 38 19 84 92 30 83 62 100 71 81 60 7 37 64 50 33 60 86 75 45 78 32 53 3 48 87 60 ...
output:
3796 4747 6203 8669 729 7280 183 7558 492 1140 253 2285 13728 6485 473 7801 2175 10892 1854 2771 4980 293 6213 627 6906 6420 11827 2818 3285 8138 18456 529 5696 18518 9941 6391 5185 15141 692 463 16213 13142 9204 8131 11404 6857 6647 5191 9453 8854 2251 10691 2750 13024 5035 2203 9141 1966 6666 2931...
result:
wrong answer 1st words differ - expected: '3668', found: '3796'
Subtask #2:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 61ms
memory: 25784kb
input:
100000 100000 3 2 3 3 3 3 2 3 2 1 3 1 1 1 3 2 1 3 1 2 2 1 3 1 2 2 1 1 1 3 2 1 3 3 3 3 1 1 1 2 3 3 2 1 1 1 3 1 3 1 3 2 1 3 2 3 3 2 3 3 2 3 3 3 3 3 2 3 2 3 1 3 3 3 3 3 3 3 1 2 3 3 1 3 1 1 2 2 3 1 1 2 3 2 3 1 3 2 1 3 2 3 2 1 1 3 3 1 3 1 2 2 2 3 2 3 2 3 2 1 1 3 1 3 2 2 3 3 3 1 2 2 3 3 2 1 3 1 2 2 2 3 2 ...
output:
128352 150896 62213 7308 10431 88737 106496 33148 44094 126519 75565 8152 74877 48480 50556 55887 12574 122112 81270 12099 5331 98810 53110 85916 30888 12398 13899 67422 21123 62473 13152 8881 6254 43916 76308 66138 19211 79437 79787 60826 58788 613 20952 135949 4545 49084 70881 38494 11190 28347 37...
result:
wrong answer 1st words differ - expected: '113194', found: '128352'
Subtask #3:
score: 0
Wrong Answer
Test #15:
score: 0
Wrong Answer
time: 4ms
memory: 19392kb
input:
5000 5000 256 63 197 36 75 66 33 72 27 75 66 248 29 166 209 252 141 95 84 226 147 249 116 94 192 256 199 273 182 166 116 274 27 211 154 144 283 23 53 110 215 11 164 284 161 221 251 96 43 47 18 115 12 51 156 61 116 209 93 98 47 165 174 106 83 67 184 75 12 290 183 197 112 240 67 56 215 148 104 5 141 2...
output:
1353 2759 1779 2667 4867 5109 3024 1359 901 5182 7991 4966 953 943 822 7437 7833 5450 3897 3306 4575 1609 2594 40 925 309 4455 3972 3893 5862 4719 1692 442 1449 3701 5371 578 121 2089 7781 9318 2096 3188 3761 2950 429 741 4102 182 3302 1473 1637 7283 3072 6088 8262 1579 3887 370 4573 3369 1782 836 2...
result:
wrong answer 1st words differ - expected: '1322', found: '1353'
Subtask #4:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 777ms
memory: 85700kb
input:
1000000 1000000 1105 3246 1880 3554 818 2331 2576 4140 149 4562 3498 3536 3400 4788 4363 4742 1216 4218 4032 1701 1489 4889 1761 3022 3145 4945 3067 4304 5016 4624 1612 13 1335 3613 1086 2210 386 3464 1156 3352 4341 5006 3465 3900 622 654 1826 2983 1250 4164 3335 4308 2995 1982 1347 4335 2535 5054 4...
output:
1270837 310327 764303 79896 161233 580078 993703 1725609 1352531 216386 619391 549268 1393595 322493 1100382 52572 277656 228838 2486 148572 145585 670804 25564 225019 185373 1453210 1675547 550682 147232 974688 1112613 238859 821830 113118 85279 1195295 318431 721953 170462 562948 772053 414516 736...
result:
wrong answer 1st words differ - expected: '1263815', found: '1270837'