Simple Template

公冶泰
2023-12-01

CodeforcesT

#include <set>
#include <map>
#include <deque>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define reset(x) { memset(x, -1, sizeof(x)); }

#ifndef ONLINE_JUDGE
#define debug(x) { cerr<<#x<<" = "<<(x)<<endl; }
#define DBG(id) { cerr<<"DEBUG LABLE = "<<(id)<<endl; }
#else
#define debug(x) {}
#define DBG(id) {}
#endif

const int N = (int)1e6 + 10;
const int M = (int)1e9 + 7;
/*------------------------------------------------------------------------*/

int n, a[N];
int mina, maxa;

void track()
{
	int tres = -1;
	sort(a + 1, a + n + 1);
	rep(i, 1, n)
	{
		
	}
	
	cout << tres << endl;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("C:\\Users\\Administrator\\Desktop\\IDE\\in.txt","r",stdin);
	#endif
	cin >> n;
	rep(i, 1, n)
	{
		cin >> a[i];
	}
	track();
	return 0;
}









1:单调队列


读题读样例。


PS:求滚动区间最值 神器


//单调递增队列1   子区间动态更新 
const int maxn=100;
int que[maxm*10+5], idx[maxm*10+5], head = 1, tail = 0;
void queini(){
	head = 1, tail = 0, que[0] = 0;// 单调减 que[0] = 99999999; 
} 
int quein(int index, int value)
{
	while(head <= tail && que[tail] <= value) tail--;
	que[++tail] = value;
	idx[tail] = index;
	return 0;
}
int queout(int index, int distence)//index 索引   distence离索引距离 
{
	while(idx[head] <= index - distence) head++;
	return 0;
}

//单调递增队列2
int num[1000001], que[1000001], first, tail;
void queini(){
	first = 1, tail = 0, que[0] = 0;
}
int quein(int index)
{
	while(num[que[tail]] >= num[index] && first <= tail) tail--;
	que[++tail] = index;
	return 0;
}
int queout(int index, int distence)
{
	while (que[first] <= index - distence) first++;
	return 0;
}





5:线段树


成段 + C, 查询区间和

#include <cstdio>  
#include <algorithm>  
using namespace std;  
   
#define lson l , m , rt << 1  
#define rson m + 1 , r , rt << 1 | 1  
#define LL long long  
const int maxn = 111111;  

/* Node Begin */
LL add[maxn<<2];  
LL sum[maxn<<2];  
/* Node End */

void PushUp(int rt) {  
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];  
}  
void PushDown(int rt,int m) {  
    if (add[rt]) {  
        add[rt<<1] += add[rt];  
        add[rt<<1|1] += add[rt];  
        sum[rt<<1] += add[rt] * (m - (m >> 1));  
        sum[rt<<1|1] += add[rt] * (m >> 1);  
        add[rt] = 0;  
    }  
}  
void build(int l,int r,int rt) {  
    add[rt] = 0;  
    if (l == r) {  
        scanf("%lld",&sum[rt]);  
        return ;  
    }  
    int m = (l + r) >> 1;  
    build(lson);  
    build(rson);  
    PushUp(rt);  
}  
void update(int L,int R,int c,int l,int r,int rt) {  
    if (L <= l && r <= R) {  
        add[rt] += c;  
        sum[rt] += (LL)c * (r - l + 1);  
        return ;  
    }  
    PushDown(rt , r - l + 1);  
    int m = (l + r) >> 1;  
    if (L <= m) update(L , R , c , lson);  
    if (m < R) update(L , R , c , rson);  
    PushUp(rt);  
}  
LL query(int L,int R,int l,int r,int rt) {  
    if (L <= l && r <= R) {  
        return sum[rt];  
    }  
    PushDown(rt , r - l + 1);  
    int m = (l + r) >> 1;  
    LL ret = 0;  
    if (L <= m) ret += query(L , R , lson);  
    if (m < R) ret += query(L , R , rson);  
    return ret;  
}  
int main() {  
    int N , Q;  
    scanf("%d%d",&N,&Q);  
    build(1 , N , 1);  
    while (Q --) {  
        char op[2];  
        int a , b , c;  
        scanf("%s",op);  
        if (op[0] == 'Q') 
		{  
            scanf("%d%d",&a,&b);  
            printf("%lld\n",query(a , b , 1 , N , 1));  
        } 
		else 
		{  
            scanf("%d%d%d",&a,&b,&c);  
            update(a , b , c , 1 , N , 1);  
        }  
    }  
    return 0;  
}  


离散化 扫描线

sort(X,X+nn);/*unique前必须sort*/  
nn = unique(X, X + nn) - X; /*去重并得到总数*/      
        
#include <cstdio>  
#include <cstring>  
#include <cctype>  
#include <algorithm>  
using namespace std;  
#define lson l , m , rt << 1  
#define rson m + 1 , r , rt << 1 | 1  
   
