Submission #529555

# Submission time Handle Problem Language Result Execution time Memory
529555 2022-02-23T07:20:07 Z i_am_noob Traffickers (RMI18_traffickers) C++17
100 / 100
570 ms 36744 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")

#define ll long long
#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define chkmin(a,b) (a=min(a,b))
#define chkmax(a,b) (a=max(a,b))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
//#define inf 1010000000
#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
template <int mod>
struct Modint{
    int val;
    Modint(int _val=0){val=_val%mod;if(val<0) val+=mod;}
    Modint operator +(const Modint& o) const {
        Modint res;
        res.val=val+o.val;
        if(res.val>=mod) res.val-=mod;
        return res;
    }
    Modint operator +(const int& o) const {return Modint(val+o);}
    Modint operator -() const {
        Modint res;
        res.val=-val;
        if(res.val<0) res.val+=mod;
        return res;
    }
    Modint operator -(const Modint& o) const {
        Modint res;
        res.val=val-o.val;
        if(res.val<0) res.val+=mod;
        return res;
    }
    Modint operator -(const int& o) const {return Modint(val-o);}
    Modint operator *(const Modint& o) const {return Modint(val*o.val);}
    Modint operator *(const int& o) const {return Modint(val*(o%mod));}
    Modint operator +=(const Modint& o){*this=(*this)+o;return *this;}
    Modint operator -=(const Modint& o){*this=(*this)-o;return *this;}
    Modint operator *=(const Modint& o){*this=(*this)*o;return *this;}
    Modint Pow(int b) const {
        Modint tmp(val),ret(1);
        while(b){
            if(b&1) ret*=tmp;
            b>>=1;tmp*=tmp;
        }
        return ret;
    }
    Modint Pow(const Modint& a, int b) const {return a.Pow(b);}
    inline Modint inv() const {return (*this).Pow(mod-2);}
    Modint operator /(const Modint& o) const {return *this*o.inv();}
    Modint operator /(const int& o) const {return *this*Modint(o).inv();}
    bool operator ==(const Modint& o) const {return val==o.val;}
};
template<int mod>
ostream& operator << (ostream& o, Modint<mod> x){return o << x.val;}
template<int mod>
Modint<mod> operator +(const int& x, const Modint<mod>& y){return Modint<mod>(x+y.val);}
template<int mod>
Modint<mod> operator -(const int& x, const Modint<mod>& y){return Modint<mod>(x-y.val);}
template<int mod>
Modint<mod> operator *(const int& x, const Modint<mod>& y){return Modint<mod>(x%mod*y.val);}
#define modint Modint<MOD>
vector<modint> inv,fac,invfac;
void init_comb(int N){
    inv.resize(N),fac.resize(N),invfac.resize(N);
    inv[1]=1,fac[0]=1,invfac[0]=1;
    rep2(i,2,N) inv[i]=inv[MOD%i]*(MOD-MOD/i);
    rep2(i,1,N) fac[i]=fac[i-1]*i;
    rep2(i,1,N) invfac[i]=invfac[i-1]*inv[i];
}
inline modint C(int n, int m){return m>n||m<0?modint(0):fac[n]*invfac[m]*invfac[n-m];}
inline modint H(int n, int m){return C(n+m-1,n);}

const int maxn=30005,maxm=50005,maxk=7777714;

//i_am_noob
//#define wiwihorz  
typedef signed v8si __attribute__ (( vector_size(32) ));
constexpr signed num_of_blocks(signed x){return (x+7)>>3;}
const int K=20;
template<signed N>
struct array_of_int{
    v8si arr[num_of_blocks(N)];
    array_of_int(){rep(num_of_blocks(N)) rep1(j,8) arr[i][j]=0;}
    signed get(int x){return arr[x>>3][x&7];}
    void set(int x, signed k){arr[x>>3][x&7]=k;}
};
template<signed N>
array_of_int<N> operator +(const array_of_int<N>& arr1, const array_of_int<N>& arr2){
    array_of_int<N> res;
    rep(num_of_blocks(N)) res.arr[i]=arr1.arr[i]+arr2.arr[i];
    return res;
}
template<signed N>
array_of_int<N> operator -(const array_of_int<N>& arr1, const array_of_int<N>& arr2){
    array_of_int<N> res;
    rep(num_of_blocks(N)) res.arr[i]=arr1.arr[i]-arr2.arr[i];
    return res;
}
template<signed N>
array_of_int<N> operator +=(array_of_int<N>& arr1, const array_of_int<N>& arr2){
    rep(num_of_blocks(N)) arr1.arr[i]+=arr2.arr[i];
    return arr1;
}
template<signed N>
array_of_int<N> operator -=(array_of_int<N>& arr1, const array_of_int<N>& arr2){
    rep(num_of_blocks(N)) arr1.arr[i]-=arr2.arr[i];
    return arr1;
}
template<signed N>
array_of_int<N> operator *(const array_of_int<N>& arr1, const signed& x){
    array_of_int<N> res;
    rep(num_of_blocks(N)) res.arr[i]=arr1.arr[i]*x;
    return res;
}

