A
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll x,y,a,b;
ll t;
cin>>t;
while(t--){
cin>>x>>y>>a>>b;
if((y-x)%(a+b))puts("-1");
else{
cout<<(y-x)/(a+b)<<endl;
}
}
}
B
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string,ll>mp;
int main(){
ll n,m;
cin>>n>>m;
string S;
for(ll i=0;i<n;i++){
string s;
cin>>s;
reverse(s.begin(),s.end());
if(mp[s]){
S+=s;
mp[s]--;
}
else{
reverse(s.begin(),s.end());
mp[s]++;
}
}
string odd;
for(auto it:mp){
if(it.second==0)
continue;
bool flag=true;
string s=it.first;
for(ll i=0;i<s.size()/2;i++){
if(s[i]!=s[s.size()-i-1]){
flag=false;
break;
}
}
if(flag){
odd=s;
break;
}
}
string rev=S;
reverse(rev.begin(),rev.end());
string ans=S+odd+rev;
cout<<ans.size()<<endl;
cout<<ans<<endl;
}
C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T[101],L[101],H[101];
int main(){
ll t;
cin>>t;
while(t--){
ll n,m;
cin>>n>>m;
bool flag=true;
for(ll i=1;i<=n;i++){
cin>>T[i]>>L[i]>>H[i];
}
ll hign=m,low=m;
for(ll i=1;i<=n;i++){
hign+=T[i]-T[i-1];
low-=T[i]-T[i-1];
if(low>H[i]||hign<L[i]){
flag=false;
}
else{
hign=min(hign,H[i]);
low=max(low,L[i]);
}
}
if(flag){
puts("YES");
}
else{
puts("NO");
}
}
}
D
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
ll ans1[maxn],ans2[maxn];
char s[maxn];
int main(){
ll t;
scanf("%lld",&t);
getchar();
while(t--){
ll n;
scanf("%lld",&n);
getchar();
scanf("%s",s+1);
ll pos=1;
ll cur=n;
vector<ll>vec;
vec.push_back(0);
for(ll i=1;i<n;i++){
if(s[i]=='>'){
ans1[i]=cur--;
vec.push_back(i);
}
}
vec.push_back(n+1);
for(ll i=1;i<vec.size();i++){
for(ll j=vec[i]-1;j>vec[i-1];j--)
ans1[j]=cur--;
}
cur=n;
for(ll i=n-1;i>0;i--){
if(s[i]=='<')
ans2[i+1]=cur--;
}
ans2[1]=cur--;
for(int i=1;i<n;i++){
if(s[i]=='>')
ans2[i+1]=cur--;
}
for(int i=1;i<=n;i++){
printf("%lld%c",ans1[i],i==n?'\n':' ');
}
for(int i=1;i<=n;i++){
printf("%lld%c",ans2[i],i==n?'\n':' ');
}
}
}
E
lca模板
倍增的写法,点从0开始标号
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int INF = 0x7fffffff;
const int N = 100000 + 10;
const int LOG = 30;
vector<int> G[N];
int parent[LOG][N];
int depth[N];
void dfs(int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (int i = 0; i < G[v].size(); i++)
if (G[v][i] != p) dfs(G[v][i], v, d + 1);
}
void init(int root, int V) {
dfs(root, -1, 0);
for (int k = 0; k + 1 < LOG; k++) {
for (int v = 0; v < V; v++) {
if (parent[k][v] < 0) parent[k + 1][v] = -1;
else parent[k + 1][v] = parent[k][parent[k][v]];
}
}
}
int lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int k = 0; k < LOG; k++) {
if ((depth[v] - depth[u]) >> k & 1)
v = parent[k][v];
}
if (u == v) return u;
for (int k = LOG - 1; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
bool isroot[N];
int getpath(int a,int b){
int LCA=lca(a,b);
return depth[a]+depth[b]-2*depth[LCA];
}
int main() {
int n;
scanf("%d", &n);
int m;
int x,y,a,b,k;
for(int i = 0; i < n; i++) {
isroot[i] = true;
}
for(int i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
isroot[b] = false;
}
int root = 0;
for(int i = 0;i<n;i++){
if(isroot[i] == true) root = i;
}
init(root, n);
cin>>m;
for(int i=1;i<=m;i++){
cin>>x>>y>>a>>b>>k;
int ans1,ans2,ans3;
ans1=getpath(a-1,b-1);
ans2=getpath(a-1,x-1)+1+getpath(y-1,b-1);
ans3=getpath(a-1,y-1)+1+getpath(x-1,b-1);
bool flag=false;
if(k>=ans1&&!((k-ans1)&1))
flag=true;
if(k>=ans2&&!((k-ans2)&1))
flag=true;
if(k>=ans3&&!((k-ans3)&1))
flag=true;
if(flag)puts("YES");
else puts("NO");
}
return 0;
}