QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#401128 | #8547. Whose Land? | ocharin | WA | 432ms | 3756kb | C++23 | 3.6kb | 2024-04-28 00:22:15 | 2024-04-28 00:22:15 |
Judging History
answer
/*
_/ _/
_/_/ _/_/_/ _/_/_/ _/_/_/ _/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/_/ _/_/_/ _/ _/ _/_/_/ _/ _/ _/ _/
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
struct FenwickTree{
int sz;
vector<int>c;
FenwickTree(int n=0){sz=n,c.assign(sz+1,0ll);}
void init(int n=0){sz=n,c.assign(sz+1,0ll);}
int lowbit(int x){return -x&x;}
void add(int u,int k){
for(;u;u-=lowbit(u)) c[u]+=k;
}
int query(int u){
int ans=0;
for(;u<=sz;u+=lowbit(u)) ans+=c[u];
return ans;
}
}ft;
struct SegmentTree{
vector<int>s;
vector<int>tag;
SegmentTree(int _n){s.assign(_n+1<<2,0ll),tag.assign(_n+1<<2,0ll);};
void pushtag(int u,int cl,int cr,int k){
if(k) s[u]=tag[u]=k;
}
void pushdown(int u,int cl,int cr){
int mid=cl+cr>>1;
pushtag(u<<1,cl,mid,tag[u]);
pushtag(u<<1|1,mid+1,cr,tag[u]);
tag[u]=0;
}
void modify(int u,int cl,int cr,int l,int r,int k){
if(r<cl || cr<l) return;
if(l<=cl && cr<=r){
int x=cr-cl+1;
ft.add(s[u],-x);
pushtag(u,cl,cr,k);
ft.add(s[u],x);
return;
}
pushdown(u,cl,cr);
int mid=cl+cr>>1;
modify(u<<1,cl,mid,l,r,k);
modify(u<<1|1,mid+1,cr,l,r,k);
}
};
void solve(){
int n,k,m;
cin>>n>>k>>m;
vector<vector<int>>e(n+1);
for(int i=1;i<n;++i){
int u,v;
cin>>u>>v;
e[u].push_back(v);
e[v].push_back(u);
}
vector<int>bfn(n+1),fa(n+1),p(n+1);
vector l(n+1,vector(k+1,n+1)),r(n+1,vector(k+1,0ll));
queue<int>q;
q.push(1);
int tot=0;
while(!q.empty()){
auto u=q.front();q.pop();
bfn[++tot]=u;
p[u]=tot;
for(auto v:e[u]){
if(v==fa[u]) continue;
fa[v]=u;
q.push(v);
}
}
for(int i=1;i<=n;++i)cerr<<p[i]<<" \n"[i==n];
for(int i=n;i;--i){
int u=i;
int pos=p[u];
for(int j=0;j<=k;++j){
l[u][j]=min(l[u][j],pos);
r[u][j]=max(r[u][j],pos);
u=fa[u];
if(!u) break;
}
}
// for(int i=1;i<=n;++i){
// for(int j=0;j<=k;++j) cerr<<i<<" "<<j<<" : "<<l[i][j]<<" "<<r[i][j]<<endl;
// }
vector<vector<pair<int,int>>>Q(n+1);
for(int i=0;i<m;++i){
int l,r;
cin>>l>>r;
Q[r].push_back({l,i});
}
vector<int>res(m);
ft.init(n);
SegmentTree seg(n);
for(int i=1;i<=n;++i){
vector<int>cl(2*k+1,n+1),cr(2*k+1,0ll);
int u=i;
for(int x=0;x<=k;++x){
for(int y=0;x+y<=k;++y){
cl[k+x-y]=min(cl[k+x-y],l[u][y]);
cr[k+x-y]=max(cr[k+x-y],r[u][y]);
}
u=fa[u];
if(!u) break;
}
for(int j=0;j<=2*k;++j){
if(cl[j]>cr[j]) continue;
// cerr<<i<<" "<<j<<" "<<cl[j]<<" "<<cr[j]<<endl;
seg.modify(1,1,n,cl[j],cr[j],i);
}
for(auto [l,id]:Q[i]) res[id]=ft.query(l);
}
for(auto i:res) cout<<i<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3664kb
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: 432ms
memory: 3756kb
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:
263 408 370 124 332 343 475 8 348 459 417 352 186 249 371 584 147 44 355 271 94 337 38 306 267 71 139 502 173 24 163 494 293 340 261 340 539 499 77 368 56 422 387 225 422 272 193 266 136 70 208 462 469 365 312 501 340 174 303 436 216 542 201 187 564 61 529 128 303 207 292 397 115 431 554 384 123 196...
result:
wrong answer 1st numbers differ - expected: '255', found: '263'