题目链接:https://vjudge.net/contest/339284#overview
A.Numbers
待做
B.Broken Watch
s = input()
s = s.split(" ")
A,B,C,N = list(map(int,s))
n = (N-1) // 2
ret = N*N*N - 3*N*(n)*(n-1) - N - 3*N*(N-1)
ans = ret
mod = 1<<64
if (A==B and B==C) :
ans = ans // 6
elif A==B or B==C or A==C :
ans = ans //2
print (ans%mod)
C.Tree
枚举根节点然后bfs找最近的m个点
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年10月31日 星期四 13时30分46秒
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e2 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
struct Node {
int u;
int cnt;
};
int a[maxn];
bool vis[maxn];
int p[maxn];
vector<int> vv[maxn];
int dis[maxn][maxn];
ll solve(int st) {
mes(vis, 0);
vis[st] = 1;
queue<Node> q;
Node now, nex;
now.u = st, now.cnt = 0;
q.push(now);
int cnt = 0;
while(!q.empty()) {
now = q.front();
q.pop();
if(p[now.u]) {
a[++cnt] = now.u;
if(cnt == m) break;
}
for(auto v : vv[now.u]) {
if(vis[v]) continue;
vis[v] = 1;
nex.u = v, nex.cnt = now.cnt+1;
q.push(nex);
}
}
int ans = 0;
for (int i=1;i<=cnt;i++) {
for (int j=i+1;j<=cnt;j++) {
ans = max(ans,dis[a[i]][a[j]]);
}
}
return ans;
}
void dfs(int u,int fa,int d,int* di){
di[u] = d;
for (int v:vv[u]) if (v != fa) {
dfs(v,u,d+1,di);
}
}
int main() {
// freopen("in", "r", stdin);
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) {
scanf("%d", &p[i]);
vv[i].clear();
}
for(int i=1, u, v; i<n; i++) {
scanf("%d%d", &u, &v);
vv[u].pb(v);
vv[v].pb(u);
}
ll ans = INF;
for (int i=1;i<=n;i++) {
dfs(i,0,0,dis[i]);
}
for(int i=1; i<=n; i++) {
ans = min(ans, solve(i));
}
printf("%lld\n", ans);
return 0;
}
D.Space Station
#include<bits/stdc++.h>
using namespace std;
const int mx = 1e3+5;
typedef long long ll;
struct edge{
int v,w;
};
vector<edge>g[mx];
int dp[mx][mx*2];
int sum[mx*2];
int sz[mx];
int n,m,k;
void dfs(int u,int fa){
sz[u] = 2;
dp[u][1] = dp[u][0] = 0;
for(edge it: g[u]){
int v = it.v;
int w = it.w;
if(v==fa) continue;
dfs(v,u);
memset(sum,63,sizeof(sum));
for(int i = 0; i <= sz[u]; i++)
for(int j = 0; j <= sz[v]; j++){
int k = i+j;
if(j&1) sum[k] = min(sum[k],dp[v][j]+dp[u][i]+w);
else sum[k] = min(sum[k],dp[v][j]+dp[u][i]+2*w);
}
sz[u] += sz[v];
memcpy(dp[u],sum,sizeof(sum));
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
memset(dp,63,sizeof(dp));
for(int i = 1; i <= n; i++)
g[i].clear();
for(int i = 2; i <= n; i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(edge{v,w});
g[v].push_back(edge{u,w});
}
dfs(1,0);
ll ans = 1e11;
for(int i = 0; i <= 2*m; i+=2)
ans = min(ans,dp[1][i]+1ll*i/2*k);
printf("%lld\n",ans);
}
return 0;
}
E.Fisherman
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lowbit(x) x&(-x)
const int mx = 3e5+5;
struct point{
ll x,y,val;
bool operator<(const point &a)const{
return x<a.x;
}
}a[mx];
struct query{
int id;
ll val;
bool operator<(const query &a)const{
return val<a.val;
}
}q[mx];
ll x[mx];
int sum[mx];
int ans[mx];
int n,m;
void add(int k){
while(k<=n){
sum[k]++;
k += lowbit(k);
}
}
int query(int k){
int ans = 0;
while(k){
ans += sum[k];
k -= lowbit(k);
}
return ans;
}
int main(){
ll l;
scanf("%d%d%lld",&n,&m,&l);
for(int i = 1; i <= n; i++)
scanf("%lld%lld",&a[i].x,&a[i].y),a[i].val = a[i].y-a[i].x,x[i] = a[i].val;
for(int i = 1; i <= m; i++)
scanf("%lld",&q[i].val),q[i].id = i;
sort(q+1,q+m+1);
sort(a+1,a+n+1);
sort(x+1,x+n+1);
for(int i = 1,j = 1; i <= m; i++){
while(a[j].x<q[i].val&&j<=n){
int k = lower_bound(x+1,x+n,a[j].val)-x;
add(k);
j++;
}
int k = upper_bound(x+1,x+n+1,l-q[i].val)-x;
k--;
ans[q[i].id] += query(k);
}
memset(sum,0,sizeof(sum));
reverse(q+1,q+m+1);
reverse(a+1,a+n+1);
for(int i = 1; i <= n; i++)
x[i] = a[i].val = a[i].x+a[i].y;
sort(x+1,x+n+1);
for(int i = 1,j = 1; i <= m; i++){
while(a[j].x>=q[i].val&&j<=n){
int k = lower_bound(x+1,x+n,a[j].val)-x;
add(k);
j++;
}
int k = upper_bound(x+1,x+n+1,l+q[i].val)-x;
k--;
ans[q[i].id] += query(k);
}
for(int i = 1; i <= m; i++)
printf("%d\n",ans[i]);
return 0;
}
F.Min Max Convert
一个结论就是要么左更新,要么右更新,左更新从右到左,右更新从左到右,而且不互相包含。而且把一个区间变成一个端点的值最多只要操作两次
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> pa;
const int mx = 1e5 + 10;
int n,m;
int a[mx],b[mx];
vector <pa> g[2];
struct node {
char ch;
int l,r;
}s[mx<<1];
bool solve() {
int siz = 0;
for (int i=1,j=1;i<=n;i++) {
while (j <= n && a[j] != b[i]) j++;
if (j == n+1) return 0*puts("-1");
if (i < j) g[0].push_back(pa(i,j));
if (i > j) g[1].push_back(pa(j,i));
}
int pre = 0;
for (int i=0;i<g[0].size();i++) {
pa now = g[0][i];
int r = now.y,l = max(pre,now.x);
int mi = 1e6, ma = -1;
if (l == r) continue;
while (l < r) {
mi = min(a[l],mi);
ma = max(a[l++],ma);
}
pre = now.y;
if (ma <= a[r]) s[siz++] = {'M',now.x,now.y};
else if (mi >= a[r]) s[siz++] = {'m',now.x,now.y};
else {
s[siz++] = {'m',now.x,now.y-1};
s[siz++] = {'M',now.x,now.y};
}
}
pre = 1e6;
for (int i=g[1].size()-1;i>=0;i--) {
pa now = g[1][i];
int r = min(now.y,pre), l = now.x;
int mi = 1e7, ma = -1;
if (l == r) continue;
while (l < r) {
mi = min(mi,a[r]);
ma = max(ma,a[r--]);
}
pre = now.x;
if (ma <= a[r]) s[siz++] = {'M',now.x,now.y};
else if (mi >= a[r]) s[siz++] = {'m',now.x,now.y};
else {
s[siz++] = {'m',now.x+1,now.y};
s[siz++] = {'M',now.x,now.y};
}
}
printf("%d\n",siz);
for (int i=0;i<siz;i++)
printf("%c %d %d\n",s[i].ch,s[i].l,s[i].r);
}
int main() {
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",a+i);
for (int i=1;i<=n;i++)
scanf("%d",b+i);
solve();
return 0;
}
G.Matrix Queries
待做
H.Modern Djinn
待做
I.Inversion
代码丢了,应该是dp求路径个数
J.Rabbit vs Turtle
读懂题目基本就会了,主要是要注意如果路径下一跳是在最短路径上是不能作弊的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
int n,m;
int p_t,p_r;
int a[mx],b[mx];
int ans[mx],siz;
ll c[mx],suf[mx],dis[mx];
struct node {
int u,v;
int T,R;
}s[mx<<1];
vector <int> g[mx];
bool vis[mx];
void spfa(int beg) {
queue <int> q;
q.push(beg);
for (int i=1;i<=n;i++)
dis[i] = 1e16;
dis[beg] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int idx : g[u]) {
node now = s[idx];
int v = now.u; // 反向边
if (dis[v] > dis[u] + now.R) {
dis[v] = dis[u] + now.R;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
int u,v,T,R;
for (int i=1;i<=m;i++) {
scanf("%d%d%d%d",&u,&v,&T,&R);
s[i] = {u,v,T,R};
g[v].push_back(i);
}
scanf("%d",&p_t);
for (int i=1;i<=p_t;i++) {
scanf("%d%lld",a+i,c+i);
}
for (int i=p_t;i>=1;i--)
suf[i] = suf[i+1] + s[a[i]].T;
scanf("%d",&p_r);
for (int i=1;i<=p_r;i++)
scanf("%d",b+i);
spfa(n);
ll r_time = 0,t_time = 0;
for (int i=1,j=1;i<=p_r;i++) {
while (j <= p_t && t_time + s[a[j]].T <= r_time) {
t_time += s[a[j]].T + c[j];
j++;
}
if (dis[s[b[i]].u] == dis[s[b[i]].v] + s[b[i]].R) {
r_time += s[b[i]].R;
continue;
}
if (r_time + dis[s[b[i]].u] <= t_time + suf[j])
ans[siz++] = s[b[i]].u;
r_time += s[b[i]].R;
}
sort(ans,ans+siz);
printf("%d\n",siz);
for (int i=0;i<siz;i++)
printf("%d%c",ans[i],i==siz-1?'\n':' ');
return 0;
}
CDQ分治,不过这题麻烦就在于,矩形和点要分开计算,可以合在一起不过就是常数大了一倍,而且点计算矩形和矩形计算点的方式也不太一样
#include <bits/stdc++.h>
using namespace std;
const int mx = 8e5 + 50;
typedef long long ll;
struct node {
int x,y;
int v,id;
bool operator < (node A) const {
return x < A.x;
}
}s[mx],tmp[mx];
int siz,n,b[mx],tot;
int sum1[mx],dp[mx],sum2[mx];
void add(int x,int v,int* sum) {
for(;x<tot;x+=x&(-x)) sum[x] += v;
}
int get_sum(int x,int* sum) {
int ans = 0;
for(;x;x-=x&(-x)) ans += sum[x];
return ans;
}
void cdq_divide(int l,int r) {
if (l==r) return ;
int mid = l + r >> 1;
cdq_divide(l,mid);
for (int i=l;i<=r;i++) tmp[i] = s[i];
sort(tmp+l,tmp+mid+1);
sort(tmp+mid+1,tmp+r+1);
int i = l,j = mid + 1;
while (j <= r) {
if (i <= mid && tmp[i].x <= tmp[j].x) {
if (!tmp[i].v) add(tmp[i].y,1,sum1);
if (tmp[i].id<0) add(tmp[i].y,tmp[i].v,sum2);
i++;
} else {
if (tmp[j].id>0) {
if (tmp[j].v)
dp[tmp[j].id] += tmp[j].v*get_sum(tmp[j].y,sum1);
else
dp[tmp[j].id] += get_sum(tmp[j].y,sum2);
}
j++;
}
}
for (i=l;i<=mid&&tmp[i].x<=tmp[r].x;i++) {
if (!tmp[i].v) add(tmp[i].y,-1,sum1);
if (tmp[i].id<0) add(tmp[i].y,-tmp[i].v,sum2);
}
cdq_divide(mid+1,r);
}
int main() {
//freopen("1.in","r",stdin);
scanf("%d",&n);
for (int i=1;i<=n;i++) {
int c,x1,x2,y1,y2;
scanf("%d",&c);
if (c == 1) {
scanf("%d%d",&x1,&y1);
b[++tot] = y1;
s[++siz] = {x1,y1,0,i};
} else {
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
s[++siz] = {x2,y2,1,i};
s[++siz] = {x1,y1,1,-i};
s[++siz] = {x1-1,y1-1,1,i};
s[++siz] = {x2+1,y2+1,1,-i};
s[++siz] = {x1-1,y2,-1,i};
s[++siz] = {x1,y2+1,-1,-i};
s[++siz] = {x2,y1-1,-1,i};
s[++siz] = {x2+1,y1,-1,-i};
b[++tot] = y2,b[++tot] = y2+1;
b[++tot] = y1,b[++tot] = y1-1;
}
}
sort(b+1,b+1+tot);
tot = unique(b+1,b+1+tot) - b;
for (int i=1;i<=siz;i++)
s[i].y = lower_bound(b+1,b+tot,s[i].y) - b;
cdq_divide(1,siz);
ll ans = 0;
for (int i=1;i<=n;i++) {
ans += dp[i];
printf("%lld\n",ans);
}
return 0;
}