2025 国庆集训 DAY3

· · 个人记录

T1 等腰三角形(isosceles,1s,512MB)

题目描述

小明有两个等腰三角形,边长分别为 a_{1},b_{1},c_{1} 和 a_{2},b_{2},c_{2},中保证 a_{1}=b_{1},2 a_{1}>c_{1} 和 a_{2}=b_{2},2 a_{2}>c_{2}。
小明想知道,在底边平行的前提下,是否可以将其中一个等腰三角形完全的放在另一个等腰三角形的内部,如果可以,输出Yes,否则输出 No。

输入格式

第一行一个整数 T 表示数据组数。
对于每组数据,输入一行六个正整数 a_{1},b_{1},c_{1},a_{2},b_{2},c_{2}。

输出格式

对于每组数据,输出 Yes 或者 No。

输入输出样例#1

说明/提示

如果 h_1≤h_2 即为 Yes,否则 No。

推一下:

\Rightarrow 4a_1^2-c_1^2≤4a_2^2-c_2^2\Rightarrow 4a_1^2+c_2^2≤4a_2^2+c_1^2

最后只需要检查 4a_1^2+c_2^2≤4a_2^2+c_1^2 就行了,避免了精度问题。

代码

自己的:

#include<cstdio>
#include<iostream>
#include<cmath>
#define ll __int128
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
int T ;
ll a1,b1,c1,a2,b2,c2 ;
bool check_h(ll a,ll b,ll c,ll x,ll y,ll z) {
    ll p = a * a * 4 + z * z ;
    ll q = x * x * 4 + c * c ;
    return p <= q ;
}
int main () {
    //freopen("isosceles.in","r",stdin) ;
    //freopen("isosceles.out","w",stdout) ;
    read(T) ;
    while (T--) {
        read(a1),read(b1),read(c1),read(a2),read(b2),read(c2) ;
        if (c1 > c2) swap(a1,a2),swap(b1,b2),swap(c1,c2) ;//底边小的是第一个三角形
        if (check_h(a1,b1,c1,a2,b2,c2)) printf("Yes\n") ;
        else printf("No\n") ; 
    } 
    return 0 ;
}

std:

