noip刷题记录 20170818

天天爱跑步

lca + 树上差分

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

const int N = 3e5 + 5, M = 3e5 + 5;
int n, m, ans[N], offset;
int ecnt, adj[N], go[M << 1], nxt[M << 1];
int w[N], dep[N], cnt[N << 2], fa[N][25];
struct node{
    int val, delta;
    node(){}
    node(int a, int b):val(a), delta(b){}
};
vector<node> tag[N];

inline void addEdge(int u, int v){
    nxt[++ecnt] = adj[u], adj[u] = ecnt, go[ecnt] = v;
    nxt[++ecnt] = adj[v], adj[v] = ecnt, go[ecnt] = u;
}

inline int getLca(int u, int v){
    if(dep[u] < dep[v]) swap(u, v);
    int delta = dep[u] - dep[v];
    for(int i = 20; i >= 0; i--)
        if((1 << i) & delta) u = fa[u][i];
    if(u == v) return u;
    for(int i = 20; i >= 0; i--)
        if(fa[u][i] != fa[v][i])
            u = fa[u][i], v = fa[v][i];
    return fa[u][0];
}

inline void dfs(int u, int f){
    fa[u][0] = f;
    for(int i = 1; i <= 20; i++)
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    for(int e = adj[u], v; e; e = nxt[e]){
        v = go[e];
        if(v != f){
            dep[v] = dep[u] + 1;
            dfs(v, u);
        }
    }
}

inline void solve(int u){
    int cur = cnt[dep[u] + w[u]] + cnt[w[u] - dep[u] + offset];
    for(int i = 0; i < tag[u].size(); i++)
        cnt[tag[u][i].val] += tag[u][i].delta;
    for(int e = adj[u], v; e; e = nxt[e]){
        if((v = go[e]) == fa[u][0]) continue;
        solve(v);
    }
    ans[u] = cnt[dep[u] + w[u]] + cnt[w[u] - dep[u] + offset] - cur;
}

inline int read(){
    int i = 0, f = 1; char ch = getchar();
    for(; (ch < ‘0‘ || ch > ‘9‘) && ch != ‘-‘; ch = getchar());
    if(ch == ‘-‘) f = -1, ch = getchar();
    for(; ch >= ‘0‘ && ch <= ‘9‘; ch = getchar())
        i = (i << 3) + (i << 1) + (ch - ‘0‘);
    return i * f;
}

inline void wr(int x){
    if(x < 0) putchar(‘-‘), x = -x;
    if(x > 9) wr(x / 10);
    putchar(x % 10 + ‘0‘);
}

int main(){
    n = read(), m = read(), offset = 3 * n + 1;
    for(int i = 1; i < n; i++){
        int x = read(), y = read();
        addEdge(x, y);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++) w[i] = read();
    for(int i = 1; i <= m; i++){
        int s = read(), t = read(), lca = getLca(s, t);
        tag[s].push_back(node(dep[s], 1));
        tag[t].push_back(node(dep[s] - 2 * dep[lca] + offset, 1));
        tag[fa[lca][0]].push_back(node(dep[s], -1));
        tag[lca].push_back(node(dep[s] - 2 * dep[lca] + offset, -1));
    }
    solve(1);
    for(int i = 1; i <= n; i++) wr(ans[i]), putchar(‘ ‘);
    return 0;
}

借教室

二分法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

const int N = 1e6 + 5;
int n, m, a[N], f[N], s[N], t[N], d[N], ans;

inline int read(){
    int i = 0, f = 1; char ch = getchar();
    for(; (ch < ‘0‘ || ch > ‘9‘) && ch != ‘-‘; ch = getchar());
    if(ch == ‘-‘) f = -1, ch = getchar();
    for(; ch >= ‘0‘ && ch <= ‘9‘; ch = getchar())
        i = (i << 3) + (i << 1) + (ch - ‘0‘);
    return i * f;
}

inline void wr(int x){
    if(x < 0) putchar(‘-‘), x = -x;
    if(x > 9) wr(x / 10);
    putchar(x % 10 + ‘0‘);
}

inline bool check(int mid){
    int sum = 0;
    memset(f, 0, sizeof f);
    for(int i = 1; i <= mid; i++)
        f[s[i]] += d[i], f[t[i] + 1] -= d[i];
    for(int i = 1; i <= n; i++){
        sum += f[i];
        if(sum > a[i]) return false;
    }
    return true;
}

int main(){
    n = read(), m = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 1; i <= m; i++) d[i] = read(), s[i] = read(), t[i] = read();
    int l = 1, r = m;
    while(l <= r){
        int mid = l + r >> 1;
        if(check(mid)) l = mid + 1;
        else ans = mid, r = mid - 1;
    }
    if(ans == 0) putchar(‘0‘);
    else{
        wr(-1), putchar(‘\n‘);
        wr(ans);
    }
    return 0;
}