const int maxn = 2222;  
int cnt[maxn << 2];  
double sum[maxn << 2];  
double X[maxn];  
struct Seg {  
    double h , l , r;  
    int s;  
    Seg(){}  
    Seg(double a,double b,double c,int d) : l(a) , r(b) , h(c) , s(d) {}  
    bool operator < (const Seg &cmp) const {  
        return h < cmp.h;  
    }  
}ss[maxn];  
void PushUp(int rt,int l,int r) {  
    if (cnt[rt]) sum[rt] = X[r+1] - X[l];  
    else if (l == r) sum[rt] = 0;  
    else sum[rt] = sum[rt<<1] + sum[rt<<1|1];  
}  
void update(int L,int R,int c,int l,int r,int rt) {  
    if (L <= l && r <= R) {  
        cnt[rt] += c;  
        PushUp(rt , l , r);  
        return ;  
    }  
    int m = (l + r) >> 1;  
    if (L <= m) update(L , R , c , lson);  
    if (m < R) update(L , R , c , rson);  
    PushUp(rt , l , r);  
}  
int Bin(double key,int n,double X[]) {  
    int l = 0 , r = n - 1;  
    while (l <= r) {  
        int m = (l + r) >> 1;  
        if (X[m] == key) return m;  
        if (X[m] < key) l = m + 1;  
        else r = m - 1;  
    }  
    return -1;  
}  
int main() {  
    int n , cas = 1;  
    while (~scanf("%d",&n) && n) {  
        int m = 0;  
        while (n --) {  
            double a , b , c , d;  
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);  
            X[m] = a;  
            ss[m++] = Seg(a , c , b , 1);  
            X[m] = c;  
            ss[m++] = Seg(a , c , d , -1);  
        }  
        sort(X , X + m);  
        sort(ss , ss + m);  
        int k = 1;  
        for (int i = 1 ; i < m ; i ++) {  
            if (X[i] != X[i-1]) X[k++] = X[i];  
        }  
        memset(cnt , 0 , sizeof(cnt));  
        memset(sum , 0 , sizeof(sum));  
        double ret = 0;  
        for (int i = 0 ; i < m - 1 ; i ++) {  
            int l = Bin(ss[i].l , k , X);  
            int r = Bin(ss[i].r , k , X) - 1;  
            if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1);  
            ret += sum[1] * (ss[i+1].h - ss[i].h);  
        }  
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++ , ret);  
    }  
    return 0;  
}







6、哈希

//整数hash 
const int maxn = 0xff;  
int hash[maxn], count[maxn];   
  
int hashit(int k)  
{  
    int t = k % maxn;  
    if( t < 0 )  t += maxn;  
    while( hash[t] != -1 && hash[t] != k )  t = (t + 1) % maxn;  
    if( hash[t] == -1 ) hash[t] = k, count[t] = 1;  
    else ++count[t];  
}  

//字符串hash 
const int maxn = 0xff;  
int hash[maxn], count[maxn];  
  
void init(){  
    memset(hash,-1,sizeof(hash));  
    memset(count,-1,sizeof(count));  
}  
inline unsigned int BKDRHash(char *str)  
{  
    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..  
    unsigned int hash = 0;  
    while (*str)  
    {  
        hash = hash * seed + (*str++);  
    }  
    return (hash & 0x7FFFFFFF);  
}  
  
inline void hashit(char *str)  
{  
    int k,t;  
    while( *str == '0' )    str++;  ///这里有时候不需要去零  
    k = BKDRHash(str);  
    t = k % maxn;  
    while( hash[t] != k && hash[t] != -1 )  t = ( t + 10 ) % maxn;  
    if( hash[t] == -1 ) hash[t] = k, count[t] = 1;  
    else ++count[t];  
}  



7、搜索

(1)二分

/*二分 1-n 闭区间*/
int BinarySearch(int A[],int x)
{
	int L = 1, M, R = sizeof(A) / sizeof(int) - 1;
	int Bres = -1;
	while(L < R){
		M = L + (R-L)/2;
		if(x < A[M]) R = M;
		else L = M+1;
	}
	return L;
}

(3)dfs

const int SZ = 110;
const int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};

int N, M; 
char mat[SZ][SZ], vis[SZ][SZ]; 

void dfs(int a,int b)
{
    if(vis[a][b] | mat[a][b]!='#')return;
    vis[a][b] = true;
	for(int i = 0;i < 4; ++i)
	{
		int ti = a + dir[i][0], tj = b + dir[i][1];
		if(ti<1 | ti>N | tj<1 | tj>M | vis[ti][tj])continue;
		dfs(ti,tj);
	}
}

(4)bfs

bool vis[N][N];

int bfs( int x, int y)
{
	queue<pos> Que;
	pos tmp;
	reset(vis);
	
	Que.push((pos){x, y}); 
	vis[x][y] = 1;
	
	while(!Que.empty())
	{
		tmp=Que.front(); Que.pop();
		for(int i = 0; i < 4; ++i)
		{
			int nx, ny;
			if(nx | ny) return 1;
			if(nx | ny)continue;
			q.push((pos){nx, ny}); vis[nx][ny] = 1;
		}
	}
	return 0;
}







 类似资料:

相关阅读

相关文章

相关问答