struct BIT{
    vector<array_of_int<K>> val;
    BIT(){val.resize(maxn);}
    void modify(int l, int r, array_of_int<K> x){
        for(int i=l+1; i<maxn; i+=i&-i) val[i]+=x;
        for(int i=r+2; i<maxn; i+=i&-i) val[i]-=x;
    }
    array_of_int<K> query(int p){
        array_of_int<K> res;
        for(int i=p+1; i; i-=i&-i) res+=val[i];
        return res;
    }
};
BIT bit[10];

int n,k,q,l[maxn],r[maxn],par[maxn][18],dep[maxn];
vector<vector<int>> adj(maxn);

void dfs(int u, int fa){
    static int t=0;
    par[u][0]=fa;
    rep2(i,1,18) par[u][i]=par[u][i-1]==-1?-1:par[par[u][i-1]][i-1];
    dep[u]=fa==-1?0:dep[fa]+1;
    l[u]=t++;
    for(auto v: adj[u]) if(v!=fa) dfs(v,u);
    r[u]=t-1;
}

vector<int> path(int u, int v){
    vector<int> res1,res2;
    while(dep[u]>dep[v]) res1.pb(u),u=par[u][0];
    while(dep[v]>dep[u]) res2.pb(v),v=par[v][0];
    while(u!=v) res1.pb(u),u=par[u][0],res2.pb(v),v=par[v][0];
    res1.pb(u);
    reverse(all(res2));
    for(auto i: res2) res1.pb(i);
    return res1;
}

int lca(int u, int v){
    if(dep[u]>dep[v]) swap(u,v);
    rep(18) if((dep[v]-dep[u])>>i&1) v=par[v][i];
    if(u==v) return u;
    rep3(i,17,0) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
    return par[u][0];
}

void upd(int u, int v, int x){
    vector<int> vec=path(u,v);
    int de=sz(vec);
    while(de<=10) de<<=1;
    array_of_int<K> arr;
    rep(de){
        arr.set(i,x);
        bit[de-11].modify(l[vec[i%sz(vec)]],r[vec[i%sz(vec)]],arr);
        arr.set(i,0);
    }
}

void orzck(){
    cin >> n;
    rep(n-1){
        int u,v;
        cin >> u >> v;
        u--,v--;
        adj[u].pb(v),adj[v].pb(u);
    }
    dfs(0,-1);
    cin >> k;
    rep(k){
        int u,v;
        cin >> u >> v;
        u--,v--;
        upd(u,v,1);
    }
    cin >> q;
    while(q--){
        int op;
        cin >> op;
        if(op<=2){
            int u,v;
            cin >> u >> v;
            u--,v--;
            if(op==1) upd(u,v,1);
            else upd(u,v,-1);
        }
        else{
            int u,v,x,y;
            cin >> u >> v >> x >> y;
            u--,v--;
            int p=lca(u,v);
            int res=0;
            rep(10){
                array_of_int<K> arr=bit[i].query(l[u])+bit[i].query(l[v])-bit[i].query(l[p]);
                if(p>0) arr-=bit[i].query(l[par[p][0]]);
                int de=i+11;
                rep1(j,de) res+=((int)arr.get(j))*((y+de-j)/(de)-(x?(x-1+de-j)/(de):0));
            }
            print(res);
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    // #ifdef i_am_noob
    // freopen("input1.txt","r",stdin);
    // freopen("output1.txt","w",stdout);
    // freopen("output2.txt","w",stderr);
    // #endif
    cout << fixed << setprecision(15);
    int t;
    #ifdef wiwihorz
    cin >> t;
    #else
    t=1;
    #endif
    ld start=clock();
    while(t--) orzck();
    bug((clock()-start)/CLOCKS_PER_SEC);
    return 0;
}

Compilation message

traffickers.cpp: In function 'int main()':
traffickers.cpp:34:18: warning: statement has no effect [-Wunused-value]
   34 | #define bug(...) 777771449
      |                  ^~~~~~~~~
traffickers.cpp:266:5: note: in expansion of macro 'bug'
  266 |     bug((clock()-start)/CLOCKS_PER_SEC);
      |     ^~~
traffickers.cpp:264:8: warning: unused variable 'start' [-Wunused-variable]
  264 |     ld start=clock();
      |        ^~~~~
traffickers.cpp: In member function 'void BIT::modify(long long int, long long int, array_of_int<20>)':
traffickers.cpp:150:10: note: the ABI for passing parameters with 32-byte alignment has changed in GCC 4.6
  150 |     void modify(int l, int r, array_of_int<K> x){
      |          ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 29260 KB Output is correct
2 Correct 15 ms 29388 KB Output is correct
3 Correct 15 ms 29376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 31432 KB Output is correct
2 Correct 60 ms 31176 KB Output is correct
3 Correct 56 ms 31484 KB Output is correct
4 Correct 70 ms 31572 KB Output is correct
5 Correct 51 ms 31428 KB Output is correct
6 Correct 52 ms 31396 KB Output is correct
7 Correct 54 ms 31436 KB Output is correct
8 Correct 51 ms 31496 KB Output is correct
9 Correct 52 ms 31492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 550 ms 36284 KB Output is correct
2 Correct 533 ms 36720 KB Output is correct
3 Correct 505 ms 36188 KB Output is correct
4 Correct 539 ms 35880 KB Output is correct
5 Correct 570 ms 35688 KB Output is correct
6 Correct 487 ms 36540 KB Output is correct
7 Correct 458 ms 36744 KB Output is correct
8 Correct 525 ms 36412 KB Output is correct