线段树

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;

const int N = 1e6 + 5, oo = 0x3f3f3f3f;
int n, m, minn[N * 4], a[N], tag[N * 4];

inline int read(){
    int i = 0, f = 1; char ch = getchar();
    for(; (ch < ‘0‘ || ch > ‘9‘) && ch != ‘-‘; ch = getchar());
    if(ch == ‘-‘) f = -1, ch = getchar();
    for(; ch >= ‘0‘ && ch <= ‘9‘; ch = getchar())
        i = (i << 3) + (i << 1) + (ch - ‘0‘);
    return i * f;
}

inline void wr(int x){
    if(x < 0) putchar(‘-‘), x = -x;
    if(x > 9) wr(x / 10);
    putchar(x % 10 + ‘0‘);
}

inline void build(int k, int l, int r){
    if(l == r){
        minn[k] = a[l];
        return;
    }
    int mid = l + r >> 1, lc = k << 1, rc = k << 1 | 1;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    minn[k] = min(minn[lc], minn[rc]);
}

inline void minuss(int k, int v){
    minn[k] -= v;
    tag[k] += v;
}

inline void pushDown(int k){
    if(tag[k]){
        if(minn[k << 1] != -1) minuss(k << 1, tag[k]);
        if(minn[k << 1 | 1] != -1) minuss(k << 1 | 1, tag[k]);
        tag[k] = 0;
    }
}

inline void modify(int k, int l, int r, int x, int y, int v){
    if(x <= l && r <= y){
        minuss(k, v);
        return;
    }
    pushDown(k);
    int mid = l + r >> 1, lc = k << 1, rc = k << 1 | 1, ret = oo;
    if(x <= mid) modify(lc, l, mid, x, y, v);
    if(y > mid) modify(rc, mid + 1, r, x, y, v);
    minn[k] = min(minn[lc], minn[rc]);
}

inline int query(int k, int l, int r, int x, int y){
    if(x <= l && r <= y) return minn[k];
    pushDown(k);
    int mid = l + r >> 1, lc = k << 1, rc = k << 1 | 1, ret = oo;
    if(x <= mid) ret = min(ret, query(lc, l, mid, x, y));
    if(y > mid) ret = min(ret, query(rc, mid + 1, r, x, y));
    return ret;
}

int main(){
    n = read(), m = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    memset(minn, -1, sizeof tag);
    build(1, 1, n);
    for(int i = 1; i <= m; i++){
        int d = read(), s = read(), t = read();
        if(s > t) swap(s, t);
//        cout<<s<<" "<<t<<" "<<query(1, 1, n, s, t)<<"!!!"<<endl;
        if(query(1, 1, n, s, t) < d){
            wr(-1), putchar(‘\n‘), wr(i);
            return 0;
        }
        modify(1, 1, n, s, t, d);
    }
    wr(0);
    return 0;
}

积木大赛

简单分析

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 1e5 + 5;
int n, ans, last;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        int tmp; cin >> tmp;
        if(tmp - last > 0) ans += tmp - last;
        last = tmp;
    }
    cout << ans;
    return 0;
}

Vigenère密码

简单分析 + 模拟

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

int klen, slen, key[1005];
char k[1005], s[1005], t[1005];

int main(){
    cin >> k >> s;
    klen = strlen(k), slen = strlen(s);
    for(int i = 0; i < klen; i++){
        if(‘a‘ <= k[i] && k[i] <= ‘z‘)
            key[i] = k[i] - ‘a‘;
        else key[i] = k[i] - ‘A‘;
    }
    for(int i = 0; i < slen; i++){
        if(‘a‘ <= s[i] && s[i] <= ‘z‘){
            t[i] = s[i] - key[i % (klen)];
            while(t[i] < ‘a‘) t[i] += 26;
        }
        else if(‘A‘ <= s[i] && s[i] <= ‘Z‘){
            t[i] = s[i] - key[i % (klen)];
            while(t[i] < ‘A‘) t[i] += 26;
        }
        else continue;
        putchar(t[i]);
    }
    return 0;
}

花匠

动态规划 + 树状数组优化

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 1e5 + 5, H = 1e6 + 5;
int n, h[N], f[N], g[N], maxh, maxxF[H], maxxG[H], ans;

inline int read(){
    int i = 0, f = 1; char ch = getchar();
    for(; (ch < ‘0‘ || ch > ‘9‘) && ch != ‘-‘; ch = getchar());
    if(ch == ‘-‘) f = -1, ch = getchar();
    for(; ch >= ‘0‘ && ch <= ‘9‘; ch = getchar())
        i = (i << 3) + (i << 1) + (ch - ‘0‘);
    return i * f;
}

inline void wr(int x){
    if(x < 0) putchar(‘-‘), x = -x;
    if(x > 9) wr(x / 10);
    putchar(x % 10 + ‘0‘);
}