#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N = 1.01e6 ;
signed main(){
    //freopen("isosceles.in","r",stdin);
    //freopen("isosceles.out","w",stdout);
    int T ;
    cin>>T ;
    while (T--) {
        int a,b,c,d ;
        cin>>a ;cin>>a ;cin>>b ;
        cin>>c ;cin>>c ;cin>>d ;
        if (d == b) {
            printf("Yes\n") ;
            continue ;
        }
        if (d > b) swap(d,b),swap(c,a) ;
        a *= 2,c *= 2 ;
        int x = a * a - b * b,y = c * c - d * d ;
        if (x >= y) printf("Yes\n") ;
        else printf("No\n") ;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

T2 修复括号串 (repair,1s,512MB)

题目描述

给定一个长度为 n(n 为偶数),由左右括号组成的字符串 S。
每次操作可以修改 S 中的某个字符,求出最少的操作次数,使 S 变成一个括号串。
我们定义满足以下条件的字符串为括号串:

  1. 空串是括号串。
  2. 如果 A 是括号串,(A) 是括号串。
  3. 如果 A,B 都是括号串,A B 是括号串。

输入格式

第一行输入一个整数 n。
第二行输入一个长度为 n 的字符串 S,S 中每个字符为左括号或者右括号。

输出格式

输出一行一个整数,表示最少的操作次数。

输入输出样例 #1

输入输出样例 #2

说明/提示

signed main(){ //freopen("repair.in","r",stdin) ; //freopen("repair.out","w",stdout) ;

cin>>n ;
scanf("%s",s + 1) ;
for (int i = 1 ;i <= n ; i++) {
    if (s[i] == '(') ++x ;
    else ++y ;
}
for (int i = 1 ; i <= n ; i++) {
    if (s[i] == ')' && x < y) s[i] = '(',++x,--y,++ans1 ;
}
for(int i = n ; i >= 1 ; i--) {
    if (s[i] == '(' && x > y) s[i] = ')',--x,++y,++ans1 ;
}
int L = 0,R = n / 2,ans = n / 2 ;
while (L <= R) {
    int M = (L + R) / 2 ;
    if (ck(M)) ans = M,R = M - 1 ;
    else L = M + 1 ;
}
printf("%d\n",ans1 + ans * 2) ;
//fclose(stdin) ;
//fclose(stdout) ;
return 0 ;

}

### T3 括号路径 (bracketpath,1s,512MB)
#### 题目描述
有一个 n 个点 m 条边的无向图,每条边上标有左括号 "$($" ,或者右括号 "$)$" 。  
我们定义满足以下条件的字符串为括号串:
1. 空串是括号串。
2. 如果 $A$ 是括号串,$(A)$是括号串。
3. 如果 $A$,$B$ 都是括号串,$A B$ 是括号串。

我们定义一条括号路径满足以下条件:从起点到终点经过的所有边上标的括号按经过的先后顺序写下来,构成的字符串是括号串。  

有 $q$ 组询问,每次询问起点为 $x$,终点为 $y$ 的最短括号路径的长度,如果不存在合法路径则输出 $-1$。

#### 输入格式
第一行输入三个整数 $n$,$m$,$q$。  
接下来 $m$ 行,每行两个整数和一个字符: $u_{i}$,$v_{i}$,$c_{i}$,表示一条连接节点 $u_{i}$ 和 $v_{i}$ 的无向边($u_{i} ≠v_{i}$),上面标记的括号为 $c_{i} \in\{ "(", ")" \}$。  
接下来 q 行,每行两个整数 $x$,$y$,表示一组询问的起点和终点。

#### 输出格式
输出 q 行每行一个整数表示对应询问的答案。

#### 输入输出样例 #1
- 输入 #1

4 5 5 1 2 ( 2 3 ) 3 4 ( 4 1 ) 1 3 ( 1 1 1 3 1 4 1 2 2 1

- 输出 #1

0 2 4 2 6


#### 输入输出样例 #2
- 输入 #2

2 1 1 1 2 ( 1 2

- 输出 #2

-1


#### 说明/提示
- 对于 $20\%$ 的数据,$1≤n≤5$,$1 ≤m ≤10$。
- 对于另外 $30\%$ 的数据,$1 ≤n ≤20$,$1 ≤m ≤50$。
- 对于另外 $20\%$ 的数据,$q ≤5$。
- 对于全部数据,$1 ≤n ≤400$,$1 ≤m ≤500$,$1 ≤u_{i}$,$v_{i} ≤n$,$u_{i} ≠v_{i}$,$1 ≤q ≤10^{5}$。
---
### 题解
一些东西:  

1. 平衡度:起点到某一点的路径中,左括号比右括号多的数量。
2. $dis_{(s,i,j)}$:起点 $s$ 到点 $i$,平衡度为 $j$ 的**最短路径长度**。
3. $check$,$pq$:打标记用的数组,优先队列(存<0>当前路径长度,<1>当前节点,<2>当前平衡度)。

$Dijkstra$ 的实现:

1. 算平衡度:是 `(` 就 $+1$,否则 $-1$。
2. 查越界:平衡度保持非负,且不能超过上界。
3. 转移(松弛):当新路径长度小于当前记录值时,更新状态。

路径的合法条件:最后平衡值为 $0$,且中途任意时刻平衡值不能为负(其实就是避免 `)(` 的出现)

时间复杂度:$O\big(Nn^2\log n\big)$($N$ 为平衡值上界)(?)($n$ 和 $m$ 差不多我就不管了)

### 代码
#### 自己的:
```cpp
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<tuple>
#include<cstring>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
const int N = 402 ;  // 平衡度上界,数组大小
struct edge {
    int v,w ;  // w: 括号类型,'('为1,')'为-1
    edge() {}
    edge(int _v,int _w) {v = _v,w = _w ;}
};
int n,m,q ;
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>>pq ;  // 堆:(路径长度, 当前节点, 括号平衡值)
vector<edge> g[N] ;  // 邻接表
bool check[N][N] ;   // 标记数组
int dis[N][N][N] ;   // dis[s][i][j]: 从起点s到节点i,平衡度为j的最短路

void dijkstra(int s) {
    memset(check,0,sizeof(check)) ;  // 初始化标记数组
    for (re int i = 1 ; i <= n ; ++i) {
        for (re int j = 0 ; j < N ; ++j) {
            dis[s][i][j] = 0x3f3f3f3f ;  // 初始化所有距离为无穷大
        }
    }
    dis[s][s][0] = 0 ;  // 起点到自身的平衡值为0,路径长度为0
    pq.push(make_tuple(0,s,0)) ;  // 初始状态入队
    while (!pq.empty()) {
        tuple<int,int,int> nd = pq.top() ;
        pq.pop() ;
        int minn = get<0>(nd),minx = get<1>(nd),num = get<2>(nd) ;  // 当前路径长度、当前节点、当前括号平衡值
        if (check[minx][num]) continue ;  // 如果该状态已处理过,跳过
        check[minx][num] = 1 ;  // 标记该状态已处理
        for (re edge e : g[minx]) {  // 遍历当前点所有邻边
            int v = e.v,w = e.w ;  // v:邻接节点,w:括号类型
            int now = num + w ;    // 更新后的括号平衡度
            if (now < 0 || now >= N) continue ;  // 平衡值必须非负且小于N,否则越界
            if (dis[s][v][now] > minn + 1) {  // 如果找到更短路
                dis[s][v][now] = minn + 1 ;   // 更新最短路
                pq.push(make_tuple(dis[s][v][now],v,now)) ;  // 新状态入队
            }
        }
    }
}

int main () {
    //freopen("bracketpath.in","r",stdin) ;
    //freopen("bracketpath.out","w",stdout) ;
    read(n),read(m),read(q) ;
    for (re int i = 1,x,y ; i <= m ; ++i) {
        re char c ;
        read(x),read(y) ;
        cin>>c ;
        // 括号换为数值:'('->1,')'->-1,
        g[x].emplace_back(y,c == '(' ? 1 : -1) ;
        g[y].emplace_back(x,c == '(' ? 1 : -1) ;
    }
    // 对每个起点Dij,算出所有点的所有平衡值的最短路
    for (re int s = 1 ; s <= n ; ++s) dijkstra(s) ;
    for (re int i = 1,x,y ; i <= q ; ++i) {
        read(x),read(y) ;
        // 起点x,终点y且平衡值为0的最短路
        if (dis[x][y][0] == 0x3f3f3f3f) write(-1,endl) ;  // 不存在是-1
        else write(dis[x][y][0],endl) ;
    }
    return 0 ;
}

std(SPFA):

#include <bits/stdc++.h>
using namespace std;
const int N = 410,mod = 998244353,inf = 1.01e9 ;
int n,m,q ;
struct node{
    int x,y ;
    node(int x = 0,int y = 0) : x(x),y(y) {}
};
vector<node> g[N] ;
int d[N][N][N],vis[N][N] ;
queue<node> Q ;

void dij(int S) {
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 0 ; j < n ; j++) d[S][i][j] = inf,vis[i][j] = 0 ;
    }
    d[S][S][0] = 0 ;
    Q.push(node(S,0)) ;
    while (!Q.empty()) {
        node u = Q.front() ;
        Q.pop() ;
        if (vis[u.x][u.y]++) continue ;
        for(unsigned i = 0 ; i < g[u.x].size() ; i++) {
            int v = g[u.x][i].x,t = u.y + g[u.x][i].y ;
            if (t < 0) continue ;
            if (d[S][u.x][u.y] + 1 < d[S][v][t]) {
                d[S][v][t] = d[S][u.x][u.y] + 1 ;
                Q.push(node(v,t)) ;
            }
        }
    }
    return ;
}

signed main () {
    //freopen("bracketpath.in","r",stdin) ;
    //freopen("bracketpath.out","w",stdout) ;

    cin>>n>>m>>q ;
    for (int i = 1,u,v ; i <= m ; i++) {
        char c ;
        cin>>u>>v>>c ;
        g[u].push_back(node(v,c == '(' ? 1 : -1)) ;
        g[v].push_back(node(u,c == '(' ? 1 : -1)) ;
    }
    for(int i = 1 ; i <= n ; i++) dij(i) ;
    while (q--) {
        int x,y ;
        cin>>x>>y ;
        if (d[x][y][0] == inf) printf("-1\n") ;
        else printf("%d\n",d[x][y][0]) ;
    }
    return 0 ;
}

T4 彩色棋盘 (colorful,1s,512MB)

题目描述

有一个 n 行 n 列的网格。
有 m 次操作,每次操作给定 lx,rx,ly,ry,c,表示对于所有第 x 行,第 y 列的格点,如果满足 l x ≤x ≤r x,l y ≤y ≤r y,则在这个位置上放入一枚染色为 c 的棋子(同一个格子可以放入多枚棋子)。

小 Z 想知道 m 次操作后,有多少个格子内部有全部 C 种颜色的棋子。

输入格式

第一行三个整数 n,m,C 表示棋盘大小,操作次数,颜色种类数。
接下来 m 行,每行五个整数 lx,rx,ly,r y,c 表示一次操作 ( 1 ≤c ≤C )。

输出格式

输出一行一个整数,表示内部有全部 C 种颜色的棋子的格点数量。

输入输出样例 #1

说明/提示

核心思路

  1. 扫描线降维:将二维网格问题转化为一维问题。按行扫描(从第 1 行到第 n 行),对每一行维护所有列的颜色覆盖状态。
  2. 事件处理:每个操作 (lx, rx, ly, ry, c) 被拆分为两个事件:
  1. 线段树维护列状态:使用线段树维护当前行所有列的颜色覆盖状态。线段树每个节点存储:
  1. 状态合并:在线段树更新过程中,每个节点的覆盖状态 z(由其 cnt 计算)会被应用到子节点的状态上,实现状态合并。
  2. 查询答案:对每一行,处理完该行的事件后,查询线段树根节点中状态为全 1(即 s[A-1])的列数,并累加到答案中。这表示当前行有多少列包含全部颜色。

算法流程

  1. 读入数据:网格大小 n、操作数 m、颜色数 C。
  2. 初始化线段树:构建区间 [1, n] 的线段树,初始状态无颜色覆盖(所有位置掩码为 0)。
  3. 预处理事件:对每个操作,在 upt[lx] 添加添加事件,在 upt[rx+1] 添加移除事件。
  4. 扫描每一行:
  1. 输出答案:累计值即为所有满足条件的格子数量。

线段树更新原理

  1. 计算当前节点的覆盖掩码 z:遍历每种颜色,若 cnt[i] > 0 则设置掩码第 i 位。
  2. 初始化 s 数组为 0。
  3. 叶子节点:直接置 s[z] = 1(只有一个列,状态为 z)。
  4. 非叶子节点:将左右子树的状态数组与掩码 z 合并:
    for (int i = 0; i < A; i++)
    t[id].s[i | z] += t[Lid].s[i] + t[Rid].s[i];

void build (int id,int L,int R) { t[id].s[0] = R - L + 1 ; // 初始化: 所有列状态为 0 if (L != R) { build(Lid,L,Mid) ; // 递归构建左子树 build(Rid,Mid + 1,R) ; // 递归构建右子树 } }

void update(int id,int L,int R,int l,int r,int col,int tag) { if (r < L || R < l) return ; // 无交集,直接返回 if (l <= L && R <= r) { // 当前节点区间完全包含在更新区间内 t[id].cnt[col] += tag ; // 更新颜色覆盖次数 } else { // 否则递归更新子树 update(Lid,L,Mid,l,r,col,tag) ; update(Rid,Mid + 1,R,l,r,col,tag) ; } // 计算当前覆盖掩码 z (存在的颜色集合) int z = 0 ; for (int i = 0 ; i < C ; i++) { if (t[id].cnt[i] > 0) z |= (1 << i) ; } // 重置状态数组 s for (int i = 0 ; i < A ; i++) t[id].s[i] = 0 ; if (L == R) { // 叶子节点: 当前列状态为 z t[id].s[z] = 1 ; } else { // 合并左右子树状态: 子树的掩码 i 与当前掩码 z 取或 for (int i = 0 ; i < A ; i++) { t[id].s[i | z] += t[Lid].s[i] + t[Rid].s[i] ; } } }

signed main() { scanf("%d %d %d",&n,&m,&C) ; A = 1 << C ; // 状态总数 = 2^C build(1,1,n) ; // 构建线段树 // 处理每个操作,生成事件 for (int i = 1,lx,rx,ly,ry,c ; i <= m ; i++) { scanf("%d %d %d %d %d",&lx,&rx,&ly,&ry,&c) ; c-- ; // 颜色转为 0-indexed upt[lx].push_back(node(ly,ry,c,1)) ; // 添加事件 upt[rx + 1].push_back(node(ly,ry,c,-1)) ; // 移除事件 } long long ans = 0 ; for (int i = 1 ; i <= n ; i++) { // 扫描每一行 // 处理当前行事件 for (unsigned j = 0 ; j < upt[i].size() ; j++) { node& e = upt[i][j] ; update(1,1,n,e.l,e.r,e.col,e.tag) ; } ans += t[1].s[A - 1] ; // 累加当前行全颜色覆盖的列数 } printf("%lld\n",ans) ; return 0 ; }