"Hnoi Simulation by Lyp" Day1

Source: Internet
Author: User

1 xlk
1.1 Topic description
Given a tree without a root size of n , the number of four tuples (A, B, C, D) that meet the following conditions:
1.1 a < b n
2.1 c < d n
3. There is no point that makes this point at the same time the shortest path to point a to b and point c to D .
1.2 input format
The first row is a number n .
Next n 1 lines, two s for each line, T, represents an edge connecting a and b .
1.3 output format
Outputs the number of four tuples that meet the criteria.
1.4 Sample Input
4
1 2
2 3
3 4
1.5 Sample output
2
1.6 Data range
For 30% data, n 50 is satisfied;
For 100% of data, 1 n 80000 is satisfied.

Xlk
.

For each subtrees tree, find a chain in the sub-tree, a chain over the subtree number of schemes. This will weigh heavily. Then enumerate each subtrees tree, minus a chain over the root, a chain in a subtrees tree in the number of scenarios.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;

#define LY (P) freopen (P ". In", "R", stdin); Freopen (P ". Out", "w", stdout)
#define LL Long Long
#define DBL Double
#define LD long Double
#ifdef WIN32
#define LLD "%i64d"
#else
#define LLD "%lld"
#endif
#define N 80010
int n, x, y;
int h[n], ent;
int siz[n];
LL F[n], g[n], ans;

struct Edge {
int V, n;

Edge (int y = 0, int t = 0): V (y), n (t) {}
} e[n << 1];

void link (int x, int y) {e[++ ent] = Edge (Y, h[x]), h[x] = ent;}

void Dfs (int o, int ft) {
Siz[o] = 1;
LL d = 0;
for (int x = H[o], y; y = e[x].v, x; x = E[X].N)
if (Y! = ft) {
Dfs (y, O);
Ans + = g[o] * G[y] + f[o] * Siz[y] + siz[o] * F[y];
F[o] + = d * Siz[y] + siz[o] * G[y] + f[y];
G[o] + = siz[o] * Siz[y] + g[y];
Siz[o] + = Siz[y];
D + = G[y];
}
}

int main ()
{
LY ("Xlk");
scanf ("%d", &n);
for (int i = 1; i < n; i++)
scanf ("%d%d", &x, &y), link (x, y), link (y, x);

DFS (1, 0);
printf (LLD, ans << 1);
return 0;
}

2 wwwwodddd
2.1 Topic Description
Defines a number x is Happy number, and if and only if the sum of squares of all digits of that number is obtained, the new
There is the sum of squares of bits, so repeated, and the end result is.
For example, 19 is a Happy number:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Given L, R , the number of Happy numbers in [L, R].
2.2 Input format
The first line is a positive integer T , which represents the number of data groups.
Next T line, two positive integers per line, indicates L, R .
2.3 output format
For each set of test data, output an integer that represents the number of Happy numbers in this interval.
2.4 Sample input
2
2 6
1 10
2.5 Sample output
0 3
2.6 Data range
For 30% data, the R 105 is satisfied;
For 100% data, 1 L R 1018, 1 T /c26>200.

Wwwwodddd
Digital DP.

Obviously any number after a transformation will be less than 2000, so preprocessing f[i][j] means I, the sum of squares is the number of J, and then preprocess the [0, 2000] in all Happy number, digital DP can be.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;

#define LY (P) freopen (P ". In", "R", stdin); Freopen (P ". Out", "w", stdout)
#define LL Long Long
#define DBL Double
#define LD long Double
#ifdef WIN32
#define LLD "%i64d"
#else
#define LLD "%lld"
#endif
BOOL vis[1500], hap[1500];
int T, m, now = 0;
LL L, R, f[2][1500][2];

int C (int o) {
int s = 0;
while (O)
s + = (o%) * (o%), O/= 10;
return s;
}

int dfs (int o) {
if (Vis[o]) return hap[o];
Vis[o] = 1;
return Hap[o] = (o > 1? dfs (C (o)): 1);
}

void Prep () {
m = 1458;
for (int i = 1; I <= m; i++)
DFS (i);
}

ll work (ll s) {
memset (f, 0, sizeof (f));
F[now][0][0] = 1;
for (int nt = 0, ns = 0; s; s/= 10) {
NT = s% 10;
for (int s = 0; s <= ns; s++)
for (int r = 0; r <= 1; r++)
if (F[now][s][r])
for (int i = 0; I <= 9; i++)
F[now ^ 1][s + i * i][i > NT | | (r && i = = NT)] + = F[now][s][r];

memset (F[now], 0, sizeof (F[now])), now ^= 1;
NS + = 81;
}
LL ans = 0;
for (int s = 1; s <= m; s++)
if (Hap[s])
Ans + = f[now][s][0];
return ans;
}

int main ()
{
LY ("wwwwodddd");
Prep ();
scanf ("%d", &t);
for (int TT = 1; TT <= T; tt++) {
scanf (LLD LLD, &l, &r);
printf (LLD "\ n", Work (R)-Work (L-1));
}
return 0;
}

3 zradiance
3.1 Topic description
Given a sequence S , the following actions are supported:
• Insert a number x so that after inserting x is the k element of the sequence
• Delete element K
• Flip Zone [L, R]
• given interval [L, R], ask

∑ (sy sx)

L x y R
3.2 Input format
The first line is two integers n, m , indicating the initial sequence length and the operand.
The second row of n integers, describing the initial sequence.
Next m line, each line is formatted as:
1 k x
·2 K
·3 L R
4 L R
meaning see the surface.
3.3 output format
For each query, the output requires the value of an expression.
3.4 Sample input
2 5
1 2
4 1 2
1 1 3
2 2
3 1 2
4 1 2
3.5 Sample Output
1 1
3.6 Data range
For 30% of data, it satisfies N, M 103
For 100% of data, it satisfies N, M 105

