QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#574070 | #8547. Whose Land? | Fesdrer | WA | 503ms | 14072kb | C++14 | 2.7kb | 2024-09-18 20:42:30 | 2024-09-18 20:42:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,K=25,Q=5e5+5;
int T,n,k,q,c[N],fa[N],bfn[N],tob,downL[N][K],downR[N][K];
vector<int> e[N];
struct Query{
int l,r,id,ans;
}query[Q];
struct Node{
int l,r,v;
bool operator <(const Node &aa)const{
return l<aa.l;
}
};
set<Node> s;
void add(int x,int y){
if(x==n+1) return;
for(;x<=n;x+=x&-x) c[x]+=y;
}
int ask(int x){
int ret=0;
for(;x;x-=x&-x) ret+=c[x];
return ret;
}
auto split(int x){
if(x>n) return s.end();
auto it=--s.upper_bound((Node){x,0,0});
if(it->l==x) return it;
int l=it->l,r=it->r,v=it->v;
s.erase(it),s.insert((Node){l,x-1,v});
return s.insert((Node){x,r,v}).first;
}
void cover(int l,int r,int v){
if(l>r||l==0||r==0) return;
auto itr=split(r+1),itl=split(l);
for(auto it=itl;it!=itr;it++) add(n+1-(it->v),(it->l)-(it->r)-1);
s.erase(itl,itr),s.insert((Node){l,r,v}),add(n+1-v,r-l+1);
}
void bfs(){
queue<int> qq;
while(qq.size()) qq.pop();
qq.push(1);
while(qq.size()){
int x=qq.front();qq.pop();
bfn[x]=++tob;
for(int y:e[x]) if(y!=fa[x]) fa[y]=x,qq.push(y);
}
}
void dfs(int x){
for(int y:e[x]) if(y!=fa[x]) dfs(y);
downL[x][0]=downR[x][0]=x;
for(int i=1;i<=k;i++){
for(int j=0;!downL[x][i]&&j<int(e[x].size());j++)
if(e[x][j]!=fa[x]) downL[x][i]=downL[e[x][j]][i-1];
for(int j=int(e[x].size())-1;!downR[x][i]&&j>=0;j--)
if(e[x][j]!=fa[x]) downR[x][i]=downR[e[x][j]][i-1];
}
}
void covers(int x){
for(int i=x,p=k;p>=0;i=fa[i],p--){
// cout<<x<<" "<<i<<" "<<p<<endl;
if(i==1){
for(int j=0;j<=p;j++){
// cout<<j<<" "<<downL[i][j]<<" "<<downR[i][j]<<" "<<x<<endl;
cover(downL[i][j],downR[i][j],x);
}
break;
}
else{
// cout<<1<<endl;
cover(downL[i][p],downR[i][p],x);
// cout<<1<<endl;
if(p>0) cover(downL[i][p-1],downR[i][p-1],x);
}
}
}
void init(){
for(int i=0;i<=n;i++) c[i]=fa[i]=bfn[i]=0,e[i].clear();
tob=0;
for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) downL[i][j]=downR[i][j]=0;
s.clear();
s.insert((Node){1,n,0});
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
cin>>n>>k>>q;
init();
for(int i=1,x,y;i<n;i++){
cin>>x>>y;
e[x].push_back(y),e[y].push_back(x);
}
for(int i=1;i<=q;i++) cin>>query[i].l>>query[i].r,query[i].id=i;
sort(query+1,query+q+1,[&](const Query &x,const Query &y){return x.r<y.r;});
bfs(),dfs(1);
for(int i=1,j=0;i<=n;i++){
covers(i);
while(j<q&&query[j+1].r==i){
j++;
query[j].ans=ask(n+1-query[j].l);
}
}
sort(query+1,query+q+1,[&](const Query &x,const Query &y){return x.id<y.id;});
for(int i=1;i<=q;i++) cout<<query[i].ans<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14072kb
input:
2 5 1 2 1 2 1 3 2 4 2 5 2 2 2 3 8 2 3 1 2 1 3 2 4 2 5 4 6 5 7 7 8 2 2 2 5 3 4
output:
4 5 7 8 6
result:
ok 5 number(s): "4 5 7 8 6"
Test #2:
score: -100
Wrong Answer
time: 503ms
memory: 9864kb
input:
1000 500 1 500 291 2 406 9 207 13 71 15 351 17 442 18 496 19 104 20 208 23 448 34 80 42 187 44 352 45 290 46 116 47 318 50 226 52 129 55 83 59 100 60 54 61 73 65 63 66 454 67 43 71 26 74 68 26 320 75 388 76 425 79 170 81 350 83 48 85 153 86 221 90 290 95 130 96 82 98 124 82 36 99 213 100 121 101 132...
output:
481 500 490 382 495 499 500 5 482 500 499 499 472 499 500 500 489 187 499 496 459 498 274 499 481 314 470 500 432 400 463 500 499 492 494 499 500 500 363 499 415 500 499 495 500 489 498 477 484 331 485 500 500 496 499 500 491 470 499 500 494 500 435 488 500 423 500 490 495 490 499 500 431 497 500 49...
result:
wrong answer 1st numbers differ - expected: '255', found: '481'