Submission #863936

#TimeUsernameProblemLanguageResultExecution timeMemory
863936MilosMilutinovicTraffickers (RMI18_traffickers)C++14
100 / 100
2129 ms106716 KiB
#include<bits/stdc++.h> #define pb push_back #define fi first #define se second #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef long double ld; template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;} template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;} int readint(){ 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*10+ch-'0';ch=getchar();} return x*f; } int n,k,q,v[200005],nxt[200005],tot,h[100005],F[100005][20],dep[100005],dfn[100005],dfo[100005],T; void addedge(int x,int y){ v[++tot]=y; nxt[tot]=h[x]; h[x]=tot; v[++tot]=x; nxt[tot]=h[y]; h[y]=tot; } void dfs(int x,int fa){ dfn[x]=++T; F[x][0]=fa; for(int i=1;i<20;i++) F[x][i]=F[F[x][i-1]][i-1]; for(int p=h[x];p;p=nxt[p]) if(v[p]!=fa) dep[v[p]]=dep[x]+1,dfs(v[p],x); dfo[x]=++T; } int lca(int x,int y){ if(dep[x]>dep[y]) swap(x,y); for(int i=19;i>=0;i--) if(dep[F[y][i]]>=dep[x]) y=F[y][i]; if(x==y) return x; for(int i=19;i>=0;i--) if(F[x][i]!=F[y][i]) x=F[x][i],y=F[y][i]; return F[x][0]; } int dist(int x,int y){ return dep[x]+dep[y]-2*dep[lca(x,y)]; } vector<int> get_path(int x,int y){ int l=lca(x,y); vector<int> a,b; while(x!=l) a.pb(x),x=F[x][0]; while(y!=l) b.pb(y),y=F[y][0]; vector<int> ret; for(int i:a) ret.pb(i); reverse(b.begin(),b.end()); ret.pb(l); for(int i:b) ret.pb(i); return ret; } struct fenwick{ vector<int> f; void init(int _n){f.resize(_n+1);} void change(int p,int v){ assert(p<f.size()); for(;p<f.size();p+=p&-p) f[p]+=v; } int query(int p){ int ret=0; for(;p;p-=p&-p) ret+=f[p]; return ret; } }fenw[20][21]; void upd(int x,int y,int nd,int v){ fenw[x][y].change(dfn[nd],v); fenw[x][y].change(dfo[nd],-v); } int get_cnt(int x,int y,int a,int b,int l){ return fenw[x][y].query(dfn[a])+fenw[x][y].query(dfn[b])-fenw[x][y].query(dfn[l])-(l!=1?fenw[x][y].query(dfn[F[l][0]]):0); } int main(){ n=readint(); for(int i=1;i<n;i++) addedge(readint(),readint()); dfs(1,1); for(int i=0;i<20;i++) for(int j=0;j<21;j++) fenw[i][j].init(T); k=readint(); for(int i=1;i<=k;i++){ int x=readint(),y=readint(); int d=dist(x,y); vector<int> ver=get_path(x,y); d=ver.size(); for(int i=0;i<d;i++) upd(i,d,ver[i],1); } q=readint(); while(q--){ int op=readint(); if(op==1){ int x=readint(),y=readint(); int d=dist(x,y); vector<int> ver=get_path(x,y); d=ver.size(); for(int i=0;i<d;i++) upd(i,d,ver[i],1); }else if(op==2){ int x=readint(),y=readint(); int d=dist(x,y); vector<int> ver=get_path(x,y); d=ver.size(); for(int i=0;i<d;i++) upd(i,d,ver[i],-1); }else{ int x=readint(),y=readint(),t1=readint(),t2=readint(); int l=lca(x,y); ll ans=0; for(int i=1;i<=20;i++){ for(int j=0;j<i;j++){ int tl=t1; while(tl%i!=j) tl++; int tr=t2; while(tr>=tl&&tr%i!=j) tr--; if(tl>tr) continue; int k=(tr-tl)/i+1; ans+=k*1ll*get_cnt(j,i,x,y,l); } } printf("%lld\n",ans); } } return 0; }

Compilation message (stderr)

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from traffickers.cpp:1:
traffickers.cpp: In member function 'void fenwick::change(int, int)':
traffickers.cpp:70:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   assert(p<f.size());
      |          ~^~~~~~~~~
traffickers.cpp:71:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |   for(;p<f.size();p+=p&-p) f[p]+=v;
      |        ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...