QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#574101 | #8547. Whose Land? | Fesdrer | WA | 500ms | 13820kb | C++14 | 2.7kb | 2024-09-18 20:47:43 | 2024-09-18 20:47:45 |
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;
l=bfn[l],r=bfn[r];
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 13820kb
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: 500ms
memory: 10164kb
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:
243 370 340 114 304 312 426 5 318 405 382 321 173 230 335 499 140 40 326 248 83 311 36 274 247 68 134 442 165 24 151 440 264 311 243 303 466 443 71 336 53 378 350 207 376 250 175 244 127 65 195 417 423 332 280 448 310 160 270 388 199 471 188 171 486 54 470 118 278 191 267 357 106 393 482 349 114 180...
result:
wrong answer 1st numbers differ - expected: '255', found: '243'