脑袋确实是不好使了需要回家暴颓治疗
数数数树鼠树
真好玩。
数数
大水题一个,妥妥的签到题目,然后。。。
我没签上
气展了!!!
其实我还是想麻烦了。
就是我们实际上就是排序之后每一次找头上和尾巴上的就行
#include<bits/stdc++.h>
using std::cout; using std::endl;
#define try(i,a,b) for(register signed i=a;i<=b;++i)
#define throw(i,a,b) for(register signed i=a;i>=b;--i)
#define asm(i,x) for(register signed i=head[x];i;i=edge[i].next)
namespace xin_io
{
#define sb(x) cout<<#x" = "<<x<<' '
#define jb(x) cout<<#x" = "<<x<<endl
#define debug cout<<"debug"<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin),p1 == p2) ? EOF : *p1++
char buf[1<<20],*p1 = buf,*p2 = buf; int ak; typedef long long ll; typedef unsigned long long ull;
class xin_stream{public:template<typename type>inline xin_stream &operator >> (type &s)
{
register int f = 0;s = 0; register char ch = gc();
while(!isdigit(ch)) {f |= ch == '-'; ch = gc();}
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc(); return s = f ? -s : s,*this;
}}io;
}
using namespace xin_io; static const int maxn = 1e6+10,inf = 1e9+7; const ll llinf = 1e18+7;
#define int long long
namespace xin
{
int a[maxn],n;
int ans = 0,sum = 0,zhi1 = 0,zhi2 = 0;
inline short main()
{
io >> n;
try(i,1,n) io >> a[i];
std::sort(a+1,a+n+1);
zhi1 = 1; zhi2 = n;
try(i,1,n)
{
if(i & 1)
{
ans += sum;
}
else
{
sum += a[zhi2] - a[zhi1];
zhi2 --; zhi1 ++;
ans += sum;
}
cout<<ans<<endl;
}
return 0;
}
}
signed main() {return xin::main();}
数树
不会,鸽了。
鼠树
似乎我是没脸暴力水过去的。。。。
首先使用一个朴实无华的laji区间加减线段树。
然后我们还要有一个 \(vector\) 来记录每个黑色的点的管辖。
然后每个操作就是:
- 直接调用 \(query(1,1,n,id[x],id[x])\)
- 将 \(x\) 的子树加上 \(val\) ,然后要把剩下的 \(vector\) 当中的东西的子树都减掉 \(val\)
- 直接 \(query(1,1,n,id[x],id[x]+siz[x]-1)\)
- 就是反复进行 \(2\)
- 这个变化之后要进行\(vector\)记录
- 大致与 \(5\) 相同
#include<bits/stdc++.h>
using std::endl; using std::cout;
#define try(i,a,b) for(register signed i=a;i<=b;++i)
#define throw(i,a,b) for(register signed i=a;i>=b;--i)
#define asm(i,x) for(register signed i=head[x];i;i=edge[i].next)
namespace xin_io
{
#define debug cout<<"debug"<<endl
#define sb(x) cout<<#x" = "<<x<<' '
#define jb(x) cout<<#x" = "<<x<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin),p1 == p2) ? EOF : *p1 ++
char buf[1<<20],*p1 = buf,*p2 = buf; typedef long long ll; typedef unsigned long long ull;
class xin_stream{public:template<typename type>xin_stream &operator >> (type &s)
{
register bool f = 0; s = 0; register char ch = gc();
while(!isdigit(ch)) f |= ch == '-',ch = gc();
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc(); return s = f ? -s : s,*this;
}}io;
}
using namespace xin_io; static const int maxn = 5e6+10;
namespace xin
{
int n,m;
int fa[maxn];
class xin_edge{public:int next,ver;}edge[maxn];
int head[maxn],rp = 0;
inline void add(int x,int y) {edge[++rp].ver = y ;edge[rp].next = head[x]; head[x] = rp;}
int col[maxn],dep[maxn],siz[maxn],id[maxn],tot;
std::vector<int>cashe[maxn];
void dfs(int x)
{
siz[x] = 1; dep[x] = dep[fa[x]] + 1; id[x] = ++tot;
asm(i,x)
{
register int y = edge[i].ver;
if(y == fa[x]) continue;
dfs(y);
siz[x] += siz[y];
}
}
class xin_seg
{
private:
#define ls(fa) (fa << 1)
#define rs(fa) (fa << 1 | 1)
inline void up(int fa) {t[fa].s = t[ls(fa)].s + t[rs(fa)].s;}
inline void down(int fa,int l,int r)
{
if(!t[fa].debt) return ;
register int mid = l + r >> 1;
(t[ls(fa)].s += (mid - l + 1) * t[fa].debt); (t[rs(fa)].s += (r - mid) * t[fa].debt);
(t[ls(fa)].debt += t[fa].debt); (t[rs(fa)].debt += t[fa].debt);
t[fa].debt = 0;
}
public:
class xin_tree{public:unsigned s,debt;}t[maxn];
void update(int fa,int l,int r,int ql,int qr,int val)
{
if(ql <= l and qr >= r) return (t[fa].s += (r - l + 1) * val),(t[fa].debt += val),void();
register int mid = l + r >> 1;
down(fa,l,r);
if(ql <= mid) update(ls(fa),l,mid,ql,qr,val); if(qr > mid) update(rs(fa),mid+1,r,ql,qr,val);
up(fa);
}
unsigned query(int fa,int l,int r,int ql,int qr)
{
if(ql <= l and qr >= r) return t[fa].s;
register int mid = l + r >> 1;unsigned ret = 0;
down(fa,l,r);
if(ql <= mid) (ret += query(ls(fa),l,mid,ql,qr)); if(qr > mid) (ret += query(rs(fa),mid+1,r,ql,qr));
return ret;
}
}t;
int up[maxn];
inline short main()
{
io >> n >> m;
try(i,2,n)
{
io >> fa[i];
add(fa[i],i);
}
col[1] = 1; dfs(1);
try(cse,1,m)
{
register int op,k; io >> op >> k;
if(op == 1)
printf("%u\n",t.query(1,1,n,id[k],id[k]));
else if(op == 2)
{
register int val; io >> val;
t.update(1,1,n,id[k],id[k] + siz[k] - 1,val);
for(auto v : cashe[k]) t.update(1,1,n,id[v],id[v] + siz[v] - 1,-val);
}
else if(op == 3)
printf("%u\n",t.query(1,1,n,id[k],id[k] + siz[k] - 1));
else if(op == 4)
{
register int val; io >> val;
if(col[k] == 1)
{
t.update(1,1,n,id[k],id[k] + siz[k] - 1,val);
continue;
}
register int now = k;
while(!col[now]) now = fa[now];
for(auto v : cashe[now])
if(id[v] >= id[k] and id[v] <= id[k] + siz[k] - 1)
t.update(1,1,n,id[v],id[v] + siz[v] - 1,val);
}
else if(op == 5)
{
register int now = k;
while(!col[now]) now = fa[now];
up[k] = now;
int len = cashe[now].size() - 1;
try(i,0,len)
{
register int v = cashe[now][i];
if(id[v] >= id[k] and id[v] <= id[k] + siz[k] - 1)
{
cashe[k].push_back(v); up[v] = k;
cashe[now][i] = cashe[now][len];
i --; len --;
cashe[now].pop_back();
}
}
cashe[now].push_back(k);
col[k] = 1;
}
else
{
for(auto v : cashe[k])
{
cashe[up[k]].push_back(v);
up[v] = up[k];
}
cashe[k].clear();
int len = (int)cashe[up[k]].size() - 1;
try(i,0,len)
if(cashe[up[k]][i] == k)
{
cashe[up[k]][i] = cashe[up[k]][len];
cashe[up[k]].pop_back();
break;
}
up[k] = col[k] = 0;
}
}
return 0;
}
}
signed main() {return xin::main();}
ckw的树
俺不会,又鸽le.....