训练网址
#include
using namespace std;
const int N = 100010;
int tr[N], age[N];
vector<int> vec[N];
int h[N], e[N], ne[N], idx;
int fa[N][20];
int ans[N];
int lowbit(int x)
{
return x & -x;
}
void insert(int x, int c)
{
for(int i = x; i <= 100000; i += lowbit(i)){
tr[i] += c;
}
}
int sum(int x)
{
int res = 0;
for(int i = x; i; i -= lowbit(i)){
res += tr[i];
}
return res;
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void init()
{
queue<int> que;
//别忘把根结点 push 进去
que.push(1);
while(que.size()){
int u = que.front(); que.pop();
for(int i = h[u]; i != -1; i = ne[i]){
int v = e[i];
que.push(v);
fa[v][0] = u;
for(int k = 1; k <= 17; k++) fa[v][k] = fa[fa[v][k - 1]][k - 1];
}
}
}
void dfs(int u)
{
for(auto p : vec[u]){
insert(p, 1);
}
ans[u] = sum(age[u]);
for(int i = h[u]; i != -1; i = ne[i]){
int v = e[i];
dfs(v);
}
for(auto p : vec[u]){
insert(p, -1);
}
}
int main()
{
memset(h, -1, sizeof h);
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%d", &age[i]);
int p;
scanf("%d", &p);
if(i != 1) add(p, i);
}
age[0] = 1e9;
init();
while(m--){
int oj, lj, rj;
scanf("%d%d%d", &oj, &lj, &rj);
int u = oj;
for(int k = 17; k >= 0; k--){
if(age[fa[u][k]] <= rj){
u = fa[u][k];
}
}
vec[u].push_back(lj);
}
dfs(1);
for(int i = 1; i <= n; i++){
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
#include
using namespace std;
typedef long long ll;
const int N = 60;
ll f[N][N], a[N];
int w[N];
int n, k;
ll dfs(int pos, int k, int lim)
{
if(pos == 0) {
return k == 0;
}
if(lim == 0 && f[pos][k] != -1){
return f[pos][k];
}
else{
ll ans = 0, up = lim ? w[pos] : 1;
for(int i = 0; i <= up; i++){
if(k > 0) ans += dfs(pos - 1, i == 0 ? k : k - 1, lim && i == up);
if(!k && !i) ans += dfs(pos - 1, k, lim && i == up);
}
if(lim == 0){
f[pos][k] = ans;
}
return ans;
}
}
ll solve(ll x)
{
memset(f, -1, sizeof f);
memset(w, 0, sizeof w);
for(int i = 1; i <= n; i++){
if(x >= a[i]){
w[n - i + 1] = 1;
x -= a[i];
}
}
return dfs(n, k, 1);
}
int main()
{
ll A, B;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
scanf("%lld%lld", &A, &B);
sort(a + 1, a + n + 1, greater<ll>());
printf("%lld\n", solve(B) - solve(A - 1));
return 0;
}
#include
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 100010;
vector<int> son[N];
ll g[N], f[N][2];
ll pre[N], suf[N];
void dfs(int u)
{
int sz = son[u].size();
if(sz == 0){
f[u][1] = g[u] = 1;
return;
}
for(int i = 0; i < sz; i++){
int v = son[u][i];
dfs(v);
}
//这个地方千万不要初始化。
f[u][0] = 1;
for(int i = 0; i < sz; i++){
int v = son[u][i];
f[u][0] = f[u][0] * g[v] % mod;
}
pre[0] = 1, suf[sz + 1] = 1;
for(int i = 1; i <= sz; i++){
int v = son[u][i - 1];
pre[i] = pre[i - 1] * g[v] % mod;
}
for(int i = sz; i >= 1; i--){
int v = son[u][i - 1];
suf[i] = suf[i + 1] * g[v] % mod;
}
for(int i = 1; i <= sz; i++){
int v = son[u][i - 1];
f[u][1] = (f[u][1] + f[v][1] * pre[i - 1] % mod * suf[i + 1] % mod) % mod;
}
g[u] = (f[u][1] + f[u][0]) % mod;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 2; i <= n; i++){
int x;
scanf("%d", &x);
son[x].push_back(i);
}
dfs(1);
printf("%lld\n", g[1]);
return 0;
}
#include
using namespace std;
const int N = 110;
bitset<N> a[N];
int g[N][N], d[N];
int n, m;
bool gauss()
{
int r, c;
for(c = 1, r = 1; c <= n; c++){
int t = r;
for(int i = r; i <= n; i++){
if(a[i][c]){
t = i;
break;
}
}
if(a[t][c] == 0) continue;
swap(a[t], a[r]);
for(int i = 1; i <= n; i++){
if(a[i][c] && i != r){
a[i] ^= a[r];
}
}
r++;
}
if(r <= n){
for(int i = r; i <= n; i++){
if(a[i][n + 1]) return false;
}
}
return true;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
int a, b;
scanf("%d%d", &a, &b);
d[a]++, d[b]++;
g[a][b] = g[b][a] = 1;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(g[i][j]) a[i][j] = 1;
}
if(d[i] & 1){
a[i][n + 1] = 0, a[i][i] = 1;
}
else{
a[i][n + 1] = 1;
}
}
if(gauss()){
printf("Y\n");
}
else printf("N\n");
}