QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#291335 | #6737. Neighbourhood | Radewoosh# | TL | 91ms | 357836kb | C++23 | 7.4kb | 2023-12-26 12:19:30 | 2023-12-26 12:19:30 |
Judging History
answer
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define shandom_ruffle random_shuffle
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;
const int d=450;
int n, q;
vector<pii> graf[nax];
pii kra[nax];//dziecko, ojciec
ll wag[nax];
int ojc[nax];
int dooj[nax];
int duzy[nax];
vi duze;
int super_ojciec[nax];
vi super_dzieci[nax];
int ktora_super[nax];
ll suma_super[nax];
vi sciezka[nax];
ordered_set<pll> dzieci[nax];
ordered_set<pll> gora_super[nax];
ordered_set<pll> dol_super[nax];
int wyn;
pii dfs1(int v)
{
for (pii &i : graf[v])
{
if (i.first==ojc[v])
{
swap(i, graf[v].back());
graf[v].pop_back();
break;
}
}
pii ret{0, 1};
for (pii i : graf[v])
{
ojc[i.first]=v;
dooj[i.first]=i.second;
pii wez=dfs1(i.first);
ret.first+=wez.first;
ret.second+=wez.second;
}
if (ret.first>1 || ret.second>=d || v==1)
{
ret={1, 0};
duzy[v]=1;
duze.push_back(v);
}
return ret;
}
vector<pll> glo;
void glo_zbierz(int v, ll s, int zakaz=-1, int startuje=1)
{
if (startuje)
glo.clear();
if (duzy[v])
return;
glo.push_back({s, v});
for (pii i : graf[v])
if (i.first!=zakaz)
glo_zbierz(i.first, s+wag[i.second], zakaz, 0);
}
//~ int od_gory_ograniczone(int v, ll x)
//~ {
//~ return dzieci[v].order_of_key({x, nax});
//~ }
void akcja(int v, int sko, ll lim)
{
if (lim<0)
return;
//~ debug() << v << " " << sko << " " << lim << " " << wyn;
wyn++;
wyn+=dzieci[v].order_of_key({lim, nax});
//~ debug() << v << " " << sko << " " << lim << " " << wyn;
if (v>1 && sko!=super_ojciec[v])
{
wyn+=gora_super[v].order_of_key({lim, nax});
akcja(super_ojciec[v], v, lim-suma_super[v]);
}
for (int i : super_dzieci[v])
{
if (i==sko)
continue;
wyn+=dol_super[i].order_of_key({lim, nax});
akcja(i, v, lim-suma_super[i]);
}
}
int znajdz_na_sciezce(int v, int kogo)
{
for (int i=0; i<(int)sciezka[v].size(); i++)
if (sciezka[v][i]==kogo)
return i;
assert(0);
}
void update(int v, ll w)
{
ll delta=w-wag[v];
ll stawag=wag[v];
wag[v]=w;
v=kra[v].first;
if (ktora_super[v])
{
int kt=ktora_super[v];
int g=znajdz_na_sciezce(kt, v);
ll nad=0;
ll pod=0;
for (int i=0; i<g; i++)
pod+=wag[dooj[sciezka[kt][i]]];
for (int i=g+1; i<(int)sciezka[kt].size(); i++)
nad+=wag[dooj[sciezka[kt][i]]];
glo_zbierz(sciezka[kt][g], nad);
for (pll j : glo)
{
dol_super[kt].erase({stawag+j.first, j.second});
dol_super[kt].insert({w+j.first, j.second});
}
for (int i=g+1; i<(int)sciezka[kt].size(); i++)
{
glo_zbierz(sciezka[kt][i], pod, sciezka[kt][i-1]);
for (pll j : glo)
{
gora_super[kt].erase({stawag+j.first, j.second});
gora_super[kt].insert({w+j.first, j.second});
}
pod+=wag[dooj[sciezka[kt][i]]];
}
suma_super[kt]+=delta;
return;
}
ll sum=0;
int u=ojc[v];
while(!ktora_super[u] && !duzy[u])
{
sum+=wag[dooj[u]];
u=ojc[u];
}
if (duzy[u])
{
glo_zbierz(v, sum);
for (pll j : glo)
{
dzieci[u].erase({stawag+j.first, j.second});
dzieci[u].insert({w+j.first, j.second});
}
}
else
{
int kt=ktora_super[u];
int g=znajdz_na_sciezce(kt, u);
ll nad=0;
ll pod=0;
for (int i=0; i<g; i++)
pod+=wag[dooj[sciezka[kt][i]]];
for (int i=g; i<(int)sciezka[kt].size(); i++)
nad+=wag[dooj[sciezka[kt][i]]];
//~ debug() << imie(sum) << imie(u) << imie(v) << imie(pod) << imie(nad);
glo_zbierz(v, sum);
for (pll j : glo)
{
gora_super[kt].erase({pod+stawag+j.first, j.second});
gora_super[kt].insert({pod+w+j.first, j.second});
dol_super[kt].erase({nad+stawag+j.first, j.second});
dol_super[kt].insert({nad+w+j.first, j.second});
}
}
}
int main()
{
scanf("%d%d", &n, &q);
for (int i=1; i<n; i++)
{
int a, b;
scanf("%d%d%lld", &a, &b, &wag[i]);
kra[i]={a, b};
graf[a].push_back({b, i});
graf[b].push_back({a, i});
}
dfs1(1);
for (int i=1; i<n; i++)
if (ojc[kra[i].second]==kra[i].first)
swap(kra[i].first, kra[i].second);
debug() << imie(duze);
for (int i=2; i<=n; i++)
{
if (!duzy[i])
continue;
int v=i;
while(v==i || !duzy[v])
{
sciezka[i].push_back(v);
ktora_super[v]=i;
suma_super[i]+=wag[dooj[v]];
v=ojc[v];
}
super_ojciec[i]=v;
super_dzieci[v].push_back(i);
ll w=0;
for (int j=1; j<(int)sciezka[i].size(); j++)
{
w+=wag[dooj[sciezka[i][j-1]]];
glo_zbierz(sciezka[i][j], 0, sciezka[i][j-1]);
for (pll l : glo)
{
gora_super[i].insert({w+l.first, l.second});
dol_super[i].insert({suma_super[i]-w+l.first, l.second});
}
}
}
for (int i=1; i<=n; i++)
{
if (!duzy[i])
continue;
for (pii j : graf[i])
{
if (ktora_super[j.first])
continue;
glo_zbierz(j.first, wag[j.second]);
for (pll l : glo)
dzieci[i].insert(l);
}
}
while(q--)
{
int typ;
scanf("%d", &typ);
if (typ==1)
{
int v;
ll w;
scanf("%d%lld", &v, &w);
update(v, w);
}
else
{
int v;
ll lim;
scanf("%d%lld", &v, &lim);
wyn=0;
int u=v;
int ost=-1;
while(!ktora_super[u] && !duzy[u])
{
glo_zbierz(u, 0, ost);
for (pll j : glo)
if (j.first<=lim)
wyn++;
ost=u;
lim-=wag[dooj[u]];
u=ojc[u];
}
if (duzy[u])
{
if (ost!=-1)
{
glo_zbierz(ost, wag[dooj[ost]]);
for (pll j : glo)
if (j.first<=lim)
wyn--;
}
akcja(u, -1, lim);
}
else
{
int w=ktora_super[u];
ll bylo=lim;
ll zbi=0;
while(!duzy[u])
{
glo_zbierz(u, 0, ost);
for (pll j : glo)
if (j.first<=lim)
wyn++;
ost=u;
zbi+=wag[dooj[u]];
lim-=wag[dooj[u]];
u=ojc[u];
}
akcja(u, w, lim);
akcja(w, u, bylo-(suma_super[w]-zbi));
}
printf("%d\n", wyn);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 91ms
memory: 357836kb
input:
3 7 1 2 3 2 3 1 2 2 1 2 1 3 2 3 4 1 1 1 2 2 1 2 1 0 2 3 1
output:
2 2 3 3 1 2
result:
ok 6 numbers
Test #2:
score: -100
Time Limit Exceeded
input:
200000 200000 1 2 146181238 2 3 45037818 3 4 176924060 4 5 969365276 5 6 683948619 6 7 356268194 7 8 871634797 8 9 630254480 9 10 283061123 10 11 204904965 11 12 838381393 12 13 516373812 13 14 253862710 14 15 223572474 15 16 114452997 16 17 145251056 17 18 905638436 18 19 375445402 19 20 549829545 ...
output:
219 62303 1358 5532 65345 682 11856 120285 4980 5689 2998 2314 18102 8014 20512 2827 113022 74534 159775 14517 17961 21855 8138 265 3336 3251 7023 35187 4932 151611 14338 101 899 117 64441 888 10380 1833 29381 1014 4806 10770 23734 236 37258 2280 14550 2196 38205 80950 80839 4517 74570 13972 95914 7...