inline void uptF(int x, int v){
    for(int i = x; i <= maxh; i += (i&(-i)))
        maxxF[i] = max(maxxF[i], v);
}

inline void uptG(int x, int v){
    for(int i = x; i <= maxh; i += (i&(-i)))
        maxxG[i] = max(maxxG[i], v);
}

inline int queryF(int x){
    int ret = 0;
    for(int i = x; i > 0; i-=(i&-i))
        ret = max(maxxF[i], ret);
    return ret;
}

inline int queryG(int x){
    int ret = 0;
    for(int i = x; i > 0; i-=(i&-i))
        ret = max(ret, maxxG[i]);
    return ret;
}

int main(){
    n = read();
    for(int i = 1; i <= n; i++) h[i] = read() + 1, maxh = max(maxh, h[i]);
    maxh += 5;
    for(int i = 1; i <= n; i++){
        f[i] = max(queryG(h[i] - 1) + 1, f[i]);
        g[i] = max(queryF(maxh - h[i] - 1) + 1, g[i]);
        uptF(maxh - h[i], f[i]);
        uptG(h[i], g[i]);
        ans = max(ans, max(f[i], g[i]));
    }
    wr(ans);
    return 0;
}

时间: 2024-10-06 07:23:17

noip刷题记录 20170818的相关文章

noip刷题记录 20170823

独木桥 怎么说呢 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N = 5050; int n, l, pos[N], maxx = 0, minn = 0; int main(){ scanf("%d%d", &l, &n); for(int i = 1; i <= n; i++) scanf(&qu

BZOJ 刷题记录 PART 4

[BZOJ1143]CTSC的题目...先用floyed传递闭包,然后直接上匈牙利算法. [BZOJ1452]从未写过的二维树状数组.好像很简单.. struct two_bit { int f[305][305]; inline void add(int x,int z,int A) { for (;x<=n;x+=L(x)) for (int y=z;y<=m;y+=L(y)) f[x][y]+=A; } inline int ask(int x,int z) { int ans=0; f

[2015.6.28] OI刷题记录

FZSZOJ刷题记录: 1051 砝码称重: DP 多重背包 1058 liqeuer: 序列DP 1061 挖地雷:DP,注意需要倒过来做DP,同时记录路径. 1059 Number:DP 1054 数塔问题:同数字三角形,普通DP 1390 等式问题:爆搜,枚举每个+号或-号的位置 1006 中位数:维护大根堆+小根堆,每次插入调整 1005 Cube Stacking:并查集维护当前在第几个和当前集合的高度,并查集变种. 1073 DNA分子的最佳比对:序列DP 1110 奖学金:傻逼题,

首师大附中科创教育平台 我的刷题记录(3)

首师大附中科创教育平台我的刷题记录(给大家刷11--15题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { int x,y; cin>>x; y=abs(x+2); cout<<x*x-y+5<<endl; return 0; } 绝对值函数 #include

BZOJ 刷题记录 PART 5

拖了好久才写的. [BZOJ2821]接触分块大法.这道题略有点新颖.首先我们先分块,然后统计每块中每个数出现的个数. 下面是联立各个方块,预处理出第I个方块到第J个方块出现正偶数次数的个数. for (i=1;i<=s;i++) { for (j=i;j<=s;j++) { sum[i][j]=sum[i][j-1]; for (k=a[j].l;k<=a[j].r;k++) { temp[data[k]]++; if (!(temp[data[k]]&1)) sum[i][j

首师大附中科创教育平台 我的刷题记录(7)

首师大附中科创教育平台我的刷题记录(这次给大家刷多一点,31--40题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> using namespace std; int main() { int n; cin>>n; if(n%4==0&&n%100!=0||n%400==0) cout<<"yes"<<endl; else cout&

首师大附中科创教育平台 我的刷题记录(6)

首师大附中科创教育平台我的刷题记录(给大家刷26--30题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { double I,R,r,E; cin>>I>>R>>r; E=I*(R+r); cout<<E<<endl; return 0

首师大附中科创教育平台 我的刷题记录 0284 最强大脑

从现在开始,我的刷题记录都开始给大家一个一个刷!今天给大家献上“E”级题:最强大脑!! 试题编号:0284     最强大脑 难度级别:E: 运行时间限制:3000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 zhb国是一个传说中的国度,国家的居民叫做最强(chang)大脑(diao).Zhb国是一个N×M的矩形方阵,每个格子代表一个街区.然而zhb国是没有交通工具的.居民大脑(diao)们完全靠地面的弹射装置来移动.每个街区都装有弹射装置.使用弹射装置是需

首师大附中科创教育平台 我的刷题记录(5)

首师大附中科创教育平台我的刷题记录(给大家刷21--25题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { double a,b,x; cin>>a>>b; x=-b/a; cout<<x<<endl; return 0; } 一元一次方程 #in