QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401507#8547. Whose Land?marherWA 520ms9736kbC++142.8kb2024-04-28 20:46:182024-04-28 20:46:19

Judging History

你现在查看的是最新测评结果

  • [2024-04-28 20:46:19]
  • 评测
  • 测评结果:WA
  • 用时:520ms
  • 内存:9736kb
  • [2024-04-28 20:46:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+80,inf=1e9+7;

struct ask
{
    int l,r,id;

    friend bool operator<(ask a,ask b)
    {
        return a.r<b.r;
    }
}Q[N<<2];

struct node
{
    int v,nxt;
}e[N];

struct inter
{
    int l,r;
}a[N][21];

int T,n,k,q,dep[N],fa[N],head[N],cnt,dfn[N],t[N],ans[N];
set<pair<int,int>>s;

void clear()
{
    cnt=0;s.clear();
    for(int i=1;i<=n;i++)
    {
        t[i]=head[i]=fa[i]=dfn[i]=ans[i]=dep[i]=0;
        for(int j=0;j<=k;j++)a[i][j]=(inter){0,0};
    }
}

void dfs(int x,int f)
{
    fa[x]=f;dep[x]=dep[f]+1;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v!=f)dfs(v,x);
    }
}

void bfs()
{
    int cc=0;queue<int>q;q.push(1);
    while(!q.empty())
    {
        int x=q.front();q.pop();dfn[x]=++cc;
        for(int i=head[x];i;i=e[i].nxt)
        {
            int v=e[i].v;
            if(v!=fa[x])q.push(v);
        }
    }
}

void init(int x)
{
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v!=fa[x])init(v);
    }
    a[x][0]=(inter){dfn[x],dfn[x]};
    for(int i=1;i<=k;i++)
    {
        auto&[l,r]=a[x][i];l=inf,r=0;
        for(int j=head[x];j;j=e[j].nxt)if(e[j].v!=fa[x])l=min(l,a[e[j].v][i-1].l),r=max(r,a[e[j].v][i-1].r);
    }
}

void insert(int x,int d)
{
    x=n-x+1;
    for(int i=x;i<=n;i+=(i&(-i)))t[i]+=d;
}

int find(int x)
{
    x=n-x+1;int ans=0;
    for(int i=x;i;i-=(i&(-i)))ans+=t[i];
    return ans;
}

void mk(int l,int r,int w)
{
    // cout<<l<<' '<<r<<' '<<w<<'\n';
    if(l>r)return;
    auto bg=--s.upper_bound({l,inf});
    auto ed=bg;
    while((*ed).first<=r)
    {
        int ll=max(l,(*ed).first),w=(*ed).second;++ed;
        int rr=min(r+1,(*ed).first);
        insert(w,ll-rr);
    }
    if((*bg).first!=l)++bg;
    s.erase(bg,ed);s.insert(make_pair(l,w));
    insert(w,r-l+1);
}

void sol()
{
    cin>>n>>k>>q;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        e[++cnt]=(node){v,head[u]};head[u]=cnt;
        e[++cnt]=(node){u,head[v]};head[v]=cnt;
    }
    dfs(1,1);bfs();init(1);
    for(int i=1;i<=q;i++)cin>>Q[i].l>>Q[i].r,Q[i].id=i;
    sort(Q+1,Q+q+1);int pos=1;
    for(int i=1;i<=n+1;i++)s.insert(make_pair(i,0));
    for(int x=1;x<=n;x++)
    {
        int ro[21];
        ro[0]=x;
        for(int i=1;i<=k;i++)ro[i]=fa[ro[i-1]];
        for(int d=dep[x]-k;d<=dep[x]+k;d++)if(d>0)
        {
        // cout<<x<<' '<<d<<'\n';
            int p=ro[dep[x]-(dep[x]+d-k+1)/2],t=d-dep[p];
            mk(a[p][t].l,a[p][t].r,x);
        }
        while(Q[pos].r==x&&pos<=q)ans[Q[pos].id]=find(Q[pos].l),pos++;
    }
    for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
    // exit(0);
}

main()
{
    // freopen("in.txt","r",stdin);
    cin>>T;
    while(T--)sol(),clear();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9736kb

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: 520ms
memory: 9732kb

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:

254
376
353
123
312
326
427
8
332
416
388
334
179
238
347
496
145
44
335
256
92
316
38
282
258
71
137
450
169
24
159
445
278
325
249
308
478
443
77
347
56
383
363
215
386
263
184
256
132
67
201
416
422
344
295
445
324
169
286
399
207
480
195
181
475
61
466
126
286
203
278
367
113
401
484
366
121
190...

result:

wrong answer 1st numbers differ - expected: '255', found: '254'