Zradiance
It is obvious that the interval is additive. So it is an ordinary sequence maintenance problem.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;

#define LY (P) freopen (P ". In", "R", stdin); Freopen (P ". Out", "w", stdout)
#define LL Long Long
#define DBL Double
#define LD long Double
#ifdef WIN32
#define LLD "%i64d"
#else
#define LLD "%lld"
#endif
#define N 100010
int n, m, opt, X, K, L, R;
int val[n];

struct Splaytree {
struct Node {
int ch[2], fa, siz;
int rev;
LL Val, lsum, Rsum, sum;
};

int siz, RT;
Node A[n << 1];

Splaytree (): siz (0), RT (0) {}

int newnode (int v, int ft) {
Return siz + +, A[siz].val = A[siz].sum = A[siz].lsum = A[siz].rsum = V, a[siz].rev = 0, A[siz].siz = 1, a[siz].fa = ft, si Z
}

void update (int o) {
A[o].siz = a[A[o].ch[0]].siz + a[a[o].ch[1]].siz + 1;
A[o].sum = a[A[o].ch[0]].sum + a[a[o].ch[1]].sum + a[o].val;
A[o].lsum = a[A[o].ch[0]].lsum + a[a[o].ch[1]].lsum + (A[o].val + a[a[o].ch[1]].sum) * (a[a[o].ch[0]].siz + 1);
A[o].rsum = a[a[o].ch[1]].rsum + a[a[o].ch[0]].rsum + (A[o].val + a[a[o].ch[0]].sum) * (a[a[o].ch[1]].siz + 1);
}

void rev (int o) {
A[o].rev ^= 1;
Swap (a[o].lsum, a[o].rsum);
Swap (a[o].ch[0], a[o].ch[1]);
}

void push (int o) {
if (A[o].rev)
Rev (A[o].ch[0]), rev (A[o].ch[1]), A[o].rev = 0;
}

void rotate (int o, int d) {
int f = A[O].FA, ff = A[F].FA, FD = (a[ff].ch[1] = = f);
a[f].ch[d ^ 1] = A[o].ch[d], a[a[o].ch[d]].fa = f;
A[o].ch[d] = f, a[f].fa = O;
A[FF].CH[FD] = o, A[O].FA = ff;
Update (f);
if (Rt = = f) rt = O;
}

void splay (int o, int ft = 0) {//Note that the ' to find ' node
for (int f, FF, D, FD; A[o].fa! = ft;) {
f = a[o].fa, ff = A[F].FA;
D = a[f].ch[1] = = O, fd = a[ff].ch[1] = = f;
if (ff = = ft)
Rotate (o, D ^ 1);
Else
if (d = = FD)
Rotate (f, fd ^ 1), rotate (o, D ^ 1);
Else
Rotate (o, D ^ 1), rotate (o, fd ^ 1);
}
Update (o); Remember it
}

int find (int k) {
int o = RT;
while (1) {
Push (O); Push the tag while searching the node
if (k = = a[A[o].ch[0]].siz + 1)
return o;
if (K < a[a[o].ch[0]].siz + 1)
o = a[o].ch[0];
Else
K-= a[a[o].ch[0]].siz + 1, o = a[o].ch[1];
}
}

int get (int L, int R) {//original rank
int L, R;
R = Find (r + 1 + 1), splay (R); All Rank + 1
L = Find (L-1 + 1), splay (L, R); All Rank + 1
return l;
}

void Insert (int k, int v) {
int o = Get (k, k-1);
A[O].CH[1] = NewNode (V, O);
Splay (o);
}

void Remove (int k) {
int o = Get (k, k);
A[O].CH[1] = 0;
Splay (o);
}

void reverse (int L, int R) {
int o = A[get (L, R)].ch[1];
Rev (O), push (O);
Splay (o);
}

LL query (int L, int R) {
int o = A[get (L, R)].ch[1];
LL s = a[o].lsum-a[o].rsum;
Push (O), splay (o);
return s;
}

void build (int &o, int ft, int *val, int l, int r) {
if (L > R) return;
int Mid (L + R >> 1);
o = NewNode (Val[mid], ft);
Build (A[o].ch[0], O, Val, L, Mid-1);
Build (A[o].ch[1], O, Val, mid + 1, R);
Update (o);
}

void init (int *val, int n) {
RT = NewNode (0, 0), a[rt].ch[1] = newnode (0, RT);
Build (a[a[rt].ch[1]].ch[0], a[rt].ch[1], Val, 1, N);
Splay (a[rt].ch[1]);
}
} *t = new Splaytree;

int main ()
{
LY ("Zradiance");
scanf ("%d%d", &n, &m);
for (int i = 1; I <= n; i++)
scanf ("%d", Val + i);
T-> Init (val, n);
for (int i = 1; I <= m; i++) {
scanf ("%d", &opt);
if (opt = = 1)
scanf ("%d%d", &k, &x), t-> insert (k, x);
if (opt = = 2)
scanf ("%d", &k), t-> remove (k);
if (opt = = 3)
scanf ("%d%d", &l, &r), t-> reverse (L, R);
if (opt = = 4)
scanf ("%d%d", &l, &r), printf (LLD "\ n", t-> query (L, R));
}
return 0;
}

The above puzzle fromZZD

"Hnoi Simulation by Lyp" Day1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.