QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#787126 | #6404. Shuttle Tour | Symbolize | WA | 182ms | 75784kb | C++20 | 3.8kb | 2024-11-27 09:58:30 | 2024-11-27 09:58:30 |
Judging History
answer
/*
Luogu name: Symbolize
Luogu uid: 672793
*/
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define rep1(i,l,r) for(register int i=l;i<=r;++i)
#define rep2(i,l,r) for(register int i=l;i>=r;--i)
#define rep3(i,x,y,z) for(register int i=x[y];~i;i=z[i])
#define rep4(i,x) for(auto i:x)
#define debug() puts("----------")
const int N=2e5+10;
const int inf=0x3f3f3f3f3f3f3f3f;
using namespace std;
int n,q,h[N],e[N<<1],ne[N<<1],w[N<<1],idx,fa[N][21],dep[N],dist[N],dfn[N],kase,id[N],top[N],cnt=1;
bool a[N];
struct Segment_Tree
{
int id;
struct node
{
int l,r;
int Min,Max;
}tr[N<<2];
int ls(int p){return p<<1;}
int rs(int p){return p<<1|1;}
void push_up(int p)
{
tr[p].Min=min(tr[ls(p)].Min,tr[rs(p)].Min);
tr[p].Max=max(tr[ls(p)].Max,tr[rs(p)].Max);
return;
}
void build(int p,int l,int r)
{
tr[p]={l,r,inf,-inf};
if(tr[p].l==tr[p].r)
{
if(a[l]&&top[l]==id) tr[p].Min=tr[p].Max=dfn[l];
return;
}
int mid=l+r>>1;
build(ls(p),l,mid);
build(rs(p),mid+1,r);
push_up(p);
}
void update(int p,int x)
{
if(tr[p].l==tr[p].r)
{
if(a[x]) tr[p].Min=tr[p].Max=dfn[x];
else tr[p].Min=tr[p].Max=0;
return;
}
int mid=tr[p].l+tr[p].r>>1;
if(x<=mid) update(ls(p),x);
else update(rs(p),x);
push_up(p);
return;
}
pii query(int p,int l,int r)
{
pii ans={inf,-inf};
if(tr[p].l>=l&&tr[p].r<=r) return make_pair(tr[p].Min,tr[p].Max);
int mid=tr[p].l+tr[p].r>>1;
if(l<=mid)
{
pii now=query(ls(p),l,r);
ans.x=min(ans.x,now.x);
ans.y=max(ans.y,now.y);
}
if(r>mid)
{
pii now=query(rs(p),l,r);
ans.x=min(ans.x,now.x);
ans.y=max(ans.y,now.y);
}
return ans;
}
}seg[55];
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return f*x;
}
void add(int x,int y,int z)
{
e[idx]=y;
ne[idx]=h[x];
w[idx]=z;
h[x]=idx++;
return;
}
void dfs(int x,int Fa)
{
fa[x][0]=Fa;
dep[x]=Fa;
top[x]=cnt;
dfn[x]=++kase;
id[kase]=x;
bool res=0;
rep3(i,h,x,ne)
{
int to=e[i];
if(to==Fa) continue;
dist[to]=dist[x]+w[i];
if(!res) ++cnt,res=1;
dfs(to,x);
}
return;
}
void init()
{
rep1(j,1,20) rep1(i,1,n) fa[i][j]=fa[fa[i][j-1]][j-1];
return;
}
void plc(int &x,int k)
{
int kase=0;
while(k)
{
if(k&1) x=fa[x][kase];
k>>=1;
++kase;
}
return;
}
int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
plc(x,dep[x]-dep[y]);
if(x==y) return x;
rep2(i,20,0)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][0];
}
int dis(int x,int y){return dist[x]+dist[y]-2*dist[lca(x,y)];}
bool cmp(int x,int y){return dfn[x]<dfn[y];}
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
memset(h,-1,sizeof h);
n=read();
q=read();
rep1(i,1,n)
{
char ch;
cin>>ch;
a[i]=ch-'0';
}
rep1(i,1,n-1)
{
int u=read();
int v=read();
int w=read();
add(u,v,w);
add(v,u,w);
}
dfs(1,0);
rep1(i,1,cnt) seg[i].id=i,seg[i].build(1,1,n);
init();
while(q--)
{
int opt=read();
if(opt==1)
{
int x=read();
a[x]^=1;
seg[top[x]].update(1,x);
}
else
{
int l=read();
int r=read();
vector<int> v;
rep1(i,1,cnt)
{
pii now=seg[i].query(1,l,r);
if(now.x>=1&&now.x<=n) v.push_back(id[now.x]);
if(now.y>=1&&now.y<=n&&now.x!=now.y) v.push_back(id[now.y]);
}
if(!v.size())
{
puts("-1");
continue;
}
sort(v.begin(),v.end(),cmp);
int len=v.size()-1,ans=dis(v[0],v[len]);
rep1(i,1,len) ans+=dis(v[i-1],v[i]);
cout<<ans<<"\n";
}
}
return 0;
}
/*
5 1
10010
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 25612kb
input:
5 6 10110 1 2 1 1 3 10 2 4 100 3 5 1000 2 1 5 1 3 2 1 5 2 2 4 2 5 5 2 1 1
output:
222 202 0 -1 0
result:
ok 5 number(s): "222 202 0 -1 0"
Test #2:
score: -100
Wrong Answer
time: 182ms
memory: 75784kb
input:
50 200000 00100100100001101010110100000111100011101110011010 14 47 940241413 11 43 331483591 37 38 496247070 47 46 832459694 7 15 16479291 1 30 402388666 30 8 513064064 46 50 739311480 5 4 761894947 32 41 631669659 17 24 715786564 35 20 763151642 32 33 492466005 39 11 186023146 9 7 805081251 3 42 25...
output:
49982357740 -1 73230104872 93860714682 60903146036 79754620318 41777780020 65052810888 83342898482 67084418260 108380286738 24223007290 17314657386 71831537782 38986964652 2856655228 38846768410 0 -1 18374096912 75957125170 55431019116 64371138754 55027273040 25263748874 21629990106 56094908174 0 88...
result:
wrong answer 1st numbers differ - expected: '17149847378', found: '49982357740'