QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#134477 | #6404. Shuttle Tour | nikolapesic2802 | WA | 377ms | 160384kb | C++14 | 6.8kb | 2023-08-03 20:46:33 | 2023-08-03 20:46:35 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ld long double
#define li __int128
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<int D, typename T>struct vec : public vector<vec<D - 1, T>> {template<typename... Args>vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)) {}};
template<typename T>struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val) {}};
template<class T1,class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
int ri(){int x;scanf("%i",&x);return x;}
void rd(int&x){scanf("%i",&x);}
void rd(long long&x){scanf("%lld",&x);}
void rd(double&x){scanf("%lf",&x);}
void rd(long double&x){scanf("%Lf",&x);}
void rd(string&x){cin>>x;}
void rd(char*x){scanf("%s",x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&p:x)rd(p);}
template<typename C,typename...T>void rd(C&a,T&...args){rd(a);rd(args...);}
//istream& operator>>(istream& is,__int128& a){string s;is>>s;a=0;int i=0;bool neg=false;if(s[0]=='-')neg=true,i++;for(;i<s.size();i++)a=a*10+s[i]-'0';if(neg)a*=-1;return is;}
//ostream& operator<<(ostream& os,__int128 a){bool neg=false;if(a<0)neg=true,a*=-1;ll high=(a/(__int128)1e18);ll low=(a-(__int128)1e18*high);string res;if(neg)res+='-';if(high>0){res+=to_string(high);string temp=to_string(low);res+=string(18-temp.size(),'0');res+=temp;}else res+=to_string(low);os<<res;return os;}
const int N=2e5+5,L=18;
int up[N][L];
int dep[N];
ll depw[N];
vector<bool> vis(N);
vector<vector<pair<int,int>>> graf(N);
vector<int> order(N);
int cntOrd=0;
vector<int> leaves;
vector<int> indl(N);
vector<int> paths[50];
int dfs(int tr){
order[tr]=cntOrd++;
vis[tr]=1;
int indx=-1;
for(auto p:graf[tr]){
if(!vis[p.f]){
dep[p.f]=dep[tr]+1;
depw[p.f]=depw[tr]+p.s;
up[p.f][0]=tr;
int a=dfs(p.f);
if(indx==-1){
indx=a;
}
}
}
if(indx==-1){
leaves.pb(tr);
indx=sz(leaves)-1;
}
indl[tr]=indx;
paths[indx].pb(tr);
return indx;
}
int lcapath[50][50];
int lca(int a,int b){
if(dep[a]<dep[b]){
swap(a,b);
}
for(int j=L-1;j>=0;j--){
if(dep[a]-dep[b]>=(1<<j)){
a=up[a][j];
}
}
if(a==b)
return a;
for(int j=L-1;j>=0;j--){
if(up[a][j]!=up[b][j]){
a=up[a][j];
b=up[b][j];
}
}
return up[a][0];
}
int lca2(int a,int b){
int A=indl[a],B=indl[b];
if(A!=B){
return lcapath[A][B];
}
return dep[a]<dep[b]?a:b;
}
int upc(int a,int k){
for(int j=L-1;j>=0;j--){
if(k>=(1<<j)){
a=up[a][j];
k-=1<<j;
}
}
return a;
}
string s;
int mn[50][4*N],mx[50][4*N];
void init(int ind,int l=0,int r=N-1,int i=1){
if(l==r){
if(s[l]=='1'&&indl[l]==ind){
//printf("%i: %i\n",ind,l);
mn[ind][i]=mx[ind][i]=dep[l];
}
else{
mn[ind][i]=N;
mx[ind][i]=0;
}
return;
}
int m=(l+r)>>1;
init(ind,l,m,2*i);
init(ind,m+1,r,2*i+1);
mn[ind][i]=min(mn[ind][2*i],mn[ind][2*i+1]);
mx[ind][i]=max(mx[ind][2*i],mx[ind][2*i+1]);
}
void flip(int ind,int p,int l=0,int r=N-1,int i=1){
if(l==r){
//printf("flip %i %i\n",ind,p);
if(s[l]=='1'){
s[l]='0';
mn[ind][i]=N;
mx[ind][i]=0;
}
else{
s[l]='1';
mn[ind][i]=mx[ind][i]=dep[l];
}
return;
}
int m=(l+r)>>1;
if(p<=m){
flip(ind,p,l,m,2*i);
}
else{
flip(ind,p,m+1,r,2*i+1);
}
mn[ind][i]=min(mn[ind][2*i],mn[ind][2*i+1]);
mx[ind][i]=max(mx[ind][2*i],mx[ind][2*i+1]);
}
pair<int,int> get(int ind,int qs,int qe,int l=0,int r=N-1,int i=1){
if(qs>r||qe<l){
return {N,0};
}
if(qs<=l&&qe>=r){
return {mn[ind][i],mx[ind][i]};
}
int m=(l+r)>>1;
auto a=get(ind,qs,qe,l,m,2*i);
auto b=get(ind,qs,qe,m+1,r,2*i+1);
return {min(a.f,b.f),max(a.s,b.s)};
}
int main()
{
int n=ri(),q=ri();
cin >> s;
while(sz(s)<N){
s+='0';
}
for(int i=1;i<n;i++){
int u=ri()-1,v=ri()-1,w=ri();
graf[u].pb({v,w});
graf[v].pb({u,w});
}
dep[0]=0;
depw[0]=0;
up[0][0]=0;
dfs(0);
for(int j=1;j<L;j++){
for(int i=0;i<n;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
int m=sz(leaves);
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
lcapath[i][j]=lca(leaves[i],leaves[j]);
}
}
for(int j=0;j<m;j++){
init(j);
}
while(q--){
int t=ri();
if(t==1){
int x=ri()-1;
flip(indl[x],x);
}
else{
int l=ri()-1,r=ri()-1;
vector<int> dots;
for(int j=0;j<m;j++){
auto a=get(j,l,r);
if(a.f<=a.s){
int depl=dep[leaves[j]];
dots.pb(paths[j][depl-a.f]);
if(a.f!=a.s)
dots.pb(paths[j][depl-a.s]);
}
}
if(sz(dots)==0){
printf("-1\n");
}
else{
vector<pair<int,int>> ord;
for(auto p:dots){
ord.pb({order[p],p});
}
//cout << ord << endl;
sort(all(ord));
ll dist=0;
for(int i=0;i<sz(ord);i++){
int j=(i+1)%sz(ord);
int l=lca2(ord[i].s,ord[j].s);
//printf("%i %i %i\n",ord[i].s,ord[j].s,l);
dist+=depw[ord[i].s]+depw[ord[j].s]-2*depw[l];
}
printf("%lld\n",dist);
}
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 29420kb
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: 377ms
memory: 160384kb
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:
17149847378 -1 26540740138 27733209928 19427465732 27003443184 11893407374 16452142896 18531713524 18401090236 29727468956 9593307160 7500227408 23682775470 16688997988 2856655228 13102105330 0 -1 9140136614 24313120040 22558599316 24807163344 19126284190 8533875940 7695830712 18079494744 0 27004673...
result:
wrong answer 4th numbers differ - expected: '29613692754', found: '27733209928'