NOI 2015 分类: noi 2015-08-06 22:22 14人阅读 评论(0) 收藏
我只会做送分题。。。
Day1
T1 离散化+并查集
T2 树链剖分,线段树维护区间内元素个数
Day2
T1 Huffman树,Huffman树是严格k叉树是比较好处理的,所以加入一些权值为0的点使得 点数 mod (k - 1) = 1
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
const int maxn = 100005;
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
struct vetor
{
#define begin() (__arr + 1)
#define end() (__arr + __len + 1)
int *__arr, __len;
void set(int size)
{
__len = 0, __arr = (int*)malloc(sizeof(int)*size);
}
void push_back(int x)
{
__arr[++__len] = x;
}
int size()
{
return __len;
}
void unique()
{
std::sort(begin(),end());
__len = std::unique(begin(),end()) - begin();
}
int find(int x)
{
return std::lower_bound(begin(), end(), x)-begin()+1;
}
int get(int x)
{
return __arr[x];
}
void erase()
{
__len = 0;
}
#undef begin
#undef end
}bowl;
int n, fa[maxn<<1];
int find(int x)
{
return (x == fa[x])?x:(fa[x] = find(fa[x]));
}
struct option
{
int i, j, e;
void scan()
{
read(i), read(j), read(e);
bowl.push_back(i);
bowl.push_back(j);
}
void prework()
{
i = bowl.find(i);
j = bowl.find(j);
}
void gather()
{
if(i > j) std::swap(i, j);
fa[find(j)] = find(i);
}
bool check()
{
return find(i) == find(j);
}
}op[maxn];
int main()
{
int T;
freopen("prog.in","r",stdin);
freopen("prog.out","w",stdout);
std::cin >> T, bowl.set(maxn<<1);
while(T--)
{
bool ans = true;
bowl.erase();
std::cin >> n;
for(int i = 1; i <= n; i++)
op[i].scan();
bowl.unique();
for(int i = 1; i <= bowl.size(); i++)
fa[i] = i;
for(int i = 1; i <= n; i++)
op[i].prework();
for(int i = 1; i <= n; i++)
if(op[i].e) op[i].gather();
for(int i = 1; i <= n; i++)
if(!op[i].e && op[i].check())
{
ans = false;
break;
}
if(ans) puts("YES");
else puts("NO");
}
fclose(stdin);
fclose(stdout);
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
const int maxn = 100005, Nya = -1;
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
int n, q;
namespace seg
{
#define L(x) ((x)<<1)
#define R(x) ((x)<<1|1)
int tag[maxn<<2], cnt[maxn<<2];
void set()
{
cnt[1] = tag[1] = 0;
}
int pushdown(int x,int ll,int rr)
{
int mid = (ll + rr) >> 1;
if(tag[x] != Nya)
{
cnt[L(x)] = tag[x]*(mid - ll + 1);
cnt[R(x)] = tag[x]*(rr - mid);
tag[L(x)] = tag[R(x)] = tag[x];
tag[x] = Nya;
}
return mid;
}
void update(int x)
{
cnt[x] = cnt[L(x)] + cnt[R(x)];
}
void color(int ll,int rr,int l,int r,int si,int col)
{
if(ll == l && rr == r)
{
tag[si] = col;
cnt[si] = (rr - ll + 1)*col;
return;
}
int mid = pushdown(si, ll, rr);
if(mid < l) color(mid + 1, rr, l, r, R(si), col);
else if(r <= mid) color(ll, mid, l, r, L(si), col);
else
{
color(ll, mid, l, mid, L(si), col);
color(mid + 1, rr, mid + 1, r, R(si), col);
}
update(si);
}
int count()
{
return cnt[1];
}
#undef L
#undef R
}
namespace tree
{
struct Edge
{
int v, next;
Edge(int v = 0,int next = 0):v(v),next(next){}
}edge[maxn];
int el, head[maxn];
int size[maxn], fa[maxn];
int dfn[maxn], dl;
int top[maxn], son[maxn];
int fc[maxn], end[maxn];
void NewEdge(int u,int v)
{
edge[++el] = Edge(v, head[u]), head[u] = el;
}
void init()
{
fa[1] = 0;
for(int i = 2; i <= n; i++)
{
read(fa[i]), fa[i]++;
NewEdge(fa[i], i);
}
}
void dfs(int a)
{
// dep[a] = dep[fa[a]] + 1;
size[a] = 1, son[a] = 0;
for(int i = head[a], p; i ; i = edge[i].next)
{
dfs(p = edge[i].v);
size[a] += size[p];
if(size[p] > size[son[a]])
son[a] = p;
}
return;
}
void build(int a)
{
dfn[++dl] = a, fc[a] = dl;
if(son[fa[a]] == a)
top[a] = top[fa[a]];
else
top[a] = a;
if(son[a]) build(son[a]);
for(int i = head[a], p; i ; i = edge[i].next)
if((p = edge[i].v) != son[a]) build(p);
end[a] = dl;
}
void prework()
{
dfs(1), build(1);
}
void install(int c)
{
while(c)
{
seg::color(1, n,fc[top[c]],fc[c], 1, 1);
c = fa[top[c]];
}
}
void uninstall(int c)
{
seg::color(1, n,fc[c],end[c], 1, 0);
}
}
void Solve()
{
int last = 0, now;
for(int i = 1, x; i <= q; i++)
{
static char str[10];
scanf("%s",str);
read(x), x++;
if(str[0] == 'i')
tree::install(x);
else
tree::uninstall(x);
now = seg::count();
write(abs(now - last));
last = now, puts("");
}
}
int main()
{
freopen("manager.in","r",stdin);
freopen("manager.out","w",stdout);
std::cin >> n;
tree::init();
tree::prework();
seg::set();
std::cin >> q, Solve();
// std::cerr << clock()*1.0/CLOCKS_PER_SEC << std::endl;
fclose(stdin);
fclose(stdout);
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const int maxn = 1e5+15;
typedef std::pair<long long,int> heapnode;
#define Root_Min std::vector<heapnode>,std::greater<heapnode>
int n, k; long long w[maxn], total;
std::priority_queue<heapnode, Root_Min> heap;
int main()
{
heapnode top;
freopen("epic.in","r",stdin);
freopen("epic.out","w",stdout);
std::cin >> n >> k;
for(int i = 1; i <= n; i++) read(w[i]);
for(int i = 1; i <= n; i++)
heap.push(std::make_pair(w[i], 0));
while(n%(k-1) != 1%(k-1))
heap.push(std::make_pair(w[++n], 0));
while(heap.size() != 1)
{
long long cost = 0;
int depth = 0;
for(int i = 1; i <= k; i++)
{
if(heap.empty()) break;
top = heap.top();
cost += top.first;
depth = std::max(depth, top.second + 1);
heap.pop();
}
total += cost;
heap.push(std::make_pair(cost, depth));
}
std::cout << total << std::endl;
std::cout << heap.top().second;
fclose(stdin);
fclose(stdout);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。