memset

【网络流24题】负载平衡问题

狂风中的少年 提交于 2019-11-28 19:56:44
这又是一道我没有当场想出来的题 题面 https://www.luogu.org/problemnew/show/P4016 题解 $S$连初始状态,末状态连$T$,转移边流量为$INF$,费用为$1$。 #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<queue> #define N 105 #define INF 1000000007 #define T (n+1) #define S 0 #define LL long long #define ri register int using namespace std; int n,a[N]; struct graph { vector<int> to,w,c; vector<int> ed[N]; LL dis[N]; int cur[N]; bool vis[N]; void add_edge(int a,int b,int aw,int ac) { to.push_back(b); w.push_back(aw); c.push_back(ac); ed[a].push_back(to.size()-1); to.push_back(a); w.push_back(0); c.push_back(-ac); ed

How memset initializes an array of integers by -1?

試著忘記壹切 提交于 2019-11-28 18:31:26
The manpage says about memset : #include <string.h> void *memset(void *s, int c, size_t n) The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c . It is obvious that memset can't be used to initialize int array as shown below: int a[10]; memset(a, 1, sizeof(a)); it is because int is represented by 4 bytes (say) and one can not get the desired value for the integers in array a . But I often see the programmers use memset to set the int array elements to either 0 or -1 . int a[10]; int b[10]; memset(a, 0, sizeof(a)); memset(b, -1, sizeof(b));

How to use VC++ intrinsic functions w/o run-time library

安稳与你 提交于 2019-11-28 17:29:20
I'm involved in one of those challenges where you try to produce the smallest possible binary, so I'm building my program without the C or C++ run-time libraries (RTL). I don't link to the DLL version or the static version. I don't even #include the header files. I have this working fine. Some RTL functions, like memset() , can be useful, so I tried adding my own implementation. It works fine in Debug builds (even for those places where the compiler generates an implicit call to memset() ). But in Release builds, I get an error saying that I cannot define an intrinsic function. You see, in

Reset C int array to zero : the fastest way?

前提是你 提交于 2019-11-28 14:49:40
问题 Assuming that we have a T myarray[100] with T = int, unsigned int, long long int or unsigned long long int, what is the fastest way to reset all its content to zero (not only for initialization but to reset the content several times in my program)? Maybe with memset? Same question for a dynamic array like T *myarray = new T[100] . 回答1: memset (from <string.h> ) is probably the fastest standard way, since it's usually a routine written directly in assembly and optimized by hand. memset(myarray

why the release version memset is slower than debug version in visual studio 2012?

丶灬走出姿态 提交于 2019-11-28 12:34:48
why the release version memset is slower than debug version in visual studio 2012? in visual sutido 2010, it is that result too. my computer: Intel Core i7-3770 3.40GHz 8G memory os: windows 7 sp1 64bit this is my test code: #include <boost/progress.hpp> int main() { const int Size = 1000*1024*1024; char* Data = (char*)malloc(Size); #ifdef _DEBUG printf_s("debug\n"); #else printf_s("release\n"); #endif boost::progress_timer timer; memset(Data, 0, Size); return 0; } the output: release 0.27 s debug 0.06 s edited: if i change code to this, it will get the same result: #include <boost/progress

cudaMemset() - does it set bytes or integers?

放肆的年华 提交于 2019-11-28 10:11:17
From online documentation: cudaError_t cudaMemset (void * devPtr, int value, size_t count ) Fills the first count bytes of the memory area pointed to by devPtr with the constant byte value value. Parameters: devPtr - Pointer to device memory value - Value to set for each byte of specified memory count - Size in bytes to set This description doesn't appear to be correct as: int *dJunk; cudaMalloc((void**)&dJunk, 32*(sizeof(int)); cudaMemset(dJunk, 0x12, 32); will set all 32 integers to 0x12, not 0x12121212. (Int vs Byte) The description talks about setting bytes. Count and Value are described

Why do ZeroMemory, etc. exist when there are memset, etc. already?

余生颓废 提交于 2019-11-28 09:39:41
Why does ZeroMemory() , and similar calls exist in the Windows API when there are memset and related calls in the C standard library already? Which ones should I call? I can guess the answer is "depends". On what? In C and C++, ZeroMemory() and memset() are the exact same thing. /* In winnt.h */ #define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length)) /* In winbase.h */ #define ZeroMemory RtlZeroMemory Why use ZeroMemory() then? To make it obvious. But I prefer memset() in C or C++ programs. The actual reason is that on a different platform it might be implemented in a more

[BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化)

梦想的初衷 提交于 2019-11-28 08:30:37
[BZOJ4182]Shopping (点分治+树上多重背包+单调队列优化) 题面 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街。商店街有n个商店,并且它们之间的道路构成了一颗树的形状。 第i个商店只卖第i种物品,小苗对于这种物品的喜爱度是wi,物品的价格为ci,物品的库存是di。但是商店街有一项奇怪的规定:如果在商店u,v买了东西,并且有一个商店w在u到v的路径上,那么必须要在商店w买东西。小葱身上有m元钱,他想要尽量让小苗开心,所以他希望最大化小苗对买到物品的喜爱度之和。这种小问题对于小葱来说当然不在话下,但是他的身边没有电脑,于是他打电话给同为OI选手的你,你能帮帮他吗? 分析 由题意,如果在u,v买了东西,那u到v的路径上的所有点一定都得买。因此最后购买东西的点一定是一个联通块。显然这个联通块一定包含某个子树的重心,所以可以点分治。 当我们点分治到某个重心rt的时候,我们考虑对这个重心分治出来子树计算背包。显然子树中的一个节点被选,它的父亲也一定被选。那么我们就把问题转化成了一般的树形背包问题,注意结点x必须选。 那么就可以列出模板的dp方程。设 \(dp[i][j]\) 表示当前处理到dfs序为i的节点x,当前背包内物品价格不大于j,能得到的最大喜爱度。设x子树的下一个子树dfs序的开始位置为nex[x].x为dfs序i对应的节点 则 \(dp[i]

how to set pointer to a memory to NULL using memset?

匆匆过客 提交于 2019-11-28 04:45:00
问题 I have a structure typedef struct my_s { int x; ... } my_T; my_t * p_my_t; I want to set the address of p_my_t to NULL and so far this is how I've tried to do this: memset (&p_my_t, 0, sizeof(my_t*)) This doesn't not look right to me though. What is the correct way of doing this? Amendment to question - asking a radically more complex question : Here is what I am trying to do: Two processes, A and B malloc p_my_t in A, B has N threads and can access it Start deleting in A but I can not simply

Why is std::fill(0) slower than std::fill(1)?

核能气质少年 提交于 2019-11-28 03:45:49
I have observed on a system that std::fill on a large std::vector<int> was significantly and consistently slower when setting a constant value 0 compared to a constant value 1 or a dynamic value: 5.8 GiB/s vs 7.5 GiB/s However, the results are different for smaller data sizes, where fill(0) is faster: With more than one thread, at 4 GiB data size, fill(1) shows a higher slope, but reaches a much lower peak than fill(0) (51 GiB/s vs 90 GiB/s): This raises the secondary question, why the peak bandwidth of fill(1) is so much lower. The test system for this was a dual socket Intel Xeon CPU E5-2680